Sample questions
Question 1Easy
How do most C++ compilers implement virtual function calls?
- Each polymorphic object holds a hidden pointer to a per-class table of function pointers (vtable)
- Each object stores a copy of every virtual function's machine code
- The compiler generates a switch on the object's type name at every call
- The linker resolves every virtual call to the derived version at link time
Answer: A. Each polymorphic object holds a hidden pointer to a per-class table of function pointers (vtable)
Explanation
The vptr/vtable mechanism lets a call through a base pointer look up the right function at run time with one indirection.
Step-by-step solution
- A polymorphic object has a hidden pointer, the vptr, to a table of function pointers for its class.
- This table is the vtable, shared by all objects of that class.
- A virtual call reads the function address from the vtable at run time.
- So the answer is option A.
Why the other options are wrong
- B. Objects do not store copies of function code; the code is shared.
- C. Compilers do not generate a switch on type names for virtual calls.
- D. Virtual calls are chosen at run time, not resolved to one version at link time.
Tip: A vptr in each polymorphic object points to a per-class vtable; a virtual call costs one extra lookup.
Question 2Easy
What is the output?
#include <iostream>
using namespace std;
struct Animal {
void speak() { cout << "Animal"; }
};
struct Cat : Animal {
void speak() { cout << "Cat"; }
};
int main() {
Cat c;
Animal* p = &c;
p->speak();
c.speak();
return 0;
}
- AnimalAnimal
- CatAnimal
- AnimalCat
- CatCat
Answer: C. AnimalCat
Explanation
Without virtual, the call is bound at compile time using the static type: through Animal* it calls Animal::speak, through the Cat object it calls Cat::speak.
Step-by-step solution
- Speak is not virtual, so calls are bound at compile time using the static type.
- P has static type Animal*, so p->speak() calls Animal::speak and prints Animal.
- c.speak() has static type Cat, so it calls Cat::speak and prints Cat.
- The output is AnimalCat: option C.
Why the other options are wrong
- A. The call through the Cat object uses Cat::speak, so Cat is printed.
- B. The call through the Animal pointer uses Animal::speak, so Animal is printed first.
- D. The call through the Animal pointer uses Animal::speak, not Cat::speak.
Tip: Without virtual, the function called is chosen by the static type of the pointer or object.
Question 3Easy
What happens when this program is compiled?
#include <iostream>
using namespace std;
struct Shape {
virtual double area() const = 0;
virtual ~Shape() {}
};
int main() {
Shape s;
cout << "created";
return 0;
}
- It prints created
- It prints created and
area() returns 0 - Compilation error: cannot instantiate an abstract class
- Runtime error: pure virtual function call
Answer: C. Compilation error: cannot instantiate an abstract class
Explanation
A class with a pure virtual function (= 0) is abstract; objects of it cannot be created, only pointers or references to it.
Step-by-step solution
- Shape has a pure virtual function area() = 0, so Shape is abstract.
- An abstract class cannot be instantiated.
- Shape s; is an object of an abstract class, so the compiler rejects it.
- So the answer is option C: a compilation error.
Why the other options are wrong
- A. The program does not compile, so created is never printed.
- B. area() is pure virtual and cannot be called on an object that cannot be created.
- D. The error is found at compile time, so no runtime call happens.
Tip: A class with a pure virtual function cannot be instantiated; use a pointer or reference to it.
Question 4Easy
What is the output?
#include <iostream>
using namespace std;
struct Animal {
virtual void speak() { cout << "..."; }
virtual ~Animal() {}
};
struct Dog : Animal {
void speak() override { cout << "Woof"; }
};
int main() {
Dog d;
Animal* p = &d;
p->speak();
return 0;
}
- Woof
- ...
- Compilation error
- ...Woof
Answer: A. Woof
Explanation
speak is virtual, so the call through a base pointer is dispatched on the dynamic type of the object (Dog).
Step-by-step solution
- Speak is declared virtual in Animal, and Dog overrides it.
- P points to a Dog, so the call uses the dynamic type Dog.
- Dog::speak prints Woof.
- The output is Woof: option A.
Why the other options are wrong
- B. Animal::speak would print the dots only if the call were not dispatched dynamically, which it is here.
- C. The code compiles; override and virtual are valid here.
- D. Only Woof is printed, because Animal::speak is not called.
Tip: A virtual call through a base pointer runs the overriding function of the real object.
Question 5Medium
What is the output?
#include <iostream>
using namespace std;
struct Base {
virtual void show() const { cout << "Base"; }
virtual ~Base() {}
};
struct Derived : Base {
void show() const override { cout << "Derived"; }
};
int main() {
Derived d;
Base b = d;
b.show();
return 0;
}
- Derived
- Compilation error
- BaseDerived
- Base
Answer: D. Base
Explanation
Copying a Derived into a Base object slices off the derived part; b really is a Base, so Base::show runs. Polymorphism needs a pointer or reference.
Step-by-step solution
- Base b = d; copies only the Base part of d; the Derived part is sliced off.
- B is a real Base object, so b.show() calls Base::show.
- Base::show prints Base.
- The output is Base: option D.
Why the other options are wrong
- A. The object is a Base, not a Derived, so Derived::show does not run.
- B. The code compiles, because copying a Base from a Derived is allowed.
- C. Only one function runs, so Base is printed once and not followed by Derived.
Tip: Copying a derived object into a base object by value slices it; use a pointer or reference for polymorphism.
Question 6Medium
What happens when this program is compiled?
#include <iostream>
using namespace std;
struct Engine {
void start() { cout << "vroom"; }
};
struct Car : private Engine {
void go() { start(); }
};
int main() {
Car c;
c.go();
c.start();
return 0;
}
- It prints vroomvroom
- It prints vroom
- Compilation error: 'start' is a private member of Car's base
- Runtime error
Answer: C. Compilation error: 'start' is a private member of Car's base
Explanation
With private inheritance, public members of Engine become private in Car. Car's own go() can call start(), but main cannot.
Step-by-step solution
- Car inherits privately from Engine, so start becomes private inside Car.
- go() is a member of Car, so it can call start() and prints vroom.
- Main is outside Car, so c.start() is not allowed.
- The compiler rejects that call, so the answer is option C.
Why the other options are wrong
- A. The call c.start() in main is not allowed, so the code does not compile.
- B. The code does not compile, so it does not print even one vroom.
- D. This is a compile-time error, not a runtime error.
Tip: Private inheritance hides the base's public members from outside code.
Question 7Medium
What happens when this program is compiled?
#include <iostream>
using namespace std;
struct Base {
virtual void draw(int scale) { cout << scale; }
virtual ~Base() {}
};
struct Circle : Base {
void draw(double scale) override { cout << scale; }
};
int main() {
Circle c;
c.draw(2.0);
return 0;
}
- It prints 2.0
- Compilation error: draw(double) does not override anything
- It prints 2
- It compiles; draw(double) overloads draw(int)
Answer: B. Compilation error: draw(double) does not override anything
Explanation
override asks the compiler to check that the function really overrides a base virtual. The parameter type differs (double vs int), so it is an error instead of a silent new overload.
Step-by-step solution
- Base has draw(int), and Circle declares draw(double) with override.
- The parameter types differ, so draw(double) does not override any base function.
- Because override is written, the compiler reports an error instead of making an overload.
- So the answer is option B: a compile error.
Why the other options are wrong
- A. The program does not compile, so nothing is printed.
- C. The code does not compile, so nothing is printed.
- D. With override, a signature that does not match is an error, not an overload.
Tip: Use override so the compiler catches signature mismatches.
Question 8Medium
What is the output?
#include <iostream>
using namespace std;
struct Base {
virtual ~Base() { cout << "~B"; }
};
struct Derived : Base {
~Derived() override { cout << "~D"; }
};
int main() {
Base* p = new Derived;
delete p;
return 0;
}
- ~D~B
- ~B
- ~D
- ~B~D
Answer: A. ~D~B
Explanation
Because Base's destructor is virtual, deleting through a Base* runs Derived's destructor first and then Base's. Without virtual it would be undefined behaviour.
Step-by-step solution
- Base has a virtual destructor.
- delete through Base* calls Derived's destructor first.
- Then Base's destructor runs.
- Output: ~D~B.
Why the other options are wrong
- B. Derived's destructor also runs because it is virtual.
- C. Base's destructor still runs afterwards.
- D. Destruction goes derived first, then base.
Tip: Base with virtual functions -> virtual destructor.
Question 9Hard
What is the output?
#include <iostream>
using namespace std;
struct Base {
virtual void f(int x = 1) { cout << "B" << x; }
virtual ~Base() {}
};
struct Derived : Base {
void f(int x = 2) override { cout << "D" << x; }
};
int main() {
Derived d;
Base* p = &d;
p->f();
d.f();
return 0;
}
- D1D1
- B1D2
- D2D2
- D1D2
Answer: D. D1D2
Explanation
The function body is chosen at run time (Derived::f), but default arguments are taken from the static type at compile time: Base's default 1 through p, Derived's default 2 through d.
Step-by-step solution
- P->f() calls through a Base pointer, so the body is Derived::f, but the default argument comes from Base, which is 1: D1.
- d.f() calls through a Derived object, so the default argument is 2: D2.
- The output is D1D2: option D.
Why the other options are wrong
- A. The call through d uses Derived's default of 2, not 1.
- B. Base::f never runs, because the call dispatches to Derived::f.
- C. The call through p uses Base's default of 1, not 2.
Tip: Default arguments are chosen by the static type, but the function body is chosen by the real object.
Question 10Hard
What is the output?
#include <iostream>
using namespace std;
struct Base {
void f(int) { cout << "B"; }
};
struct Derived : Base {
void f(double) { cout << "D"; }
};
int main() {
Derived d;
d.f(1);
d.Base::f(1);
return 0;
}
- BD
- Compilation error
- BB
- DB
Answer: D. DB
Explanation
Declaring f in Derived hides every Base::f, so d.f(1) only sees f(double) and converts 1 to 1.0. Writing using Base::f; in Derived would bring the int overload back.
Step-by-step solution
- Derived declares f(double), which hides every Base::f, including f(int).
- d.f(1) sees only f(double), so 1 is converted to 1.0 and D is printed.
- d.Base::f(1) calls Base::f(int) explicitly, which prints B.
- The output is DB: option D.
Why the other options are wrong
- A. The first call picks f(double), so D is printed, not B.
- B. The code compiles; the name hiding makes the call valid.
- C. The first call does not reach Base::f, so the output is not BB.
Tip: A function in a derived class hides base overloads with the same name; add using Base::f to bring them back.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →