1. When should we use a call by reference parameter? Call by value or call by re
ID: 653590 • Letter: 1
Question
1. When should we use a call by reference parameter?
Call by value or call by reference is only a matter of preference. You may use whichever and they are always the same.
When you want to pass new value back to the caller.
When there is no need to pass any value back to the caller.
When parameters are integers.
2. What is the purpose of using parameters in a function?
They should be avoided.
They are used to communicate data in and out of the function.
They can be used to receive data into the function, but not to send back.
They are used to either receive data in or send data back to caller, but not both for the same parameter.
3. You can always replace a nested if statement with a switch statement.
True
False
4. Let s say the parameters listed in the function definition are formal parameters and the parameters listed in the function
calling statement are actual parameters. How does the compiler match the actual parameters with the formal parameters?
By their names.
By their names and data types.
By position, data type, and quantity of parameters
By name, position, data type, and quantity of parameters
5. What will happen if you declare a local variable in a function with the same name as one of the function s parameters?
You get a syntax error.
Function parameters and local variables each have their own space and they don't interfere with each other.
It becomes a hard to discover logical error.
It is alright but not recommended.
6. Are the following valid variable names? _myVar, 12jun. Why?
Both are valid. Because variable names can be anything.
Both are invalid because variable names must start with a letter.
_myVar is valid but 12jun is not because variable names may begin with an underscore but not a digit
_myVar is not but 12jun is because a variable name may start with a number but not an underscore.
7. If you have a variable declaration as follows: int a; What is the value in a?
Nothing.
Zero.
Anything you want.
An unpredictable value left from the previous use of the memory location.
8. If a function needs to return more than one value, what can we do to achieve that?
Call the function more times.
Define additional return values in the function header.
Use call-by-reference parameters to achieve that.
Use a loop.
9.In a while-loop, how do you make sure that the loop is not an infinite loop?
Make sure there is a counter.
Make sure that the loop condition is false.
Make sure that the loop condition is modified in the loop body so it may become false.
It depends on the user.
10. What does the declaration of a variable do?
It gives the variable a name and value.
It names the variable and specifies the data type for the variable.
It allows the compiler to associate the variable name to a memory location of a certain size.
both b and c
11. At most how many values can a function return through its name?
None.
1
2
Any number. It's up to the programmer.
12. What is an initialization.
Initialization is the same as declaration.
To start something.
To create a variable.
To assign a variable an initial value.
13. In a function definition, what is the data type before the function name for?
It determines the function return value's datatype.
It is the value returned by the function.
It's not necessary, but when used, it makes the function faster.
It should always be int. A return value of 0 indicates the function succeeded, otherwise, failed.
14. Three loop structures were introduced in the book. If you can only keep one of them (and still be able to write any kind of
repetitions), which one would you keep? Why?
The while loop because the other two structures can be easily converted into a while loop.
The for loop because it is the simplest and the most straight forward.
The do .. while loop because it is suitable for making a menu.
You have to use all three for different looping tasks.
15. What is a variable in C++? What is it used for?
It's a named memory space for storing data.
Something that changes and can be used repeatedly.
Something that never changes and can be used only once.
It's a name that is associated to a value.
16. You can always replace a switch statement with a nested if statement.
True
False
Explanation / Answer
1. The formal arguments used in a call-by-reference are referred to as reference arguments. These formal arguments must be identified by putting the symbol & following the type of the argument in the parameter list in both the function prototype and the header of the function definition. However, the actual arguments must not have the & symbol, nor any data type specification.
The actual arguments used in the function call must be variables since their addresses are passed to the function.
2-A function can take parameters which are just values you supply to the function so that the function can do something utilising those values.
#!/usr/bin/python
# Filename: func_param.py
def printMax(a, b):
if a > b:
print a, 'is maximum'
else:
print b, 'is maximum'
printMax(3, 4) # directly give literal values
x = 5
y = 7
printMax(x, y) # give variables as arguments
3-false
4- Each expression in the function call is an actual argument.If types do not match, implicit type conversions rules will be applied. Actual arguments can be of any complexity, but order of their evaluation is not specified.
int limit = 32;
char ch = 'A';
long res;
// prototype
extern long func(long par1, long par2);
main() {
...
res = func(limit, ch); // function call
}
5- A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller. In addition, a function may optionally return a value to its caller. Consider the following simple function that accepts two numbers and returns their sum:
Add(x, y)
{
return x + y ; "Return" expects an expression.
}
Var := Add(2, 3) ; The number 5 will be stored in Var.
Also, a function may be called without storing its return value:
Add(2, 3)
if InStr(MyVar, "fox")
MsgBox The variable MyVar contains the word fox.
Finally, functions may be called in the parameters of any command (except OutputVar and InputVar parameters such as those of StringLen). However, parameters that do not support expressions must use the "% " prefix as in this example:
MsgBox % "The answer is: " . Add(3, 2)
6-
If I was forced to have both a list and set of names, I'd use names for the list, and nameSet for the set since the type implies plurality already
both are invalid because variable names must start with a letter.
7- An unpredictable value left from the previous use of the memory location.
8- If a function needs to return more than one value, what can we do to achieve that?
Some particular cases If a function needs to return more than one value, what can we do to achieve that and one for the index where we found max sxore.
* function definition to swap the values */
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
return;
}
9- An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. The while construct consists of a block of code and a condition/expression
10- both b and c
11- Any number. It's up to the programmer.
12- To assign a variable an initial value.
13- It determines the function return value's datatype.
14- The do .. while loop because it is suitable for making a menu because in do while the body of loop executes at least once.
15- It's a named memory space for storing data.
16- true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.