C++ Programming Multiple Choice Question - Functions
This section focuses on the "Functions" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following function / types of function cannot have default parameters?
A. Member function of class
B. Main()
C. Member function of structure
D. Both B and C
View Answer
Ans : B
Explanation: None
2. Correct way to declare pure virtual function in a C++ class is
A. Virtual void foo() =0 ;
B. Void virtual foo()= { 0 }
C. Virtual void foo() {} = 0;
D. None of the above
View Answer
Ans : A
Explanation: A is the correct declaration of pure virtual function in a class in C++.NOTE: Pure virtual function is used in an Interface or an abstract class
3. What is the scope of the variable declared in the user defined function?
A. Whole program
B. Only inside the {} block
C. The main function
D. None of the above
View Answer
Ans : B
Explanation: The variable is valid only in the function block as in other.
4. Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++.
A. Inheritance
B. Polymorphism
C. Encapsulation
D. None of these
View Answer
Ans : B
Explanation: Both of the features allow one function name to work for different parameter..
5. Predict the output:
float x= 3.1496;
cout << setprecision(2) << x;
A. 3.14
B. 3.15
C. 3
D. 3.1
View Answer
Ans : D
Explanation: The output for the following code is 3.1
6. Which of the following statement is correct?
A. Only one parameter of a function can be a default parameter.
B. Minimum one parameter of a function must be a default parameter.
C. All the parameters of a function can be default parameters.
D. No parameter of a function can be default.
View Answer
Ans : C
Explanation: All the parameters of a function can be default parameters statement is correct.
7. Which of the following function declaration using default arguments is incorrect?
A. int foo(int x, int y =5, int z=10)
B. int foo(int x=5, int y =10, int z)
C. int foo(int x=5, int y, int z=10)
D. All are correct
View Answer
Ans : A
Explanation: Default arguments in a function in C++ program is initialized from right to left.
8. How are many minimum numbers of functions need to be presented in c++?
A. 0
B. 1
C. 2
D. 3
View Answer
Ans : B
Explanation: The main function is the mandatory part, it is needed for the execution of the program to start.
9. Inline functions may not work ______ .
i) If function contain static variables.
ii) If function contain global and register variables.
iii) If function returning value consists looping construct(i.e. for, while).
iv) If inline functions are recursive.
v) If function contains const value.
A. Only i,iv & v
B. Only ii,iii & v
C. Only i,iii & iv
D. All of the above
View Answer
Ans : C
Explanation: int foo(int x, int y =5, int z=10) function declaration using default arguments is incorrect.
10. Unary scope resolution operator is denoted by
A. ! !
B. % %
C. :
D. : :
View Answer
Ans : D
Explanation: 1 is the minimum numbers of functions need to be presented in c++.
Also check :
Discussion