C++ MCQs - Virtual Functions
This section focuses on the "Virtual 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. A ___________ is a member function which is declared within a base class and is re-defined (overridden) by a derived class.
A. Static function
B. Virtual function
C. Lambdas function
D. Dynamic function
View Answer
Ans : B
Explanation: A virtual function is a member function which is declared within a base class and is re-defined (overridden) by a derived class.
2. Functions are declared with a _________ keyword in base class.
A. init
B. self
C. virtual
D. extend
View Answer
Ans : C
Explanation: Functions are declared with a virtual keyword in base class.
3. Virtual functions cannot be static.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: Virtual functions cannot be static
4. Which of the following are the limitations of virtual functions?
A. function call takes slightly longer time
B. virtual functions can make it a little more difficult to figure out where a function is being called from
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: Both a and b are limitations of virtual functions
5. Virtual functions are accessed through?
A. variable
B. array
C. object
D. object pointers
View Answer
Ans : D
Explanation: They are accessed through object pointers.
6. When the function has no definition, such function is known as ________ function.
A. pure virtual function
B. do-nothing
C. late function
D. Both A and B
View Answer
Ans : D
Explanation: When the function has no definition, such function is known as "do-nothing" function.The "do-nothing" function is known as a pure virtual function.
7. What is the syntax for overriding a virtual function in a derived class?
A. virtual void functionName() const;
B. void functionName() override;
C. virtual void functionName() = 0;
D. void functionName() const override;
View Answer
Ans : B
Explanation: void functionName() override;
8. Which class cannot have virtual functions?
A. Derived classes
B. Abstract classes
C. Concrete classes
D. None of the above
View Answer
Ans : C
Explanation: Concrete classes cannot have virtual function.
9. Which access specifier can a virtual function have in a base class?
A. Private
B. Protected
C. Public
D. Any of the above
View Answer
Ans : D
Explanation: Private,public and protected access specifier can a virtual function have in a base class
10. A virtual function can be a friend function of another class.
A. Yes
B. No
C. Can be yes or no
D. Can not say
View Answer
Ans : A
Explanation: Yes, A virtual function can be a friend function of another class.
Discussion