C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Functions”.
Q 1. What happens to a function defined inside a class without any complex operations (like looping, a large number of lines, etc)?
A. It becomes an inline function of the class
B. It becomes a default calling function of the class
C. It becomes a virtual function of the class
D. The program gives an error
Show Answer
Answer:-A. It becomes an inline function of the classExplanation
Any function which is defined inside a class and has no complex operations like loops, a large number of lines then it is made inline.Q 2.Which of the following is the default return value of functions in C++?
A. char
B. int
C. float
D. void
Show Answer
Answer:-B. intExplanation
C++ uses int as the default return values for functions. It also restricts that the return type of the main function must be int.Q 3.What is an inline function?
A. A function that is not checked for syntax errors
B. A function that is called during compile time
C. A function that is expanded at each call during execution
D. A function that is not checked for semantic analysis
Show Answer
Answer:-C. A function that is expanded at each call during executionExplanation
Inline function is those which are expanded at each call during the execution of the program to reduce the cost of jumping during execution. advertisementQ 4. An inline function is expanded during __
A. run-time
B. compile-time
C. never expanded
D. end of the program
Show Answer
Answer:-B. compile-timeExplanation
An inline function is expanded during the compile-time of a program.Q 5. In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
A. i, iv
B. iii, iv
C. ii, iii, iv
D. i, iii, iv
Show Answer
Answer:-D. i, iii, ivExplanation
A function is not inline if it has static variables, loops or the function is having any recursive calls.Q 6. If an argument from the parameter list of a function is defined constant then ___
A. It can be modified inside the function
B. Segmentation fault
C. Error occurs
D. It cannot be modified inside the function
Show Answer
Answer:-D. It cannot be modified inside the functionExplanation
A function is not allowed a constant member of the parameter list.Q 7. When we define the default values for a function?
A. When a function is defined
B. When a function is declared
C. When the scope of the function is over
D. When a function is called
Show Answer
Answer:-B. When a function is declaredExplanation
Default values for a function is defined when the function is declared inside a program.Q 8. Where should default parameters appear in a function prototype?
A. Anywhere inside the parameter list
B. To the leftmost side of the parameter list
C. To the rightmost side of the parameter list
D. Middle of the parameter list
Show Answer
Answer:-C. To the rightmost side of the parameter listExplanation
Default parameters are defined to the rightmost side of parameter list in a function to differentiate between the normal and default parameters for example if a function is defined as fun(int x = 5, int y) then if we call fun(10) then 10 should be given to x or y because one can apply both logics like x = 10 already defined and 10 passed is for y but if compiler reads it from left to right it will think it is for x and no parameter is given for y, therefore, the compiler will give error.Q 9. Which of the following is important in a function?
A. Function name
B. Return type
C. Both return type and function name
D. The return type, function name and parameter list
Show Answer
Answer:-A. Function nameExplanation
The important things required in a function is its return type and its name other than that parameter list are optional which a function may or may not have.Q 10. Which of the following feature is used in function overloading and function with default argument?
A. Encapsulation
B. Abstraction
C. Polymorphism
D. Modularity
Show Answer
Answer:-C. PolymorphismExplanation
Both of the above types allows a function overloading which is the basic concept of Polymorphism.Q 11. From which function the execution of a C++ program starts?
A. start() function
B. new() function
C. main() function
D. end() function
Show Answer
Answer:-C. main() functionExplanation
The execution of a C++ program starts from the main() function.Q 12. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int fun(int x = 0, int y = 0, int z)
{ return (x + y + z); }
int main()
{
cout << fun(10);
return 0;
}
A. 10
B. 0
C. Error
D. Segmentation fault
Show Answer
Answer:-C. ErrorExplanation
Default arguments should always be declared at the rightmost side of the parameter list but the above function has a normal variable at the rightmost side which is a syntax error, therefore the function gives an error.Q 13. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
class Test
{
protected:
int x;
public:
Test (int i):x(i) { }
void fun() const { cout << “fun() const ” << endl; }
void fun() { cout << “fun() ” << endl; }
};
int main()
{
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;
}
A. fun()
fun() const
B. fun() const
fun()
C. fun()
fun()
D. fun() const
fun() const
Show Answer
Answer:-A. fun() fun() constExplanation
As the object declared are of two types one is normal object and other is constant object So normal objects calls normal fun() whereas constant objects calls constant fun().Q 14. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y); }
A. -5
B. 0
C. 10
D. 5
Show Answer
Answer:-D. 5Explanation
C++ allows to define such prototype of the function in which you are not required to give variable names only the default values. While in function definition you can provide the variable names corresponding to each parameter.Q 15. What will be the output of the following C++ code?
#include<iostream>
using namespace std;
void square (int *x, int *y)
{
x = (x) * –(*y);
}
int main ( )
{
int number = 30;
square(&number, &number);
cout << number;
return 0;
}
A. 30
B. 870
C. Error
D. Segmentation fault
Leave a Reply