
Inheritance and virtual functions are where C++ interviews separate memorised definitions from real understanding. This second C++ set covers static vs dynamic binding, virtual destructors, abstract classes, the diamond problem, object slicing, override and final, and the basics of templates. Assume iostream is included with using namespace std.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. What is printed?
class B { public: void f() { cout << "B"; } };
class D : public B { public: void f() { cout << "D"; } };
int main() {
B *p = new D;
p->f();
}
a) B
b) D
c) BD
d) Compilation error
Q2. Same code as before, but B::f is now declared virtual. What is printed?
class B { public: virtual void f() { cout << "B"; } };
class D : public B { public: void f() { cout << "D"; } };
int main() {
B *p = new D;
p->f();
}
a) B
b) D
c) BD
d) Undefined behaviour
Q3. Why should a base class that is used polymorphically have a virtual destructor?
a) So that it can be called manually
b) So that delete on a base pointer also runs the derived class destructor
c) To make the class abstract
d) To stop the class being copied
Q4. Which declaration makes f a pure virtual function?
a) virtual void f() {}
b) virtual void f() = 0;
c) void f() = delete;
d) abstract void f();
Q5. What is printed?
struct B { B() { cout << "1"; } ~B() { cout << "2"; } };
struct D : B { D() { cout << "3"; } ~D() { cout << "4"; } };
int main() { D d; }
a) 1342
b) 3142
c) 1324
d) 3412
Q6. The diamond problem in multiple inheritance (two copies of a common base) is solved in C++ using:
a) Pure virtual functions
b) Virtual inheritance
c) Friend classes
d) Templates
Q7. With class D : private B, public members of B become ___ in D.
a) public
b) protected
c) private
d) inaccessible even inside D
Q8. Most C++ compilers implement run-time polymorphism using:
a) Function templates
b) A vtable (table of function pointers) and a hidden vptr in each object
c) Macros
d) Operator overloading
Q9. What is printed?
struct B { virtual void f() { cout << "B"; } };
struct D : B { void f() override { cout << "D"; } };
void call(B b) { b.f(); }
int main() { D d; call(d); }
a) B
b) D
c) Compilation error
d) Undefined behaviour
Q10. What does the override specifier do?
a) Makes a function virtual in the base class
b) Causes a compile error if the function does not actually override a base class virtual function
c) Hides the base class function
d) Makes the function final
Q11. A class declared as class A final { ... }; :
a) Cannot be instantiated
b) Cannot be inherited from
c) Cannot have virtual functions
d) Cannot be copied
Q12. Given template <typename T> T mx(T a, T b); what happens with the call mx(3, 4.5)?
a) Returns 4.5
b) Returns 4
c) Compilation error: conflicting deductions for T
d) Returns 3
Q13. What is the following called?
template <typename T> void show(T x) { cout << "generic"; }
template <> void show<int>(int x) { cout << "int"; }
a) Partial specialization
b) Explicit (full) specialization
c) Template overloading by return type
d) Template instantiation error
Q14. When is code for a template generated?
a) At run time, when the function is first called
b) At compile time, for each set of template arguments actually used
c) At link time only
d) Once, for all types
Q15. What is printed?
struct B {
B() { f(); }
virtual void f() { cout << "B"; }
};
struct D : B { void f() override { cout << "D"; } };
int main() { D d; }
a) B
b) D
c) BD
d) Undefined behaviour
Recommended MCQs
C++ MCQs - Classes, Objects, Constructors and References (Set 1)
Comments
Post a Comment