🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: C++ Interview Questions: all 10 questions

Quiz: C++ Interview Questions · Question 9 of 10

What does this print? (code)

What does this print?

#include <iostream>
using namespace std;
struct A { void show() { cout << "A"; } virtual void vshow() { cout << "a"; } virtual ~A() {} };
struct B : A { void show() { cout << "B"; } void vshow() override { cout << "b"; } };
int main() {
    A *p = new B();
    p->show();
    p->vshow();
    cout << "\n";
    delete p;
    return 0;
}
  1. A.Bb
  2. B.Ab
  3. C.Aa
  4. D.Ba
Answer & explanation

Correct answer: Ab

show is not virtual, so the pointer type (A) decides; vshow is virtual, so the real object (B) decides.

Can you get the rest right without peeking?

Take the timed Quiz: C++ Interview Questions quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →