OOP Mid
How does the lifecycle of an object work, and explain the concept of garbage collection or manual destruction at a high level?
Select the correct answer
It is duplicated on each access and every old copy must be freed by the caller
It exists forever from program start and is only cleared at final process exit
It is created, used, then reclaimed automatically or freed manually when unreferenced
It is destroyed instantly the moment any single method finishes running on it
Can you have an object without a class?
Select the correct answer
Yes, but only when the object contains no methods and holds pure data
Yes, prototype-based languages create objects directly from other objects
No, every object universally requires a compiled class declaration first
No, unless the runtime silently generates a hidden anonymous class for it
What happens conceptually during Object Instantiation?
Select the correct answer
All existing instances are copied together into one new merged object
A shared static template is renamed and exposed directly to the caller
Memory is allocated, fields are initialized, and a reference is returned
The class source is recompiled and its bytecode is reloaded into memory
What is the 'Root Object' concept (e.g., Object class) found in many OO languages?
Select the correct answer
The first instance created at startup that every later object must reference
The entry class whose main method runs before any other object exists
A base type all classes implicitly inherit, providing common shared methods
A special container holding one global copy of every class in the program
Explain the difference between state, behavior, and identity of an object.
Select the correct answer
State is its data, behavior is its methods, identity is its unique existence
State is its methods, behavior is its data, identity is its memory alignment
State is its class name, behavior is its parent, identity is its field count
State is its address, behavior is its type, identity is its stored values
Explain the order of initialization when an object is instantiated.
Select the correct answer
Superclass constructor runs first, then field initializers, then the constructor body
Field initializers run first, then the constructor body, then the superclass constructor
The superclass and subclass constructors run together, then all field values are set
The constructor body runs first, then field initializers, then the superclass setup
What are the primary goals of OOP, and how does it improve modularity, reusability, and maintainability in a large-scale codebase?
Select the correct answer
It forces all code into a single class so the whole system stays tightly connected together.
It removes interfaces entirely, exposing internal state so modules can share data freely.
It duplicates logic across classes to guarantee that each module stays fully independent.
It encapsulates state behind interfaces, letting components be reused and changed independently.
Explain the concept of 'Message Passing' in OOP.
Select the correct answer
Objects directly modify each other's fields, bypassing methods to exchange internal data.
Messages are shared memory buffers that objects read from to synchronize their state.
Message passing means copying an entire object whenever another object needs its data.
Objects request behavior by sending messages, letting the receiver decide how to respond.
What is the difference between Abstraction and Encapsulation? They both hide things, so why do we need both?
Select the correct answer
Abstraction bundles data with methods; encapsulation hides complexity behind a clean and simple interface.
Abstraction and encapsulation are identical concepts described using two different naming conventions in code.
Abstraction restricts access with keywords; encapsulation groups related classes together into logical packages.
Abstraction hides complexity to expose essential behavior; encapsulation bundles data and hides internal state.
Explain the conceptual difference between public, private, and protected access, and why is package-private or internal visibility useful in large systems?
Select the correct answer
public is visible within a package, private to subclasses, protected everywhere; internal exposes helper methods globally.
public is visible everywhere, private within the class, protected to subclasses; package-private hides module internals.
public is limited to the class, private to the module, protected to callers; package-private restricts inheritance chains.
public is open to subclasses, private to the package, protected within a file; internal keeps constructors fully hidden.
What is an 'Inner Class' (or Nested Class), and when is it conceptually appropriate to use one?
Select the correct answer
A class instantiated statically; used when it must exist before the outer class object is ever created at runtime.
A class defined inside another; used when it is tightly bound to and only meaningful within the outer class.
A class that inherits from another; used when it must reuse and extend the outer class's public behavior.
A class shared across packages; used when many unrelated classes need access to the same reusable helper logic.
What is an anonymous class, and in what situations is it conceptually appropriate to use one?
Select the correct answer
A class created at runtime by reflection, ideal for dynamic plugin type loading.
A class with no fields or methods, ideal for marking a type without behavior.
A class hidden from other modules, ideal for keeping internal helpers private.
A class defined without a name and used once, ideal for quick one-off behavior.
What is the purpose of a sealed or final class, and when would you prevent a class from being extended?
Select the correct answer
To prevent modification so the class's fields become read-only after creation.
To prevent inheritance so the class's behavior and invariants stay guaranteed.
To prevent overriding so subclasses reuse but cannot alter any parent methods.
To prevent instantiation so the class can only expose static utility methods.
What is the purpose of a constructor, and can you explain constructor chaining and why it is used in inheritance hierarchies?
Select the correct answer
One constructor calls another to reuse setup and initialize the base class first.
One constructor overrides another so the most specific initializer always runs.
One constructor delays another until all subclass fields have been fully assigned.
One constructor copies another's fields to avoid duplicating initialization logic.
Under what circumstances would you define a constructor as private, and what does this achieve conceptually?
Select the correct answer
To block method calls, ensuring only internal code can invoke the object's behavior.
To block subclassing, ensuring the class stays final and cannot be extended later.
To block external instantiation, enabling singletons or factory-controlled creation.
To block field access, ensuring the object's state stays fully encapsulated always.
What is a copy constructor, and when would you use one instead of relying on default copying?
Select the correct answer
It merges two objects into one, combining their fields into a single new instance.
It resets an existing object's fields, reusing the instance instead of allocating one.
It builds a new object from an existing one, giving control over how copies form.
It duplicates a reference to an object, letting two variables share the same state.
What is the concept of a static factory method as a language feature, and how does it differ from using a constructor directly?
Select the correct answer
A static method that mutates shared global state each time it runs instead of returning a new object.
A method that overrides the default constructor to enforce singleton behaviour across the whole app.
A constructor variant that is marked static so it can be invoked without any instance of the class.
A static method returning an instance that, unlike a constructor, can be named and may reuse objects.
Explain the difference between association, aggregation, and composition, and how does ownership and lifetime define a composition versus an aggregation?
Select the correct answer
In aggregation the part is owned and dies with the whole, while composition lets the part exist alone.
In association the part is owned and dies with the whole, while composition keeps the part independent.
In composition and aggregation alike the part always shares the exact lifetime of the whole object.
In composition the part is owned and dies with the whole, while aggregation lets the part outlive it.
Explain the is-a relationship, and what are the risks of creating an inheritance hierarchy that is too deep?
Select the correct answer
Is-a denotes composition; deep hierarchies improve reuse and always make the code easier to maintain later.
Is-a denotes inheritance; deep hierarchies raise coupling and fragility, making changes hard to reason about.
Is-a denotes delegation; deep hierarchies remove duplication and guarantee changes stay local to a class.
Is-a denotes aggregation; deep hierarchies reduce coupling but slow the running program down considerably.
What is the difference between Aggregation and Composition in terms of object lifetime?
Select the correct answer
In both composition and aggregation the contained object is destroyed together with its parent container.
In composition the contained object outlives its container, while in aggregation they share one lifetime.
In aggregation the contained object dies with its container, while in composition it exists on its own.
In composition the contained object dies with its container, while in aggregation it exists independently.
Why can a class implement multiple interfaces but not inherit from multiple classes in many languages?
Select the correct answer
Interfaces are resolved at runtime while classes resolve at compile time, avoiding clashes.
Classes are stored differently in memory, which physically prevents linking two parents.
Interfaces cannot contain any methods at all, so nothing they hold could ever conflict.
Interfaces declare behavior without shared state or implementation, so no ambiguity can arise.
What is a dependency relationship between classes, and how does it differ from an association?
Select the correct answer
A dependency is always bidirectional, while an association can only ever point one direction.
A dependency is a transient 'uses' link, while an association is a persistent structural relationship.
A dependency implies ownership of an object, while an association just means two classes coexist.
A dependency stores a reference as a field, while an association is only a temporary method call.
How do UML class diagrams represent relationships like inheritance, composition, and association, including multiplicity?
Select the correct answer
Inheritance uses a hollow diamond, aggregation a solid line, and multiplicity is never drawn on the ends.
Inheritance uses a hollow triangle, composition a filled diamond, and multiplicity numbers label the ends.
Inheritance uses a dashed arrow, association a filled diamond, and multiplicity appears inside each class box.
Inheritance uses a filled diamond, composition a hollow triangle, and multiplicity is shown by dashed lines.
Explain the difference between compile-time (static) and runtime (dynamic) polymorphism with a conceptual example of each.
Select the correct answer
Static resolves calls at compile time via overloading; dynamic resolves at runtime via overriding.
Static resolves calls at runtime via overriding; dynamic resolves at compile time via overloading.
Static polymorphism applies to fields and data; dynamic applies to constructors and destructors.
Static polymorphism uses interfaces exclusively; dynamic uses only abstract base class methods.
What is method overriding, and how does it differ from method hiding or shadowing?
Select the correct answer
Overriding is resolved by the reference's declared type; hiding uses dynamic dispatch on the runtime type.
Overriding works only with static methods; hiding works only with instance methods marked as virtual.
Overriding uses dynamic dispatch on virtual methods; hiding is resolved statically by the reference's type.
Overriding requires identical signatures; hiding requires different signatures within the same hierarchy tree.
What is the difference between Upcasting and Downcasting, and what are the risks of the latter?
Select the correct answer
Upcasting converts a subtype to a supertype safely; downcasting reverses it and can fail at runtime.
Upcasting converts a supertype to a subtype and is always safe; downcasting reverses it and never fails.
Downcasting converts a subtype to a supertype safely; upcasting is risky and needs an explicit type check.
Upcasting and downcasting both change the object's actual type in memory, risking data loss on conversion.
What is a 'Virtual Method' conceptually, and how does 'Late Binding' work?
Select the correct answer
A virtual method can be overridden by subclasses, and late binding resolves the actual call at runtime.
A virtual method is abstract with no body, and late binding inlines the correct implementation during compilation.
A virtual method runs only on the base class, and late binding delays object creation until the call.
A virtual method cannot be overridden, and late binding chooses the method at compile time from the reference.
What are the rules for method overloading versus overriding, and can you overload a method by changing only the return type?
Select the correct answer
Overloading needs different parameter lists in a class; you cannot overload by only changing the return type.
Overloading needs the same parameters across classes; you can overload a method by only changing its return type.
Overriding needs different parameter lists; the return type alone is enough to distinguish two overloaded methods.
Overloading requires an inheritance relationship; changing only the return type creates a valid method overload.
Explain the concept of 'Subtype Polymorphism' without using code.
Select the correct answer
A single type is reused with different data structures, each supplying identical behavior for all callers.
A base type inherits behavior from its subtypes, letting callers reuse the most specialized version available.
One function name works on unrelated types by using type checks to select behavior at runtime.
Code written against a base type can operate on any subtype, each supplying its own behavior.
What is the difference between 'Early Binding' and 'Late Binding'?
Select the correct answer
Early binding resolves calls at compile time; late binding resolves them at runtime by actual object type.
Early binding links methods during object creation; late binding links them when the class is first loaded.
Early binding applies to static methods only; late binding applies to any method regardless of the object.
Early binding resolves calls at runtime; late binding resolves them at compile time using the reference type.
What is the difference between an Abstract Class and an Interface, and when would you choose one over the other?
Select the correct answer
Abstract classes hold state and partial implementation; interfaces declare behavior without state.
Abstract classes are resolved at runtime while interfaces are fully resolved during compilation.
Abstract classes cannot be extended further whereas interfaces can be freely implemented anywhere.
Abstract classes support multiple inheritance while interfaces are restricted to a single parent.
What is a 'Marker Interface,' and what is its purpose if it has no methods?
Select the correct answer
It marks a class as final so no subclass can override or extend its existing functionality.
It forces implementers to write empty method bodies purely to document future intended behavior.
It groups shared constants that many classes import, avoiding duplicate declarations elsewhere.
It tags a class with metadata so code can test type membership, despite having no methods.
What does it mean to say an interface is a contract, and how does this facilitate decoupled development between teams?
Select the correct answer
It enforces that both teams share the same class hierarchy before any development work can begin.
It guarantees runtime behavior is identical across every class that chooses to implement the type.
It records concrete implementation details so consumers know exactly how each operation is performed.
It specifies what operations exist without implementation, letting teams build against it independently.
Can an Interface have a constructor? Why or why not?
Select the correct answer
No, because constructors are inherited from the first concrete class that implements it later.
No, because an interface cannot be instantiated and holds no instance state to initialize.
Yes, but only a private constructor used internally to set up its default constant values.
Yes, provided the constructor takes no arguments and simply returns the interface instance.
Why is it often said that you should favour composition over inheritance, and what are the specific trade-offs in flexibility?
Select the correct answer
Composition assembles behaviour from objects at runtime, giving more flexibility than rigid inheritance
Composition always runs faster than inheritance because no virtual dispatch is ever needed
Composition removes all coupling entirely, so classes never depend on one another anymore
Inheritance is preferred because composition breaks encapsulation and exposes internal object state
Explain the concept of substitutability: why should a derived class be able to stand in for its base class without breaking the program?
Select the correct answer
A subclass must honour the base class contract so client code works unchanged with either
A subclass must add new methods so it can fully replace the base class in all cases
A subclass must be smaller than its base class to safely stand in wherever it is used
A base class must expose all its fields publicly so subclasses can override behaviour freely
What does it mean for two objects to be 'Tightly Coupled,' and how does OOP help achieve 'Loose Coupling'?
Select the correct answer
Tightly coupled objects share a common base class; polymorphism removes the shared inheritance need
Tightly coupled objects depend on each other's details; interfaces and abstraction reduce that dependency
Tightly coupled objects run in the same thread; message passing separates them into different threads
Tightly coupled objects have identical methods; encapsulation hides those methods to loosen the link
How do you identify potential classes and responsibilities from a set of requirements?
Select the correct answer
Verbs in the requirements suggest candidate classes and nouns suggest their responsibilities
Classes are derived only from the database schema, not from the written requirements text
Each requirement sentence becomes exactly one class with one responsibility by definition
Nouns in the requirements suggest candidate classes and verbs suggest their responsibilities
What does 'High Cohesion' mean for a class, and why is it desirable?
Select the correct answer
A class exposes all its fields directly so related objects can share state more efficiently
A class holds as many methods as possible so callers rarely need other classes around
A class depends heavily on other classes so its behaviour stays consistent across the system
A class's members focus on a single, well-defined responsibility, making it easier to maintain
In the context of object design, what does it mean to have high cohesion and low coupling, and why is this the gold standard?
Select the correct answer
A class handles one focused responsibility with minimal dependencies on other classes.
A class depends heavily on others while keeping its internal methods loosely related.
A class handles many responsibilities but shares no data with other classes at all.
A class exposes all of its fields publicly so other classes can freely reuse them.
How do you decide what should be a class versus what should be an attribute when modeling a real-world system?
Select the correct answer
Make it a class if it stores a single value, else always model it as an attribute.
Make it a class only when it holds numbers, and an attribute when it holds text.
Make it a class if it appears once, else an attribute when it repeats many times.
Make it a class if it has its own behavior and identity, else an attribute.
What is the difference between a Shallow Copy and a Deep Copy when cloning an object?
Select the correct answer
A shallow copy duplicates all nested objects; a deep copy only copies the top level.
A shallow copy shares nested objects by reference; a deep copy duplicates them fully.
A shallow copy is always faster while a deep copy always fails on immutable fields.
A shallow copy clones primitives only while a deep copy clones nothing but pointers.
Explain the concept of 'Object Equality' versus 'Object Identity.' How do they differ?
Select the correct answer
Equality checks the memory address; identity compares the contents of two objects.
Equality applies only to primitives; identity applies only to reference-type objects.
Equality compares values or state; identity checks if it's the very same instance.
Equality is always reflexive but identity can change whenever an object is mutated.
What is an immutable object, and what are the benefits of designing classes to be immutable by default?
Select the correct answer
Its methods are all static, so instances never need to be created or destroyed later.
Its state cannot change after creation, making it thread-safe and easy to share.
Its fields are all public constants, letting other objects modify them without copying.
Its state can change only once after creation, which prevents accidental data races.
What is the contract between equals and hashCode, and why must two 'equal' objects produce the same hash code?
Select the correct answer
Unequal objects must always share a hash code to avoid collisions inside the map.
Equal objects must share a hash code so hash-based collections can find them.
Equal objects must have different hash codes so they occupy separate hash buckets.
Hash codes must equal the object's identity so equals can reuse the same address.
What is the purpose of generics in OOP, and how do they improve type safety without sacrificing reusability?
Select the correct answer
They cast objects dynamically at runtime, giving flexible storage while a single container tolerates any inserted concrete type
They parameterize code over types, giving compile-time checking while a single implementation works for many concrete types
They overload methods per type signature, giving distinct behavior while one class name covers many parameter combinations
They defer type resolution to reflection, giving late binding while a single interface adapts to unknown concrete types
What is operator overloading conceptually, and what are the risks of abusing it?
Select the correct answer
Defining multiple methods with one operator name; abuse yields ambiguous resolution that forces explicit casting everywhere
Giving operators custom meaning for user types; abuse yields surprising, unintuitive semantics that reduce code readability
Redefining built-in operators globally for all types; abuse yields runtime conflicts that break unrelated library code silently
Mapping operators to virtual dispatch at runtime; abuse yields slower arithmetic that degrades performance in tight loops
What is a finalizer or destructor conceptually, and why are finalizers considered unreliable for resource cleanup?
Select the correct answer
A method invoked explicitly by the programmer at end of use; unreliable because forgetting to call it leaves resources permanently leaked
A method that frees memory as soon as an object is created; unreliable because it blocks other threads during allocation cycles
A method run by the garbage collector before reclaiming an object; unreliable because its timing is nondeterministic and it may never run
A method run automatically when an object leaves scope; unreliable because it cannot safely access any of the object's instance fields