Intermediate Programming
3.0 Credits
Midterm Exam Review (Qns & Ans)
2025
©2025
, Multiple Choice Questions (10 Questions)
1. Question:
In a recursive function, which property designates that the
recursive call is the final operation executed, allowing the compiler
to optimize stack usage?
- a. Head recursion
- b. Tail recursion
- c. Nested recursion
- d. Indirect recursion
ANS:
b. Tail recursion
Rationale:
Tail recursion occurs when a function’s recursive call is its last
action. Many compilers can optimize such calls (tail-call
optimization) by reusing the current function’s stack frame, thus
reducing memory overhead.
2. Question:
Which container in C++ dynamically adjusts its size and manages
memory automatically?
- a. Static array
- b. std::vector
- c. C-style array
- d. std::list
ANS:
b. std::vector
Rationale:
The `std::vector` provides dynamic memory allocation,
automatically resizing as elements are added, making it ideal for
cases where the size isn’t known at compile time.
3. Question:
©2025
, Which balanced binary tree data structure ensures that the height
difference between left and right subtrees is at most one?
- a. Binary Search Tree (BST)
- b. AVL Tree
- c. B-Tree
- d. Red-Black Tree
ANS:
b. AVL Tree
Rationale:
An AVL tree maintains a strict balance condition (height
difference ≤ 1) at every node, offering faster lookup times in the
worst case than an unbalanced BST.
4. Question:
Which design pattern decouples the construction of a complex
object from its representation, making it possible to create different
types of the same object using the same construction process?
- a. Singleton
- b. Observer
- c. Builder
- d. Decorator
ANS:
c. Builder
Rationale:
The Builder pattern separates object construction from its
representation, allowing the same construction process to create
various representations. This is particularly useful in constructing
complex objects in an organized and modular way.
5. Question:
In Java concurrency, which interface, when combined with the
ExecutorService framework, enables asynchronous computation
and retrieval of a result?
- a. Runnable
©2025
, - b. Callable
- c. FutureTask
- d. Timer
ANS:
b. Callable
Rationale:
The `Callable` interface allows tasks to return a result and throw
exceptions. When executed by an `ExecutorService`, it returns a
`Future` object representing the result of the asynchronous
computation.
6. Question:
According to the Liskov Substitution Principle, which statement
best describes the requirement for subclass objects relative to their
base class?
- a. Subclass objects can alter base class behavior arbitrarily.
- b. Subclass objects must be substitutable for base class objects
without affecting program correctness.
- c. Subclass objects must mirror the implementation details of
base class methods.
- d. Subclass objects eliminate the need for overriding methods.
ANS:
b. Subclass objects must be substitutable for base class objects
without affecting program correctness.
Rationale:
The Liskov Substitution Principle requires that objects of a
superclass be replaceable with objects of a subclass without
altering the desirable properties of the program, ensuring
consistent behavior.
7. Question:
In modern C++ programming, which type of smart pointer
provides exclusive ownership semantics for a dynamically allocated
object?
©2025