Sample questions
Question 1Easy
What is the output?
#include <iostream>
using namespace std;
struct Base {
Base() { cout << "B "; }
~Base() { cout << "~B "; }
};
struct Derived : Base {
Derived() { cout << "D "; }
~Derived() { cout << "~D "; }
};
int main() {
Derived d;
return 0;
}
- D B ~B ~D
- B D ~D ~B
- B D ~B ~D
- D B ~D ~B
Answer: B. B D ~D ~B
Explanation
Base parts are constructed before the derived part, and destruction runs in exactly the reverse order.
Step-by-step solution
- Creating Derived first runs the Base constructor, which prints B.
- Then the Derived constructor body runs and prints D.
- At the end of main, destruction runs in reverse: ~D first, then ~B.
- The output is B D ~D ~B: option B.
Why the other options are wrong
- A. Base is constructed before Derived, and destructors run in reverse order.
- C. Derived's destructor runs first, so ~D comes before ~B.
- D. Base is constructed first, so B comes before D.
Tip: Constructors run from base to derived; destructors run from derived to base.
Question 2Easy
What is the output?
#include <iostream>
using namespace std;
struct Shape {
int sides;
Shape(int s) : sides(s) {}
};
struct Square : Shape {
Square() : Shape(4) {}
};
int main() {
Square sq;
cout << sq.sides;
return 0;
}
- Garbage value
- Compilation error
- 4
- 0
Answer: C. 4
Explanation
The derived constructor passes 4 to the base constructor through its member initialiser list.
Step-by-step solution
- Square's constructor passes 4 to the Shape constructor in its initialiser list.
- Shape sets sides to s, so sides becomes 4.
- sq.sides is printed, giving 4.
- The answer is option C: 4.
Why the other options are wrong
- A. Sides is set by the base constructor, so it is not garbage.
- B. The code compiles, because Square calls the Shape constructor that exists.
- D. Sides is 4 after the base constructor runs, not 0.
Tip: A derived constructor must pass arguments to a base class without a default constructor through its initialiser list.
Question 3Easy
What is the output?
#include <iostream>
using namespace std;
struct A {
int id;
A(int i) : id(i) { cout << "c" << id; }
~A() { cout << "d" << id; }
};
int main() {
A x(1), y(2);
return 0;
}
- c1d1c2d2
- c1c2d1d2
- c2c1d1d2
- c1c2d2d1
Answer: D. c1c2d2d1
Explanation
Local objects are destroyed in reverse order of construction when they go out of scope.
Step-by-step solution
- x constructed: c1, then y: c2.
- At end of main locals are destroyed in reverse.
- y first: d2, then x: d1.
- Output: c1c2d2d1.
Why the other options are wrong
- A. Destruction is not interleaved with construction.
- B. Destruction order is reverse, so d2 first.
- C. Construction order is x then y, so c1 first.
Tip: Last in, first out.
Question 4Easy
Which of these cannot be declared virtual in C++?
- A const member function
- A destructor
- A constructor
- A member function that returns a reference
Answer: C. A constructor
Explanation
The constructor runs before the object's dynamic type is established, so it cannot be virtual. Destructors can, and in polymorphic bases should, be virtual.
Step-by-step solution
- A virtual function is looked up through the object's vtable, which is set up during construction.
- A constructor runs before the object's dynamic type exists, so it cannot be virtual.
- Destructors, const member functions and functions returning references can all be virtual.
- So the one that cannot be virtual is option C: a constructor.
Why the other options are wrong
- A. A const member function can be declared virtual.
- B. A destructor can and often should be virtual in a base class.
- D. A member function that returns a reference can be declared virtual.
Tip: Constructors cannot be virtual; destructors often should be in base classes.
Question 5Medium
Which members MUST be initialised in a constructor's member initialiser list (when they have no default member initialiser)?
- Members of built-in types such as int and double
- const data members and reference members
- All pointer members
- static data members and static const members
Answer: B. const data members and reference members
Explanation
const and reference members cannot be assigned after construction, so they must be initialised in the initialiser list (or with a default member initialiser).
Step-by-step solution
- Const members and reference members cannot be assigned after the object is built.
- So they must be set in the member initialiser list, unless they have a default member initialiser.
- Built-in types, pointers and static members do not have this rule.
- So the answer is option B.
Why the other options are wrong
- A. An int or double member can be assigned in the constructor body, so it need not be in the list.
- C. A pointer member can be assigned in the constructor body, so it need not be in the list.
- D. Static data members belong to the class, not to each object, so they are not set in the list.
Tip: Use the initialiser list for const and reference members.
Question 6Medium
What is the output?
#include <iostream>
using namespace std;
struct M1 { M1() { cout << "1"; } };
struct M2 { M2() { cout << "2"; } };
struct Host {
M1 a;
M2 b;
Host() : b(), a() { cout << "H"; }
};
int main() {
Host h;
return 0;
}
- 12H
- 21H
- H12
- H21
Answer: A. 12H
Explanation
Members are always initialised in the order they are declared in the class (a then b), regardless of the order in the member initialiser list.
Step-by-step solution
- Members are initialised in declaration order, not initialiser-list order.
- a (M1) is declared first, then b (M2).
- Prints 1 then 2, then the body prints H.
- Output: 12H.
Why the other options are wrong
- B. That would follow the initialiser list order, which is ignored.
- C. Members are built before the constructor body runs.
- D. Body output always comes after member initialisation.
Tip: Declaration order wins.
Question 7Medium
What happens when this program is compiled?
#include <iostream>
using namespace std;
struct Meter {
int v;
explicit Meter(int x) : v(x) {}
};
int main() {
Meter m = 5;
cout << m.v;
return 0;
}
- It prints 5
- It prints 0
- It compiles with a narrowing warning
- Compilation error: the explicit constructor cannot be used for copy-initialisation
Answer: D. Compilation error: the explicit constructor cannot be used for copy-initialisation
Explanation
explicit forbids implicit conversions, including the copy-initialisation form T x = value. Meter m(5); or Meter m{5}; would compile.
Step-by-step solution
- Meter has an explicit constructor that takes an int.
- Meter m = 5; is copy-initialisation, which needs an implicit conversion.
- Explicit forbids that conversion, so the code does not compile.
- The answer is option D.
Why the other options are wrong
- A. The code does not compile, so it cannot print 5.
- B. The code does not compile, so nothing is printed.
- C. Explicit makes this an error, not a warning, and no narrowing is involved.
Tip: Explicit stops implicit conversions; write Meter m(5) or Meter m{5} instead.
Question 8Medium
What is the output?
#include <iostream>
using namespace std;
struct Timer {
Timer() : Timer(5) { cout << "D"; }
Timer(int s) { cout << s; }
};
int main() {
Timer t;
return 0;
}
- 5
- D5
- 5D
- D
Answer: C. 5D
Explanation
A delegating constructor first runs the target constructor completely (printing 5) and then executes its own body (printing D).
Step-by-step solution
- Timer() delegates to Timer(5) in its initialiser list.
- The target constructor runs first and prints 5.
- Then the body of the delegating constructor runs and prints D.
- The output is 5D: option C.
Why the other options are wrong
- A. The D from the delegating constructor's body is also printed.
- B. The order is reversed: the target constructor's 5 prints before D.
- D. The 5 from Timer(5) is also printed.
Tip: A delegating constructor finishes the target constructor first, then runs its own body.
Question 9Hard
What is the output?
#include <iostream>
using namespace std;
struct A {
A() { cout << "C"; }
};
int main() {
A a();
cout << "X";
return 0;
}
- X
- XC
- Compilation error
- CX
Answer: A. X
Explanation
A a(); is parsed as a declaration of a function named a returning A (the "most vexing parse"), so no object is constructed. Use A a; or A a{}; instead.
Step-by-step solution
- A a(); looks like an object but is a function declaration.
- No object is created, so C never prints.
- Only X prints.
Why the other options are wrong
- B. No A is constructed so there is no C.
- C. It compiles as a function declaration.
- D. No constructor call occurs.
Tip: Use A a; or A a{};
Question 10Hard
What is the output?
#include <iostream>
using namespace std;
struct Member {
~Member() { cout << "m"; }
};
struct A {
Member m;
A() { throw 1; }
~A() { cout << "a"; }
};
int main() {
try {
A obj;
} catch (int) {
cout << "c";
}
return 0;
}
- mc
- amc
- c
- mac
Answer: A. mc
Explanation
If a constructor throws, the object never finished construction, so ~A() is not called; members that were already fully constructed (m) are destroyed before the handler runs.
Step-by-step solution
- A obj; starts constructing: its member m is constructed first.
- A constructor body throws 1, so the constructor of A never finishes.
- The destructor of A is not called, but the fully built member m is destroyed, printing m.
- The handler catches the int and prints c, so the output is mc: option A.
Why the other options are wrong
- B. ~A is not run because the constructor did not finish, so a is not printed.
- C. The member m was fully constructed, so its destructor runs and prints m.
- D. ~A is never run, so a is not printed, and m comes before c.
Tip: If a constructor throws, the destructor of that class is not run, but fully built members are destroyed.
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 →