C++ Programming · Lesson 9 of 9 · Updated ·
C++ Interview Questions and Output Questions — With Explanations
Revise these before C++ interviews and written tests. Answers are short on purpose — add an example when asked.
Concepts
- C vs C++? C is procedural; C++ adds classes, references, overloading, templates, exceptions and the STL.
- Reference vs pointer? A reference must be initialised, can't be null or re-seated; a pointer can be null and re-pointed.
- struct vs class? Default access: struct public, class private. Otherwise identical.
- What is a constructor? Copy constructor? Initialises a new object; the copy constructor creates an object from another of the same type (
A(const A &other)). - Shallow vs deep copy? Shallow copies pointers (both objects share the same memory → double free); deep copy duplicates the pointed-to data.
- Virtual function? Called through a base pointer/reference, it runs the derived version at runtime (vtable).
- Pure virtual / abstract class?
virtual f() = 0;— the class can't be instantiated. - Why a virtual destructor? So deleting via a base pointer destroys the derived part too.
- Overloading vs overriding? Same name different parameters (compile time) vs same signature in a derived class with virtual (runtime).
- inline? A request to expand the function at the call site; safe replacement for macros.
- static member? Shared by all objects; static functions have no
this. - new vs malloc?
newcalls constructors and returns a typed pointer;mallocjust gives raw bytes. - Smart pointers?
unique_ptr(one owner),shared_ptr(reference counted),weak_ptr(non-owning). - vector vs array? vector grows dynamically, knows its size, works with algorithms.
- map vs unordered_map? Sorted tree O(log n) vs hash table O(1) average, no order.
Output questions (compiled and run)
1. Constructor and destructor order with inheritance
#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; }
cout << "\n";
return 0;
}
B D ~D ~B
Base is built first and destroyed last.
2. Static local variable
#include <iostream>
using namespace std;
int counter() { static int c = 0; return ++c; }
int main() {
counter(); counter();
cout << counter() << "\n";
return 0;
}
3
3. Reference changes the original
#include <iostream>
using namespace std;
int main() {
int a = 5;
int &r = a;
int b = r;
r = 20;
cout << a << " " << b << "\n";
return 0;
}
20 5
b got a copy of the value 5; r is a itself.
4. Virtual vs non-virtual
#include <iostream>
using namespace std;
struct A { virtual void f() { cout << "A::f "; } void g() { cout << "A::g "; } virtual ~A() = default; };
struct B : A { void f() override { cout << "B::f "; } void g() { cout << "B::g "; } };
int main() {
A *p = new B();
p->f();
p->g();
delete p;
cout << "\n";
return 0;
}
B::f A::g
Only f is virtual, so only f dispatches to B.
5. Integer division and promotion
#include <iostream>
using namespace std;
int main() {
int x = 7, y = 2;
double d = x / y;
double e = (double)x / y;
cout << d << " " << e << "\n";
return 0;
}
3 3.5
6. Pre/post increment in separate statements
#include <iostream>
using namespace std;
int main() {
int i = 3;
int a = i++;
int b = ++i;
cout << a << " " << b << " " << i << "\n";
return 0;
}
3 5 5
Tips
- Practise writing a small class with a constructor, destructor, operator overload and a virtual function from memory.
- Know the STL costs (O(1) vs O(log n)) — interviewers ask why you chose a container.
- For more practice: our C output questions (most apply to C++ too) and coding problems.
Common mistakes
- Confusing overloading with overriding.
- Forgetting that
classmembers are private by default. - Saying fields are polymorphic — only virtual functions are.
Quick recap
- Know references, constructors/destructors, virtual functions, abstract classes, smart pointers and the STL.
- Construction: base → derived; destruction: derived → base.
- Only virtual functions dispatch at runtime.