MCQ On Functions - C++ Programming
31. What will be the output of the following program?
void abc(int x=0, int y, int z=0)
{
cout << x << y << z;
}
A. abc () ;
B. abc (h) ;
C. abc (h,h) ;
D. Both A and B
View Answer
Ans : D
Explanation: Since the second argument is mandatory, any call should have at least the first two parameters. Some compilers expect the optional parameters to follow the others. Such compilers give a compilation error.
32. What will be the output of the following program?
int a =1 , b=2;
a=chg(b) ;
cout << a << b ;
If the function chg is coded as
int chg(int x)
{
x = 10;
return ();
}
then
A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 1
View Answer
Ans : C
Explanation: Here the default parameter passing mechanism of pass by value will be used. Any change done to the parameter will not be reflected outside the function. So, b retains its value 2.
33. What will be the output of the following program?
int a =1 , b=2;
a=chg(b) ;
cout << a << b ;
If the function chg is coded as
int chg(int &x)
{
x = 10;
return ();
}
then
A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 10
View Answer
Ans : D
Explanation: Here the parameter passing mechanism is pass by reference. Any change done to the parameter will be reflected outside the function. So, the value of b, after the exit of the function will be 10.
34. What will be the output of the following program?
int a =1 , b=2;
a=chg(b) ;
cout << a << b ;
If the function chg is coded as
int chg(const int &x)
{
x = 10;
return ();
}
then
A. It results in compile-time error
B. It results in run time error
C. It prints 1 1 2
D. It prints 1 1 10
View Answer
Ans : A
Explanation: Here the function chg tries to change the parameter x, which is not allowed as it is declared to be a constant integer. So, the compiler gives an error.
35. Choose the correct statements regarding inline functions.
A. It speeds up execution
B. It slows down execution
C. It increases the code size
D. Both A and C
View Answer
Ans : D
Explanation: Each occurrence of the iriline function call wiIl be replaced by its body. No function call overhead will be there but the size of the code will increase.
36. The compiler identifies a virtual function to be pure
A. by the presence of the keyword pure
B. by its location in the program
C. if it is equated to 0
D. None of the above
View Answer
Ans : C
Explanation: None
37. If many functions-have the same name, which of the following information, if present, will be used by the compiler to invoke the correct function to be used?
A. The operator : :
B. The return value of the function
C. Function signature
D. Both A & C
View Answer
Ans : D
Explanation: None
38. Which of the following parameter passing mechanism(s) is/are supported by C++, but not by C?
A. Pass by value
B. Pass by reference
C. Pass by value-result
D. All of the above
View Answer
Ans : B
Explanation: As such C does not support pass by reference. But it can be simulated by using pointers.
39. If a piece of code can be implemented as a macro or as an inline function, which of the following factors favour implementation as an inline function?
A. Interacting with other components (like variables in an expression), in the correct way ..
B. Flexibility to manipulate as a pointer
C. Source code size
D. Both A and B
View Answer
Ans : D
Explanation: Macros do not have an address associated with them as they are processed by the pre-processor.
They cannot be passed as arguments to a function, etc. Also, macros are replaced in a blind manner without any regard to the context which may result in a stupid code, like
#define mul(a,b) a*b
The macro call mul(a, b + 1) will be expanded as a *b+ 1, but our intention was to get a * (b+ 1 ) .
40. Assume that the random number generating function - rand( ), returns an integer between 0 and 10000 (both inclusive). If you want to simulate the throwing of a die using this random function, use the expression
A. rand ( ) % 6
B. rand ( ) % 6 + 1
C. rand ( ) % 5 + 1
D. None of the above
View Answer
Ans : B
Explanation: It should randomly generate any integer between 1 and 6. rand( ) % 6 returns an integer from 0 to 5. To make it 1 to 6, we need to add 1
Also check :
Discussion