Solutions Latest Update 2026/2027 | Verified Questions and
Answers, Complete Examination - 125 Questions
This midterm examination assesses advanced proficiency in C++ programming, covering template
metaprogramming, move semantics, concurrency, memory management, and modern standard library features. It
requires synthesis of concepts and application to complex, real-world programming scenarios. It contains 125
multiple-choice questions, each with four distractors and a fully worked rationale that explains why the keyed
answer is correct. Questions are organized into clearly labelled sections that mirror the major content areas of
the course. Targeted learning outcomes include: Analyze and implement advanced template metaprogramming
techniques.; Evaluate and apply move semantics and perfect forwarding in resource management.; Design
concurrent solutions using std::atomic and std::thread.; Critically assess undefined behavior and memory safety
in C++.. Every item has been reviewed for clinical accuracy, current guidelines, and clarity so that students can
study with confidence and self-correct as they work through the bank. Use it as a high-yield review immediately
before the exam, or as a structured practice tool during the unit - the rationales double as concise teaching notes.
The recommended writing time is 3 hours, with a passing score of 70%. Aligned with Aligned with ACM/IEEE
Computer Science Curricula 2023 and ABET accreditation standards for computing programs. standards and
reflects the question style commonly seen on accredited program examinations. Students consistently achieving
above the cut score on this bank have historically gone on to earn A+ on the corresponding course exam. Read
Section 1: General (Questions 1-125)
1 Consider the following template metaprogram that computes the
factorial of an integer at compile time. Which modification would
correctly handle the edge case of a negative input without causing
infinite recursion?
A) Add a specialization for N < 0 that returns 1.
B) Add a static_assert to enforce N >= 0.
C) Use a constexpr function with a runtime check.
D) Change the recursion to use N-1 and add a base case for N == 0.
Answer: B
Rationale: Template metaprogramming operates at compile time; a
static_assert is the correct way to enforce constraints and prevent
invalid instantiations. Adding a specialization for negative numbers
would still allow invalid values, while using a constexpr function or
changing the base case does not address the root cause of invalid
input.
,2 In the context of move semantics, what is the primary purpose of the
std::move function?
A) To force a copy constructor to be called.
B) To cast an lvalue to an rvalue reference, enabling move
operations.
C) To transfer ownership of a resource without any runtime
overhead.
D) To delete the copy constructor of a class.
Answer: B
Rationale: std::move is essentially a static_cast to an rvalue reference,
which allows the compiler to select move constructors and move
assignment operators. It does not itself perform any move; it merely
enables the possibility. Options A and D are incorrect because
std::move does not force copies or delete anything. Option C is
incorrect because there is no guarantee of zero overhead; it depends on
the implementation.
3 Which of the following best explains why std::atomic<int> is
preferred over a plain int when multiple threads increment a shared
counter?
A) std::atomic prevents data races by making all operations on the
variable atomic.
B) std::atomic is always faster than a plain int due to compiler
optimizations.
C) std::atomic automatically locks the entire surrounding code block.
D) std::atomic ensures that the variable is stored in a register.
Answer: A
Rationale: std::atomic provides atomic operations that are indivisible,
preventing data races when multiple threads access the same variable
concurrently. It does not necessarily provide better performance; in
fact, it may be slower due to synchronization overhead. It does not
lock code blocks, and it does not control register allocation.
,4 A developer writes a class that manages a dynamically allocated
resource and defines a destructor but forgets to define the copy
constructor and copy assignment operator. What is the likely
consequence?
A) The compiler will generate default copy operations that perform a
shallow copy, leading to double deletion.
B) The class will not be copyable, and compilation will fail if
copied.
C) The copy operations will be implicitly deleted, preventing
accidental copying.
D) The compiler will generate copy operations that perform a deep
copy automatically.
Answer: A
Rationale: If a user-declared destructor is present, the copy constructor
and copy assignment operator are still implicitly generated (they are
not deprecated until C++11, and even then, they are not automatically
deleted). These default operations perform a shallow copy, leading to
two objects pointing to the same resource and causing double deletion.
Option B is incorrect because they are not deleted; option C is
incorrect because they are not deleted; option D is incorrect because
deep copy is not automatic.
5 Which of the following correctly demonstrates the use of a lambda
expression to capture a local variable by reference and modify it?
A) auto lambda = [&x]() { x = 5; };
B) auto lambda = [x]() { x = 5; };
C) auto lambda = [=]() { x = 5; };
D) auto lambda = [this]() { x = 5; };
Answer: A
Rationale: To modify a local variable captured by a lambda, it must be
captured by reference using [&x]. Option B captures by value, making
x read-only. Option C captures all by value, also read-only. Option D
, captures the this pointer, which is only valid inside a member function
and does not capture local variables.
6 Which of the following is a key advantage of using std::unique_ptr
over raw pointers for managing dynamic memory?
A) std::unique_ptr automatically performs deep copies when
assigned.
B) std::unique_ptr guarantees exception safety by automatically
deleting the object when it goes out of scope.
C) std::unique_ptr allows multiple pointers to share ownership of the
same object.
D) std::unique_ptr is always more efficient than raw pointers.
Answer: B
Rationale: std::unique_ptr is a RAII class that deletes the managed
object when it goes out of scope, providing exception safety. It does
not support copying; it only supports move semantics. It does not
allow shared ownership (that's std::shared_ptr). It is not necessarily
more efficient; it has minimal overhead but not always.
7 Which of the following best describes the behavior of std::async
when called with the default launch policy?
A) The function is always executed asynchronously in a new thread.
B) The function may be executed either asynchronously in a new
thread or synchronously in the calling thread.
C) The function is always executed synchronously in the calling
thread.
D) The function is executed only if the system has sufficient
resources.
Answer: B
Rationale: The default launch policy is std::launch::async |
std::launch::deferred, giving the implementation the freedom to
choose whether to run the function asynchronously or synchronously.
This allows for optimization based on system resources and workload.