Hack4Hack

C++ practice questions with answers

Practise C++ questions at your own pace: pointers, references, the STL, virtual functions, smart pointers and move semantics. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.

  1. What does this print?

    easy

    int a = 7, b = 2;
    std::cout << a / b << " " << a % b;
    1. 3.5 1
    2. 3 1
    3. 3 0
    4. 4 1

    Answer: B. 3 1

    Explanation: Dividing two ints truncates towards zero: 7 / 2 = 3, and the remainder is 1.

  2. Which container keeps its keys sorted?

    easy

    1. std::unordered_map
    2. std::map
    3. std::vector
    4. std::queue

    Answer: B. std::map

    Explanation: std::map is a balanced binary search tree (usually red-black): sorted keys, O(log n) operations. unordered_map is a hash table.

  3. What does the & in void f(int& x) mean?

    easy

    1. x is a pointer
    2. x is a reference: changes inside f change the caller's variable
    3. x is passed by value
    4. x is the address of an int

    Answer: B. x is a reference: changes inside f change the caller's variable

    Explanation: Pass-by-reference avoids copying and lets the function modify the original. const int& gives no-copy read-only access.

  4. What is the size of the vector after these lines?

    easy

    std::vector<int> v(3, 5);
    v.push_back(1);
    v.pop_back();
    v.push_back(2);
    1. 3
    2. 4
    3. 5
    4. 6

    Answer: B. 4

    Explanation: v(3, 5) holds three 5s. push_back then pop_back cancel out, and the last push_back makes it 4.

  5. What does this print?

    medium

    int x = 5;
    int* p = &x;
    *p += 3;
    std::cout << x;
    1. 5
    2. 8
    3. The address of x
    4. Undefined

    Answer: B. 8

    Explanation: p points to x, so *p += 3 changes x itself.

  6. Which function is a virtual call resolved at run time?

    medium

    1. A non-virtual member function
    2. A virtual function called through a base-class pointer or reference
    3. A static member function
    4. A function template

    Answer: B. A virtual function called through a base-class pointer or reference

    Explanation: Dynamic dispatch uses the object's vtable, so the derived override runs even through a Base*. Non-virtual calls are bound at compile time.

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

    medium

    1. To make the class abstract
    2. So that deleting a derived object through a base pointer runs the derived destructor too
    3. It makes construction faster
    4. It is required to compile

    Answer: B. So that deleting a derived object through a base pointer runs the derived destructor too

    Explanation: Without it, delete basePtr only runs ~Base, which is undefined behaviour and usually leaks the derived part's resources.

  8. What is the time complexity of std::sort?

    medium

    1. O(n)
    2. O(n log n) on average and in the worst case (introsort)
    3. O(n²) worst case
    4. O(log n)

    Answer: B. O(n log n) on average and in the worst case (introsort)

    Explanation: Since C++11 the standard requires O(n log n) comparisons. Implementations use introsort: quicksort that falls back to heapsort.

  9. What problem does std::unique_ptr solve?

    hard

    1. Sharing ownership between many owners
    2. Exclusive ownership: the object is deleted automatically when the unique_ptr goes out of scope, and it can be moved but not copied
    3. Making objects thread-safe
    4. Garbage collection

    Answer: B. Exclusive ownership: the object is deleted automatically when the unique_ptr goes out of scope, and it can be moved but not copied

    Explanation: This is RAII. shared_ptr is for shared ownership with reference counting; weak_ptr breaks cycles.

  10. What does std::move do?

    hard

    1. Moves memory to a new address
    2. Casts its argument to an rvalue reference so a move constructor or move assignment can be chosen
    3. Deletes the source object
    4. Copies the object faster

    Answer: B. Casts its argument to an rvalue reference so a move constructor or move assignment can be chosen

    Explanation: std::move itself moves nothing; it only allows overloads that take T&& to "steal" resources. The moved-from object is left valid but unspecified.

  11. After these lines, what is true about r?

    hard

    std::vector<int> v = {1, 2, 3};
    int& r = v[0];
    v.reserve(1000);
    1. r is still a valid reference to v[0]
    2. r may now dangle: reallocation invalidates references, pointers and iterators
    3. r becomes 0
    4. It does not compile

    Answer: B. r may now dangle: reallocation invalidates references, pointers and iterators

    Explanation: Growing capacity moves the elements to new memory. Any reference or iterator taken before the reallocation is invalid.

  12. Which statements about const in C++ are true? (Select all that apply)

    hard

    1. A const member function promises not to modify the object
    2. const int* p means the int cannot be changed through p
    3. int* const p means p cannot point elsewhere
    4. const variables are always stored in read-only memory

    Answer: A. A const member function promises not to modify the object; B. const int* p means the int cannot be changed through p; C. int* const p means p cannot point elsewhere

    Explanation: Read const from right to left: const int* is a pointer to const int; int* const is a const pointer. Where a const object is stored is up to the compiler.

Play the Daily Quiz at 4 PM IST