Skills101

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.

virtual functions: a base pointer calls the real object's override via the vtable — one of the most asked C++ topics

Concepts

  1. C vs C++? C is procedural; C++ adds classes, references, overloading, templates, exceptions and the STL.
  2. Reference vs pointer? A reference must be initialised, can't be null or re-seated; a pointer can be null and re-pointed.
  3. struct vs class? Default access: struct public, class private. Otherwise identical.
  4. 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)).
  5. Shallow vs deep copy? Shallow copies pointers (both objects share the same memory → double free); deep copy duplicates the pointed-to data.
  6. Virtual function? Called through a base pointer/reference, it runs the derived version at runtime (vtable).
  7. Pure virtual / abstract class? virtual f() = 0; — the class can't be instantiated.
  8. Why a virtual destructor? So deleting via a base pointer destroys the derived part too.
  9. Overloading vs overriding? Same name different parameters (compile time) vs same signature in a derived class with virtual (runtime).
  10. inline? A request to expand the function at the call site; safe replacement for macros.
  11. static member? Shared by all objects; static functions have no this.
  12. new vs malloc? new calls constructors and returns a typed pointer; malloc just gives raw bytes.
  13. Smart pointers? unique_ptr (one owner), shared_ptr (reference counted), weak_ptr (non-owning).
  14. vector vs array? vector grows dynamically, knows its size, works with algorithms.
  15. 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 class members 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.

Practice what you learned

📝 Quiz: C++ Interview Questions5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →