Questions and Detailed Solutions Latest Update 2026/2027 |
Encapsulation, Interfaces, Exception Handling, Verified
Answers - 190 Questions
This midterm exam assesses advanced proficiency in object-oriented programming concepts including
encapsulation, interfaces, exception handling, and design patterns. It requires analytical reasoning and
application to complex, real-world programming scenarios. It contains 190 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 design robust encapsulation boundaries in complex systems.; Evaluate and
implement interface-based designs for extensibility and polymorphism.; Design comprehensive exception handling
strategies for fault-tolerant applications.; Apply advanced OOP principles to solve novel programming problems..
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 CS
curricula and rigorous US university standards. 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 every stem carefully - distractors are
Section 1: General (Questions 1-190)
1 In a system where a class must expose read-only state to external
clients but also allow internal mutation, which design pattern best
preserves encapsulation while minimizing coupling?
A) Using public fields with const qualifiers
B) Providing getter methods that return deep copies of internal state
C) Implementing a friend class for all external clients
D) Using reflection to access private members
Answer: B
Rationale: Returning deep copies prevents external mutation of
internal state, preserving encapsulation. Public fields with const are
not enforceable in most OO languages. Friend classes increase
coupling. Reflection violates encapsulation.
2 When an interface evolves by adding a new method, which
approach avoids breaking existing implementers while supporting
the new functionality?
,A) Add the method to the interface and update all implementers
B) Create a new interface that inherits the old one and add the
method there
C) Use a default method in the interface with a default
implementation
D) Define the method as a static utility in a separate class
Answer: C
Rationale: Default methods allow new functionality to be added
without forcing existing classes to implement them, preserving
backward compatibility. Option B creates a new interface but doesn't
provide backward compatibility. Option A breaks existing
implementers. Option D doesn't support polymorphic behavior.
3 Which exception handling approach best preserves the original
exception's context when wrapping it in a domain-specific
exception?
A) Throw a new exception with a message that includes the original
exception's string
B) Catch the low-level exception, log it, and rethrow the same
exception
C) Catch the low-level exception, set it as the cause of the new
exception, and throw the new exception
D) Ignore the low-level exception and throw a custom exception
without any reference
Answer: C
Rationale: Setting the original exception as the cause preserves the
stack trace and context, aiding debugging. Option A loses stack trace.
Option B doesn't abstract the low-level details. Option D discards
context entirely.
4 Given a class with a private constructor and a static factory method
that caches instances, which pattern does this exemplify and what is
its primary benefit?
,A) Prototype pattern; allows cloning of instances
B) Singleton pattern; ensures a single instance and controlled access
C) Factory Method pattern; delegates object creation to subclasses
D) Abstract Factory pattern; creates families of related objects
Answer: B
Rationale: A private constructor with a static factory that returns a
cached instance is the Singleton pattern. It controls instantiation and
provides a global access point. The other patterns do not involve
caching or private constructors in this way.
5 In languages with checked exceptions, which practice best aligns
with robust exception handling in large-scale applications?
A) Declare every method as throwing Exception to allow any
exception to propagate
B) Catch all exceptions at the top level and log them without
rethrowing
C) Use custom exception types that are specific to the error domain
and handle them at appropriate layers
D) Convert all checked exceptions to unchecked exceptions before
propagating
Answer: C
Rationale: Custom domain-specific exceptions improve clarity and
allow targeted handling. Declaring generic Exception or catching all at
top level hides details. Converting to unchecked may be useful but not
universally best; it can bypass compile-time checks.
6 Which of the following is a key trade-off when using composition
over inheritance for code reuse?
A) Composition leads to tighter coupling between classes
B) Composition makes it harder to change behavior at runtime
C) Composition can result in more boilerplate code and slightly
more complex delegation
D) Composition prevents the use of polymorphism
, Answer: C
Rationale: Composition often requires explicit delegation, which can
be more verbose. It reduces coupling and allows runtime flexibility, so
A and B are false. Polymorphism is still possible through interfaces,
so D is false.
7 Consider a banking application where a transfer must be atomic.
Which exception handling strategy ensures that if the debit succeeds
but the credit fails, the system remains consistent?
A) Catch the credit exception, log it, and continue with the next
transaction
B) Use a try-catch around the credit and perform a compensating
rollback of the debit in the catch block
C) Perform the debit only after the credit has been completed
D) Use a global transaction manager that automatically rolls back all
operations on any exception
Answer: B
Rationale: A compensating rollback ensures atomicity in the absence
of a full transaction manager. Option A leaves inconsistent state.
Option C doesn't guarantee atomicity. Option D may not be available
in all contexts.
8 In a language that supports multiple inheritance, why might a
developer prefer interfaces over abstract classes for defining
capabilities?
A) Interfaces can contain implementation code, reducing duplication
B) Interfaces allow a class to inherit behavior from multiple sources
without diamond ambiguity
C) Interfaces enforce a contract without imposing a class hierarchy,
reducing coupling
D) Interfaces are faster at runtime because they avoid dynamic
dispatch
Answer: C