Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 4 out of 66 pages
Exam (elaborations)

C++ Programming Midterm Exam Study Guide 2026/2027 | 125 Verified Syntax & Logic Questions with Detailed Solutions

Document preview thumbnail
Preview 4 out of 66 pages

Master your introductory programming assessment with this comprehensive midterm preparation guide featuring 125 verified questions and step-by-step code solutions. This document covers essential core concepts including C++ syntax, variable scopes, loops, conditional statements, basic arrays, and functional programming logic. Designed specifically for the 2026/2027 university curriculum, this study bank provides clear explanations and output verifications to ensure computer science students secure a top grade.

Content preview

C++ Programming Midterm Exam Questions and Detailed
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.

Document information

Uploaded on
August 19, 2026
Number of pages
66
Written in
2026/2027
Type
Exam (elaborations)
Contains
Questions & answers
$27.79

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
StudentArchive
3.9
(7)
Sold
31
Followers
1
Items
1286
Last sold
2 weeks ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions