C++ Interview Questions and Answers (OOP, STL, Pointers)

C++ questions on OOP, the STL and modern C++ that product companies and competitive-programming-minded interviewers love, each with a short explanation.

  1. Q1. Which STL container keeps unique elements in sorted order?

    • A. stack
    • B. set
    • C. vector
    • D. unordered_map
    Show answer

    Answer: B. set
    std::set stores unique keys sorted; unordered_set is unsorted but faster on average.

  2. Q2. What is the main difference between a class and a struct in C++?

    • A. Structs can't have methods
    • B. Classes can't have constructors
    • C. Default access: private for class, public for struct
    • D. There is no difference at all
    Show answer

    Answer: C. Default access: private for class, public for struct
    Otherwise they are the same; both can have methods, constructors and inheritance.

  3. Q3. What is a reference in C++?

    • A. A copy of a variable
    • B. A type of loop
    • C. Another name (alias) for an existing variable
    • D. A pointer that can be null
    Show answer

    Answer: C. Another name (alias) for an existing variable
    int& r = x; makes r refer to x; it must be initialised and can't be reseated.

  4. Q4. Which keyword enables runtime polymorphism in C++?

    • A. inline
    • B. virtual
    • C. friend
    • D. static
    Show answer

    Answer: B. virtual
    virtual functions are resolved through the vtable at run time.

  5. Q5. What does a pure virtual function (= 0) make a class?

    • A. Final
    • B. Template
    • C. Abstract
    • D. Static
    Show answer

    Answer: C. Abstract
    An abstract class can't be instantiated; derived classes must override the function.

  6. Q6. Which operator is used to free memory allocated with new[]?

    • A. delete[]
    • B. delete
    • C. free
    • D. remove
    Show answer

    Answer: A. delete[]
    Pair new with delete and new[] with delete[].

  7. Q7. What does RAII stand for?

    • A. Random Access Is Immediate
    • B. Runtime Allocation In Instances
    • C. Reference And Inheritance Interface
    • D. Resource Acquisition Is Initialization
    Show answer

    Answer: D. Resource Acquisition Is Initialization
    Resources are tied to object lifetime and released in the destructor.

  8. Q8. Which smart pointer allows only one owner of an object?

    • A. shared_ptr
    • B. auto_ptr
    • C. unique_ptr
    • D. weak_ptr
    Show answer

    Answer: C. unique_ptr
    unique_ptr can be moved but not copied; shared_ptr counts owners.

  9. Q9. What is function overloading?

    • A. Redefining a base class function
    • B. Several functions with the same name but different parameters
    • C. A function inside a class
    • D. A function calling itself
    Show answer

    Answer: B. Several functions with the same name but different parameters
    The compiler picks the right version from the argument types.

  10. Q10. What is the time complexity of push_back on a vector (amortised)?

    • A. O(1)
    • B. O(n)
    • C. O(n²)
    • D. O(log n)
    Show answer

    Answer: A. O(1)
    Occasionally the vector grows and copies, but on average push_back is constant time.

  11. Q11. When is a destructor called?

    • A. When an object is created
    • B. When an object goes out of scope or is deleted
    • C. When a method returns a value
    • D. Only when the program ends
    Show answer

    Answer: B. When an object goes out of scope or is deleted
    Stack objects are destroyed at the end of their block; heap objects on delete.

  12. Q12. Why should a base class with virtual functions have a virtual destructor?

    • A. To make the class abstract
    • B. To allow copying
    • C. It is required for templates
    • D. So deleting through a base pointer also destroys the derived part
    Show answer

    Answer: D. So deleting through a base pointer also destroys the derived part
    Without it, delete basePtr only runs the base destructor (undefined behaviour).

  13. Q13. What does the const after a member function, like int get() const, mean?

    • A. The function can't be overridden
    • B. The function returns a constant
    • C. The function doesn't modify the object
    • D. The function is static
    Show answer

    Answer: C. The function doesn't modify the object
    Const member functions can be called on const objects.

  14. Q14. Which header provides sort(), reverse() and max_element()?

    • A. <numeric>
    • B. <algorithm>
    • C. <vector>
    • D. <utility>
    Show answer

    Answer: B. <algorithm>
    accumulate lives in <numeric>.

  15. Q15. What is a template in C++?

    • A. A comment block
    • B. A pre-written class from the STL only
    • C. A way to write code that works with any type
    • D. A design pattern
    Show answer

    Answer: C. A way to write code that works with any type
    template <typename T> T max(T a, T b) works for ints, doubles, strings and more.

  16. Q16. Which container gives the largest element first?

    • A. queue
    • B. priority_queue
    • C. deque
    • D. stack
    Show answer

    Answer: B. priority_queue
    std::priority_queue is a max-heap by default.

  17. Q17. What does the 'auto' keyword do?

    • A. Makes a variable global
    • B. Lets the compiler deduce the variable's type
    • C. Creates a constant
    • D. Allocates on the heap
    Show answer

    Answer: B. Lets the compiler deduce the variable's type
    auto x = 3.5; makes x a double.

  18. Q18. Which access specifier lets derived classes use a member but hides it from other code?

    • A. protected
    • B. private
    • C. friend
    • D. public
    Show answer

    Answer: A. protected
    protected members are visible in the class and its subclasses.

  19. Q19. What happens when you read m["key"] from a std::map and the key is missing?

    • A. It returns null
    • B. Compile error
    • C. An exception is thrown
    • D. The key is inserted with a default value
    Show answer

    Answer: D. The key is inserted with a default value
    Use find() or count() to check without inserting.

  20. Q20. What is the difference between a pointer and a reference?

    • A. A pointer can be null and re-pointed; a reference can't
    • B. References use more memory
    • C. Pointers can't point to objects
    • D. There is no difference
    Show answer

    Answer: A. A pointer can be null and re-pointed; a reference can't
    References must be bound when created and always refer to the same object.

  21. Q21. What does this code print?

    #include <iostream>
    using namespace std;
    
    void set(int& v) { v = 20; }
    
    int main() {
        int x = 10;
        set(x);
        cout << x << endl;
        return 0;
    }
    • A. 10
    • B. 0
    • C. Error
    • D. 20
    Show answer

    Answer: D. 20
    The parameter is a reference, so the function changes x itself.

  22. Q22. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct Animal { virtual void speak() { cout << "Animal" << endl; } virtual ~Animal() = default; };
    struct Dog : Animal { void speak() override { cout << "Dog" << endl; } };
    
    int main() {
        Animal* a = new Dog();
        a->speak();
        delete a;
        return 0;
    }
    • A. Dog
    • B. AnimalDog
    • C. Error
    • D. Animal
    Show answer

    Answer: A. Dog
    speak() is virtual, so the Dog version runs through the base pointer.

  23. Q23. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct Animal { void speak() { cout << "Animal" << endl; } };
    struct Dog : Animal { void speak() { cout << "Dog" << endl; } };
    
    int main() {
        Dog d;
        Animal* a = &d;
        a->speak();
        return 0;
    }
    • A. Error
    • B. Animal
    • C. AnimalDog
    • D. Dog
    Show answer

    Answer: B. Animal
    Without virtual, the call is decided by the pointer's type (Animal) at compile time.

  24. Q24. What does this code print?

    set<int> s = {1, 2, 2, 3, 3};
    cout << s.size() << endl;
    • A. 3
    • B. 4
    • C. 5
    • D. 2
    Show answer

    Answer: A. 3
    A set ignores duplicates: {1, 2, 3}.

  25. Q25. What does this code print?

    vector<int> v = {5, 2, 9, 1};
    sort(v.begin(), v.end());
    for (size_t i = 0; i < v.size(); i++) cout << v[i] << (i + 1 < v.size() ? " " : "\n");
    • A. 1 2 5 9
    • B. 1 5 2 9
    • C. 9 5 2 1
    • D. 5 2 9 1
    Show answer

    Answer: A. 1 2 5 9
    sort arranges the vector in ascending order.

  26. Q26. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct T {
        T() { cout << "Construct "; }
        ~T() { cout << "Destruct" << endl; }
    };
    
    int main() {
        { T t; }
        return 0;
    }
    • A. Nothing
    • B. Destruct Construct
    • C. Construct Destruct
    • D. Construct
    Show answer

    Answer: C. Construct Destruct
    The object is created inside the block and destroyed when the block ends.

  27. Q27. What does this code print?

    stack<int> st;
    st.push(1); st.push(2); st.push(3);
    while (!st.empty()) { cout << st.top() << (st.size() > 1 ? " " : "\n"); st.pop(); }
    • A. 1 2 3
    • B. 3 1 2
    • C. 1 3 2
    • D. 3 2 1
    Show answer

    Answer: D. 3 2 1
    A stack is last-in, first-out.

  28. Q28. What does this code print?

    priority_queue<int> pq;
    for (int x : {4, 9, 1}) pq.push(x);
    cout << pq.top() << endl;
    • A. Error
    • B. 1
    • C. 4
    • D. 9
    Show answer

    Answer: D. 9
    priority_queue keeps the largest element on top.

  29. Q29. What does this code print?

    map<string, int> m;
    m["a"] = 1;
    int x = m["b"];
    cout << m.size() << endl;
    • A. 3
    • B. 1
    • C. 0
    • D. 2
    Show answer

    Answer: D. 2
    m["b"] doesn't exist yet, so reading it inserts "b" with 0, making 2 keys.

  30. Q30. What does this code print?

    #include <iostream>
    using namespace std;
    
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
    
    int main() {
        cout << add(3, 4) << " " << add(1.5, 2.0) << endl;
        return 0;
    }
    • A. 3.5 7
    • B. 7 3
    • C. 7 3.5
    • D. Error
    Show answer

    Answer: C. 7 3.5
    Overloading picks add(int, int) for ints and add(double, double) for doubles.

  31. Q31. What does this code print?

    string s = "Skill Park";
    cout << s.substr(0, 5) << endl;
    • A. Skill Park
    • B. ark
    • C. Skill
    • D. Park
    Show answer

    Answer: C. Skill
    substr(0, 5) takes 5 characters starting at index 0.

  32. Q32. What does this code print?

    vector<int> a = {10};
    vector<int> b = a;
    b[0] = 15;
    cout << a[0] << " " << b[0] << endl;
    • A. 15 10
    • B. 15 15
    • C. 10 15
    • D. 10 10
    Show answer

    Answer: C. 10 15
    b is a copy of a, so changing b doesn't change a.

  33. Q33. What does this code print?

    vector<int> a = {10};
    vector<int>& r = a;
    r[0] = 15;
    cout << a[0] << " " << r[0] << endl;
    • A. 15 10
    • B. 10 10
    • C. 10 15
    • D. 15 15
    Show answer

    Answer: D. 15 15
    r is a reference to the vector a, so the change is visible through both names.

  34. Q34. What does this code print?

    auto a = make_shared<int>(5);
    auto b = a;
    cout << a.use_count() << endl;
    • A. 3
    • B. 1
    • C. 0
    • D. 2
    Show answer

    Answer: D. 2
    Two shared_ptrs own the same object.

  35. Q35. What does this code print?

    try {
        throw runtime_error("bad");
    } catch (const exception& e) {
        cout << "Caught: " << e.what() << endl;
    }
    • A. Caught: bad
    • B. bad
    • C. Crash
    • D. Nothing
    Show answer

    Answer: A. Caught: bad
    The thrown runtime_error is caught by the matching catch block.

  36. Q36. What does this code print?

    int k = 4;
    auto mul = [k](int x) { return x * k; };
    cout << mul(3) << endl;
    • A. 12
    • B. Error
    • C. 34
    • D. 7
    Show answer

    Answer: A. 12
    The lambda captures k = 4 by value and multiplies: 3 × 4 = 12.

  37. Q37. What does this code print?

    vector<int> v = {1, 2, 3, 4, 5};
    cout << accumulate(v.begin(), v.end(), 0) << endl;
    • A. 10
    • B. Error
    • C. 5
    • D. 15
    Show answer

    Answer: D. 15
    accumulate adds every element to the starting value 0.

  38. Q38. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct Base { Base() { cout << "Base "; } };
    struct Derived : Base { Derived() { cout << "Derived" << endl; } };
    
    int main() {
        Derived d;
        return 0;
    }
    • A. Base Derived
    • B. Base
    • C. Derived
    • D. Derived Base
    Show answer

    Answer: A. Base Derived
    The base class part is always constructed before the derived part.

Practised these? Now prove it.

Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.

Start a mock test