Level 1 — RecognitionC++ Programming

C++ Programming

20 minutes30 marksprintable — key stays hidden on paper

Chapter: 5.2 C++ Programming Level: 1 — Recognition (MCQ + Matching + True/False with justification) Time Limit: 20 minutes Total Marks: 30


Section A — Multiple Choice (1 mark each) — 10 marks

Choose the single best answer.

Q1. Which of the following best describes an lvalue reference (int&) compared with a pointer?

  • (a) A reference can be reseated to refer to another object after initialization
  • (b) A reference must be initialized when declared and cannot be null in well-defined code
  • (c) A reference occupies guaranteed extra storage like a pointer always does
  • (d) A reference supports pointer arithmetic

Q2. What does the unique_ptr smart pointer model?

  • (a) Shared ownership with reference counting
  • (b) Sole (exclusive) ownership of a resource
  • (c) A non-owning observer that breaks cycles
  • (d) Manual delete required by the programmer

Q3. The Rule of Three states that if a class needs a user-defined one of these, it likely needs all three. Which set?

  • (a) Constructor, destructor, assignment operator
  • (b) Destructor, copy constructor, copy assignment operator
  • (c) Default constructor, move constructor, destructor
  • (d) Copy constructor, move constructor, destructor

Q4. Which expression yields an rvalue reference used to enable move semantics?

  • (a) std::copy(x)
  • (b) &x
  • (c) std::move(x)
  • (d) const T& x

Q5. Which STL container provides O(1) average lookup by key without maintaining sorted order?

  • (a) std::map
  • (b) std::set
  • (c) std::unordered_map
  • (d) std::list

Q6. std::lock_guard primarily provides:

  • (a) Deferred locking and manual unlock control
  • (b) RAII-style automatic locking/unlocking of a mutex within a scope
  • (c) A lock-free atomic counter
  • (d) Inter-thread signalling

Q7. What is the purpose of the noexcept specifier?

  • (a) To catch all exceptions inside a function
  • (b) To declare that a function does not throw, enabling optimizations
  • (c) To rethrow exceptions automatically
  • (d) To disable RAII

Q8. SFINAE stands for:

  • (a) Standard Function Is Not An Error
  • (b) Substitution Failure Is Not An Error
  • (c) Static Function In Namespace Auto Expansion
  • (d) Simple Function Interface Non-Explicit

Q9. Which algorithm returns true only if every element satisfies a predicate?

  • (a) std::any_of
  • (b) std::all_of
  • (c) std::find
  • (d) std::accumulate

Q10. A const member function guarantees that:

  • (a) The return value is const
  • (b) It cannot modify (non-mutable) data members of the object
  • (c) It cannot be called on non-const objects
  • (d) All its parameters are const

Section B — Matching (1 mark each) — 8 marks

Match each item in Column X to the correct description in Column Y. Write pairs (e.g., Q11 → iv).

Column X Column Y
Q11. weak_ptr (i) Non-owning pointer used to break shared_ptr cycles
Q12. shared_ptr (ii) Combines multiple values into a single result (e.g. sum)
Q13. std::accumulate (iii) Shared ownership with a reference count
Q14. Anonymous namespace (iv) Gives internal linkage, replacing static-at-file-scope
Q15. Variadic template (v) Function/class accepting a variable number of type arguments
Q16. Random access iterator (vi) Supports it + n jumps in O(1)
Q17. std::condition_variable (vii) Blocks a thread until notified of a condition change
Q18. Concepts (C++20) (viii) Named constraints on template parameters

Section C — True/False WITH Justification (2 marks each: 1 verdict + 1 justification) — 12 marks

Q19. "A std::vector guarantees contiguous storage of its elements, so a pointer to its first element can be passed to a C API."

Q20. "Under the Rule of Zero, you should always write your own copy constructor, move constructor, and destructor for every class."

Q21. "std::move(x) physically moves the object's memory immediately when called."

Q22. "A std::atomic<int> can be safely incremented from multiple threads without a mutex."

Q23. "A lambda with capture list [=] captures the used enclosing variables by value."

Q24. "Full template specialization is allowed for function templates, and partial specialization is allowed for class templates but not for function templates."


Answer keyMark scheme & solutions

Section A (1 mark each)

Q1 → (b) — A reference must bind on initialization and cannot legitimately be null; unlike pointers it cannot be reseated, has no arithmetic, and (a),(c),(d) are pointer properties. (1)

Q2 → (b)unique_ptr models exclusive ownership; only one owner, non-copyable, movable. (a) is shared_ptr, (c) is weak_ptr. (1)

Q3 → (b) — Rule of Three: destructor, copy constructor, copy assignment. Manual resource management usually requires all three consistently. (1)

Q4 → (c)std::move(x) casts to an rvalue reference (T&&), signalling the object may be moved from. It does not itself move. (1)

Q5 → (c)unordered_map uses a hash table giving O(1) average lookup, unordered. map/set are tree-based O(log n) and sorted. (1)

Q6 → (b)lock_guard acquires the mutex in its constructor and releases in its destructor (RAII); no manual unlock. (1)

Q7 → (b)noexcept declares non-throwing behaviour, enabling optimizations (e.g. move in containers). Violating it calls std::terminate. (1)

Q8 → (b) — Substitution Failure Is Not An Error: an ill-formed substitution removes a candidate rather than causing a hard error. (1)

Q9 → (b)all_of returns true iff the predicate holds for every element (true for empty range). (1)

Q10 → (b) — A const member function cannot modify non-mutable members; this is a pointer-to-const. It can be called on const objects. (1)

Section B (1 mark each)

Q11 → (i) weak_ptr — non-owning, breaks cycles. Q12 → (iii) shared_ptr — shared ownership, ref count. Q13 → (ii) accumulate — folds range into single result. Q14 → (iv) anonymous namespace — internal linkage. Q15 → (v) variadic template — variable number of arguments. Q16 → (vi) random access iterator — O(1) it + n. Q17 → (vii) condition_variable — blocks until notified. Q18 → (viii) concepts — named template constraints.

(1 mark each, 8 total)

Section C (verdict 1 + justification 1)

Q19 — TRUE. (1) std::vector mandates contiguous storage; &v[0] / v.data() is a valid pointer to a C-style array of its elements (when non-empty). (1)

Q20 — FALSE. (1) Rule of Zero says the opposite: prefer not writing the special members and let the compiler generate them (by using RAII members like smart pointers). (1)

Q21 — FALSE. (1) std::move only performs a cast to rvalue reference; the actual move happens later in a move constructor/assignment that receives it. (1)

Q22 — TRUE. (1) atomic<int>::operator++ (or fetch_add) is atomic and race-free across threads without a separate mutex. (1)

Q23 — TRUE. (1) [=] captures the odr-used enclosing variables by value (a copy stored in the closure); [&] would capture by reference. (1)

Q24 — TRUE. (1) Function templates support full (explicit) specialization but not partial specialization; class templates support both. Overloading is used instead for functions. (1)

[
  {"claim":"all_of returns true for empty range (vacuous truth)", "code":"result = all(p for p in [])==True"},
  {"claim":"unordered_map average lookup O(1) < map O(log n) for large n modelled by comparing 1 vs log2(1e6)", "code":"import sympy; result = 1 < sympy.log(10**6,2)"},
  {"claim":"Rule of Three names exactly 3 special members", "code":"members={'destructor','copy_ctor','copy_assign'}; result = len(members)==3"},
  {"claim":"Section total marks = 10 + 8 + 12 = 30", "code":"result = (10 + 8 + 12)==30"}
]