1. A variable declared outside any function is said to be a local variable.
2. A variable declared within a function block is said to be local to the function.
3. Consider two blocks, one within another. If an identifier is declared as a variable in the inmost of these two blocks, one can access this variable from the outer block.
4. There is only one kind
of parameter passing in C++, namely call-by-value.
5. A call-by-value parameter may pass data only into a function.
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. A call to a C++ function is
a) The name of the function followed by empty parentheses.
b) The name of the function followed by any number of arguments, regardless of the number of parameters in the definition.
c) The name of the function followed by a number of arguments not greater than the number of parameters in the definition.
d) The name of the function followed by exactly the number of arguments as there are parameters in the definition.
e) The name of the function only.
2. A void function
a) performs some action and returns a value
b) performs some action but does not return a value
c) is a statement
d) call is written much like a call to a value returning function but is terminated with a semicolon.
e) A void function may have a return statement but is not required to have one.
3. A definition of a variable outside any function is called a
a) local function definition
b) global variable definition
c) global function header
d) global function definition
e) local variable definition
4. Here is a small program. Which of the statements about the variables is correct?
#include <iostream>
using namespace std;
const double NUM = 2.9345358;
double num = 3;
double numTimes(int x);
int main()
{
int value;
cout << "Enter a value, I’ll multiply it by "
<< NUM << endl;
cin >> value;
cout << "You entered " << value
<< " NUM times this is " << numTimes(value)
<< endl;
return 0;
}
double numTimes(int x)
{
double d;
d = NUM * x;
return d;
}
a) NUM is a global variable.
b) num is a global constant.
c) value is local variable in the main function
d) d is a local variable in the numTimes function.
5. Consider the following pair of functions. You are to assume that the ellipses (. . .) contain correct code sufficient that it with the code you can see will compile apart from the two variables named sam in the two functions.
...
void func1( )
{
int sam;
...
}
void func2( )
{
int sam;
...
}
int main( )
{
func1();
func2();
...
}
a) This code will not compile because of the multiply defined variable sam. In the explanation give the compiler message you get on compiling.
b) This code will compile just fine.
c) This code will compile but will give a runtime error due to the multiply defined variable sam. In the explanation give the error message you get when this is run.
d) This code compiles and runs without any error messages. Explain why in the explanations.
6. Assume this code fragment is embedded in an otherwise correct and complete program. What should be the output from this code segment?
{
for (int i = 0; i < 10; i++)
{
...
}
cout << i << endl;
}
a) 10
b) 9
c) 0
d) The variable i is undefined in this scope, so this should not compile
7. 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;
8. 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.
9. 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.
10. 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);
11. 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.
12. 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;
}
f) Of course it works! Just look at it. It clearly swaps the two parameters!
g) It fails because the programmer forgot to make the parameters call-by-reference.
h) It fails OK, and we can fix it we can just reverse the order of the lines.
i) 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.
j) 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;