An explanation is required.
1. There is only one kind
of parameter passing in C++, namely call-by-value.
2. The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter.
3. A call-by-value parameter may pass data only into a function.
4. A call-by-reference parameter may pass data only out of a function.
5. Call-by-reference is restricted to void functions.
6. There is no problem with these two function definitions:
void func(int x){/* ... */}
int func(double x){/* ... */ return something_double;}
7. There is no problem with the compiler distinguishing these two function definitions:
void func(double x){/* ... */}
int func(double x){/* ... */ return something_double;}
1. Write a void function using two type int call-by-reference parameters that swaps the values in the arguments.
2. Write a definition for a void-function that has two int value parameters and outputs to the screen the product of these arguments. Write a main function that asks the user for these two numbers, reads them in, calls your function, then terminates.
There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.
1. Consider the following function and code segment.
void One( int first, int & second )
{
first = 17;
second = first + 1;
}
int main()
{
// other code ...
int j = 4;
int k = 3;
One(j, k);
// other code ..
}
After the call to One(j, k); what are the values of j and k? Why?
a) j == 4, k == 3;
b) j == 17, k == 18;
c) j == 4, k == 18;
d) j == 17, k == 3;
2. Given the function, and the main function calling it: Which, if any, of the following choices is the output of the following code? What does this function do?
#include <iostream>
using namespace std;
void func ( int& x, int & y)
{
int t = x;
x = y;
y = t;
}
int main()
{
int u = 3; v = 4;
// ...
cout << u << " " << v << endl;
func ( u, v )
cout << u << " " << v << endl;
// ...
}
a) 3 4
3 3
b) 3 4
4 3
c) 3 4
3 4
d) 3 4
4 4
e) none of the above. If you choose this, you must specify the output.
3. Which of the following overloadings will be invoked by this call?
g(1,2);
a) int g(int count, double value);
b) void g(double value, int count);
c) void g(int value, int count);
d) Neither, the compiler cannot decide which of these to use.
5. Which of the following function declarations with default arguments are correct?
a) void g(int length, int width, int height = 1);
b) void g(int length=1, int width, int height);
c) void g(int length, int width=1, int height = 1);
d) void g(int length=1, int width=1, int height);
6. Which of these remarks about overloading a function name is correct?
a) C++ distinguishes between function overloaded implementations by examining differences in return types.
b) C++ distinguishes between overloaded function versions by examining differences in the argument lists.
c) C++ does not support function name overloading.
d) To decide which version of overloaded functions, C++ looks first for an exact match in the argument list types and the parameter list types.
e) If there is no match between the argument list types and the parameter list types C++ expects the programmer to supply type conversions to get a match.
7. Consider the function
void doStuff(int parValue, int& parRef)
{
parValue = 100;
cout << "parValue in call to doStuff = "
<< parValue << endl;
parRef =222;
cout << "parRef in call to doStuff = "
<< parRef << endl;
}
and consider the call, which we assume is in a complete and correct program
int n1 = 1, n2 =2;
doStuff(n1, n2);
a) The call to doStuff results in the assignment n1 = 222;
b) The call to doStuff results in the assignment n2 = 222;
c) The call to doStuff results in the assignment n1 = 100;
d) The call to doStuff results in the assignment n2 = 100;
e) There is no effect on either of these variables..
8. Why does this version of the swap function fail to work? Is there a fix?
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
a) Of course it works! Just look at it. It clearly swaps the two parameters!
b) It fails because the programmer forgot to make the parameters call-by-reference.
c) It fails OK, and we can fix it we can just reverse the order of the lines.
d) It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them.
e) To fix this, we must save the lhs value in a local variable before making the first assignment indicated, then instead of the second line, assign the rhs the value of the local variable:
int local = lhs;
lhs = rhs;
rhs = local;