ProgrammingHardStatement-count style · Product-company MCQ pattern
How many of these are correct about C++ virtual functions on a typical implementation? (i) Each class with virtual functions has a table of function pointers (vtable). (ii) Each object of such a class carries a hidden pointer to its class's vtable. (iii) A constructor can be declared virtual. (iv) A static member function can be virtual.
- A. Only one
- B. Only two
- C. Only three
- D. All four
Correct answer: B. Only two
(i) and (ii) describe the usual vtable/vptr mechanism, which is why such objects grow by one pointer. (iii) false: constructors cannot be virtual (destructors can). (iv) false: static functions have no this object to dispatch on. So two.
More OOP with C++ and Java questions
- A method call such as shape.area() runs different code depending on whether shape refers to a Circle or a Square object at run time. Which…
- A Java class declares only the constructor Account(String id) and no other constructor. What happens with the statement new Account()?
- What is printed? java int x = 5; x = x++ + ++x; System.out.print(x);
- What is printed? cpp class A { public: int x; A(int v) : x(v) {} }; int main() { A a(5); A b = a; b.x = 9; cout << a.x << b.x; }
- Which line fails to compile? java abstract class Shape { abstract double area(); } class Sq extends Shape { double area() { return 4; } }…
- What is printed? java class K { K() { this(5); System.out.print("A"); } K(int x) { System.out.print("B" + x); } } // in main: new K();