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.

  39. Q39. What is the difference between new and malloc?

    • A. They are identical
    • B. new calls constructors and is type-safe; malloc only allocates raw memory
    • C. malloc calls constructors
    • D. new allocates on the stack
    Show answer

    Answer: B. new calls constructors and is type-safe; malloc only allocates raw memory
    Pair new with delete, malloc with free.

  40. Q40. What is a namespace?

    • A. A type of class
    • B. A named scope that prevents name clashes
    • C. A file
    • D. A memory region
    Show answer

    Answer: B. A named scope that prevents name clashes
    std is the standard library's namespace.

  41. Q41. What is an inline function?

    • A. A virtual function
    • B. A function defined in one line
    • C. A function the compiler may expand at the call site
    • D. A function without parameters
    Show answer

    Answer: C. A function the compiler may expand at the call site
    Avoids call overhead for small functions.

  42. Q42. What is the 'this' pointer?

    • A. A pointer to the object a member function was called on
    • B. A global pointer
    • C. A pointer to the class
    • D. A pointer to main()
    Show answer

    Answer: A. A pointer to the object a member function was called on
    Used to tell members apart from parameters.

  43. Q43. What is a friend function?

    • A. A public member function
    • B. A non-member function allowed to access private members
    • C. A function in a friend class only
    • D. A virtual function
    Show answer

    Answer: B. A non-member function allowed to access private members
    Declared with the friend keyword inside the class.

  44. Q44. What is the rule of three?

    • A. If a class needs a destructor, copy constructor or copy assignment, it likely needs all three
    • B. A class can inherit from at most three classes
    • C. Every class needs three constructors
    • D. Use at most three pointers
    Show answer

    Answer: A. If a class needs a destructor, copy constructor or copy assignment, it likely needs all three
    The rule of five adds move constructor and move assignment.

  45. Q45. What is move semantics?

    • A. Moving files on disk
    • B. Moving a function between classes
    • C. Changing the order of members
    • D. Transferring resources from a temporary object instead of copying them
    Show answer

    Answer: D. Transferring resources from a temporary object instead of copying them
    std::move and && (rvalue references), since C++11.

  46. Q46. What is a lambda capture [&]?

    • A. Capture all by value
    • B. Capture nothing
    • C. Capture all used variables by reference
    • D. Capture only this
    Show answer

    Answer: C. Capture all used variables by reference
    [=] captures by value.

  47. Q47. What is the difference between std::map and std::unordered_map?

    • A. map is sorted (tree, O(log n)); unordered_map is hashed (average O(1))
    • B. They are identical
    • C. map doesn't allow string keys
    • D. unordered_map is sorted
    Show answer

    Answer: A. map is sorted (tree, O(log n)); unordered_map is hashed (average O(1))
    Pick unordered_map unless you need order.

  48. Q48. What does std::vector::reserve do?

    • A. Sorts the vector
    • B. Adds elements
    • C. Shrinks the vector
    • D. Allocates capacity in advance without changing size
    Show answer

    Answer: D. Allocates capacity in advance without changing size
    Avoids repeated reallocations.

  49. Q49. What is iterator invalidation?

    • A. Iterators becoming unusable after the container changes, e.g. vector reallocation
    • B. An iterator reaching end()
    • C. An iterator pointing to the first element
    • D. A compile error
    Show answer

    Answer: A. Iterators becoming unusable after the container changes, e.g. vector reallocation
    Erasing or inserting into a vector can invalidate iterators.

  50. Q50. What is an abstract class in C++?

    • A. A template class
    • B. A class with only static members
    • C. A class with at least one pure virtual function
    • D. A class with no members
    Show answer

    Answer: C. A class with at least one pure virtual function
    virtual void draw() = 0;

  51. Q51. What does the override keyword do?

    • A. Hides a base function
    • B. Makes a function static
    • C. Makes the compiler check that the function really overrides a base virtual function
    • D. Prevents overriding
    Show answer

    Answer: C. Makes the compiler check that the function really overrides a base virtual function
    Catches signature mistakes.

  52. Q52. What does the final keyword do on a virtual function?

    • A. Prevents further overriding in derived classes
    • B. Makes it static
    • C. Makes it inline
    • D. Makes it pure virtual
    Show answer

    Answer: A. Prevents further overriding in derived classes
    On a class, it prevents inheritance.

  53. Q53. What is a const reference parameter (const T&) good for?

    • A. Passing large objects without copying and without allowing changes
    • B. Making the object global
    • C. Passing by value
    • D. Allowing the function to change the object
    Show answer

    Answer: A. Passing large objects without copying and without allowing changes
    Common for strings and vectors.

  54. Q54. What is the STL?

    • A. A string library only
    • B. A graphics library
    • C. The Standard Template Library of containers, iterators and algorithms
    • D. A testing framework
    Show answer

    Answer: C. The Standard Template Library of containers, iterators and algorithms
    vector, map, sort, find…

  55. Q55. What is a deque?

    • A. A stack only
    • B. A hash table
    • C. A sorted list
    • D. A double-ended queue with fast insert/remove at both ends
    Show answer

    Answer: D. A double-ended queue with fast insert/remove at both ends
    std::deque supports random access too.

  56. Q56. What does std::pair hold?

    • A. A key and a hash
    • B. Two values, accessed as first and second
    • C. An array of two strings
    • D. Two pointers only
    Show answer

    Answer: B. Two values, accessed as first and second
    make_pair(1, "a").

  57. Q57. What is the purpose of std::sort's third argument?

    • A. The starting index
    • B. The number of elements
    • C. The sorting algorithm name
    • D. A custom comparison function or lambda
    Show answer

    Answer: D. A custom comparison function or lambda
    sort(v.begin(), v.end(), greater<int>()).

  58. Q58. What is undefined behaviour?

    • A. A runtime exception
    • B. Code whose result the C++ standard doesn't specify, like reading out of bounds
    • C. A warning from the compiler
    • D. Code that doesn't compile
    Show answer

    Answer: B. Code whose result the C++ standard doesn't specify, like reading out of bounds
    Anything may happen; avoid it.

  59. Q59. What does nullptr replace?

    • A. NULL / 0 as a null pointer
    • B. void
    • C. delete
    • D. this
    Show answer

    Answer: A. NULL / 0 as a null pointer
    nullptr has its own type and avoids overload ambiguities.

  60. Q60. What is a static member variable?

    • A. A private variable
    • B. A constant
    • C. A variable shared by all objects of the class
    • D. A variable on the stack
    Show answer

    Answer: C. A variable shared by all objects of the class
    Defined once outside the class (before C++17 inline).

  61. Q61. What happens if an exception is thrown and not caught?

    • A. The exception is ignored
    • B. std::terminate is called and the program ends
    • C. The program restarts
    • D. It returns 0
    Show answer

    Answer: B. std::terminate is called and the program ends
    Catch exceptions you can handle.

  62. Q62. What is a template specialization?

    • A. An abstract template
    • B. A virtual template
    • C. A template with no parameters
    • D. A custom implementation of a template for a specific type
    Show answer

    Answer: D. A custom implementation of a template for a specific type
    template<> class Box<bool> { … };

  63. Q63. What is the difference between struct and class inheritance by default?

    • A. struct inherits publicly by default; class inherits privately
    • B. They are the same
    • C. class inherits publicly
    • D. structs can't inherit
    Show answer

    Answer: A. struct inherits publicly by default; class inherits privately
    Write public explicitly to be clear.

  64. Q64. What is object slicing?

    • A. Using pointers to members
    • B. Copying a derived object into a base object, losing the derived part
    • C. Deleting part of an object
    • D. Splitting an object into two
    Show answer

    Answer: B. Copying a derived object into a base object, losing the derived part
    Use references or pointers to avoid it.

  65. Q65. Which smart pointer breaks reference cycles with shared_ptr?

    • A. auto_ptr
    • B. raw pointer
    • C. weak_ptr
    • D. unique_ptr
    Show answer

    Answer: C. weak_ptr
    weak_ptr observes without owning.

  66. Q66. What is constexpr?

    • A. A runtime constant only
    • B. An exception type
    • C. A constant pointer
    • D. A value or function that can be evaluated at compile time
    Show answer

    Answer: D. A value or function that can be evaluated at compile time
    constexpr int square(int x) { return x * x; }

  67. Q67. What does std::string::npos represent?

    • A. 'Not found' or 'until the end' (the largest size_t value)
    • B. Position 0
    • C. The string's length
    • D. A null character
    Show answer

    Answer: A. 'Not found' or 'until the end' (the largest size_t value)
    find() returns npos when nothing is found.

  68. Q68. Why use emplace_back instead of push_back?

    • A. It is the only way to add objects
    • B. It sorts the vector
    • C. It constructs the element in place, avoiding a temporary copy
    • D. It inserts at the front
    Show answer

    Answer: C. It constructs the element in place, avoiding a temporary copy
    v.emplace_back("Asha", 21) for a vector of structs.

  69. Q69. What does this code print?

    queue<int> q;
    for (int x : {1, 2, 3}) q.push(x);
    for (int i = 0; i < 3; i++) { cout << q.front() << (i < 2 ? " " : "\n"); q.pop(); }
    • A. Error
    • B. 2 1 3
    • C. 1 2 3
    • D. 3 2 1
    Show answer

    Answer: C. 1 2 3
    A queue returns items in insertion order.

  70. Q70. What does this code print?

    map<string, int> m = {{"b", 2}, {"a", 1}};
    string out;
    for (auto& [k, v] : m) out += k + to_string(v) + " ";
    out.pop_back();
    cout << out << endl;
    • A. a1 b2
    • B. b2 a1
    • C. 1a 2b
    • D. Error
    Show answer

    Answer: A. a1 b2
    A map iterates in sorted key order.

  71. Q71. What does this code print?

    vector<int> v;
    v.reserve(100);
    for (int i = 0; i < 5; i++) v.push_back(i);
    cout << v.size() << endl;
    • A. 3
    • B. 6
    • C. 5
    • D. 4
    Show answer

    Answer: C. 5
    reserve changes capacity, not size; 5 elements were added.

  72. Q72. What does this code print?

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

    Answer: A. 3
    count returns how many times 2 appears.

  73. Q73. What does this code print?

    vector<int> v = {4, 9, 1, 5};
    cout << *max_element(v.begin(), v.end()) << endl;
    • A. 9
    • B. 1
    • C. 5
    • D. Error
    Show answer

    Answer: A. 9
    max_element returns an iterator to the largest value.

  74. Q74. What does this code print?

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

    Answer: A. 4 3 2 1
    sort with greater<int>() sorts descending.

  75. Q75. What does this code print?

    string s = "hello";
    reverse(s.begin(), s.end());
    cout << s << endl;
    • A. hello
    • B. olleh
    • C. h
    • D. Error
    Show answer

    Answer: B. olleh
    reverse works on strings too.

  76. Q76. What does this code print?

    string s = "hello";
    cout << s.find('l') << endl;
    • A. Error
    • B. 1
    • C. 2
    • D. -1
    Show answer

    Answer: C. 2
    find returns the index of the first 'l'.

  77. Q77. What does this code print?

    string s = "hello";
    cout << (s.find("xyz") == string::npos ? "Not found" : "Found") << endl;
    • A. Error
    • B. Found
    • C. 0
    • D. Not found
    Show answer

    Answer: D. Not found
    find returns string::npos when the text isn't present.

  78. Q78. What does this code print?

    pair<int, int> p = {3, 1};
    cout << p.first << " " << p.second << endl;
    • A. 1 1
    • B. 3 1
    • C. 1 3
    • D. 3 3
    Show answer

    Answer: B. 3 1
    pair members are first and second.

  79. Q79. What does this code print?

    int x = 5;
    auto get = [&x]() { return x; };
    x = 10;
    cout << get() << endl;
    • A. 5
    • B. Error
    • C. 20
    • D. 10
    Show answer

    Answer: D. 10
    The lambda captures x by reference, so it sees the updated value.

  80. Q80. What does this code print?

    int x = 5;
    auto get = [x]() { return x; };
    x = 10;
    cout << get() << endl;
    • A. Error
    • B. 0
    • C. 10
    • D. 5
    Show answer

    Answer: D. 5
    Capture by value copies x when the lambda is created.

  81. Q81. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct Shape { virtual string draw() const { return "Shape"; } virtual ~Shape() = default; };
    struct Circle : Shape { string draw() const override { return "Circle"; } };
    
    void show(const Shape& s) { cout << s.draw() << endl; }
    
    int main() {
        Circle c;
        show(c);
        return 0;
    }
    • A. Circle
    • B. Shape
    • C. Nothing
    • D. Error
    Show answer

    Answer: A. Circle
    draw is virtual, so the Circle version runs through the reference.

  82. Q82. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct Shape { virtual string draw() const { return "Shape"; } virtual ~Shape() = default; };
    struct Circle : Shape { string draw() const override { return "Circle"; } };
    
    void show(Shape s) { cout << s.draw() << endl; }
    
    int main() {
        Circle c;
        show(c);
        return 0;
    }
    • A. Error
    • B. Nothing
    • C. Shape
    • D. Circle
    Show answer

    Answer: C. Shape
    Passing by value slices the Circle into a Shape (object slicing).

  83. Q83. What does this code print?

    auto a = make_unique<int>(7);
    auto b = move(a);
    cout << (a == nullptr) << endl;
    • A. 0
    • B. 1
    • C. Error
    • D. 2
    Show answer

    Answer: B. 1
    unique_ptr has a single owner; moving transfers it, leaving a empty.

  84. Q84. What does this code print?

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

    Answer: D. Derived Base
    Destructors run in reverse order of construction: derived first.

  85. Q85. What does this code print?

    #include <iostream>
    using namespace std;
    
    struct C {
        static int n;
        C() { n++; }
    };
    int C::n = 0;
    
    int main() {
        C a, b, c;
        cout << C::n << endl;
        return 0;
    }
    • A. 3
    • B. Error
    • C. 1
    • D. 2
    Show answer

    Answer: A. 3
    Each object increments the shared static counter.

  86. Q86. What does this code print?

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

    Answer: D. 7
    The template is instantiated for int: 3 + 4.

  87. Q87. What does this code print?

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

    Answer: B. 2 3
    set::insert ignores duplicates; size goes 2 → 2 → 3.

  88. Q88. What does this code print?

    #include <iostream>
    using namespace std;
    
    void say(string s = "Default") { cout << s << endl; }
    
    int main() {
        say();
        return 0;
    }
    • A. Default
    • B. Value
    • C. Error
    • D. 0
    Show answer

    Answer: A. Default
    The default argument is used when none is passed.

  89. Q89. What does this code print?

    int x = 10;
    int& r = x;
    r += 5;
    cout << x << endl;
    • A. 10
    • B. Error
    • C. 15
    • D. 5
    Show answer

    Answer: C. 15
    The reference r is another name for x, so x becomes 15.

  90. Q90. What does this code print?

    vector<int> v = {1, 2, 3}, out(3);
    transform(v.begin(), v.end(), out.begin(), [](int x) { return x * x; });
    cout << out[0] << " " << out[1] << " " << out[2] << endl;
    • A. 2 4 6
    • B. 1 2 3
    • C. Error
    • D. 1 4 9
    Show answer

    Answer: D. 1 4 9
    transform squares every element into the output vector.

  91. Q91. What does this code print?

    vector<int> v = {1, 2, 3, 4, 5};
    cout << count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) << endl;
    • A. 1
    • B. 2
    • C. Error
    • D. 3
    Show answer

    Answer: B. 2
    count_if counts the even numbers 2 and 4.

  92. Q92. What does this code print?

    string a = "a", b = "b";
    cout << a + b << endl;
    • A. Error
    • B. ba
    • C. ab
    • D. a
    Show answer

    Answer: C. ab
    string + concatenates.

  93. Q93. What does this code print?

    vector<int> v = {1, 3, 5, 7, 9};
    cout << lower_bound(v.begin(), v.end(), 8) - v.begin() << endl;
    • A. 4
    • B. 5
    • C. 3
    • D. Error
    Show answer

    Answer: A. 4
    lower_bound returns the first element not less than 8, which is 9 at index 4.

  94. Q94. What does this code print?

    priority_queue<int, vector<int>, greater<int>> pq;
    for (int x : {3, 1, 2}) pq.push(x);
    for (int i = 0; i < 3; i++) { cout << pq.top() << (i < 2 ? " " : "\n"); pq.pop(); }
    • A. 1 3 2
    • B. 1 2 3
    • C. Error
    • D. 3 2 1
    Show answer

    Answer: B. 1 2 3
    A min-heap via greater<int> pops the smallest first.

  95. Q95. What does this code print?

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

    Answer: B. 6
    accumulate adds 1 + 2 + 3 to the starting value 0.

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