Object-oriented programming and SOLID principles – Server Side interview prep

261 interview-style multiple-choice questions on Object-oriented programming and SOLID principles, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 149 key terms with the flashcards first, then test yourself.

Take the practice test · Flashcards · All study tools

What it covers

  1. 13.1 Core OOP concepts
  2. 13.2 Classes, objects and lifecycle
  3. 13.3 Inheritance and its pitfalls
  4. 13.4 Composition, delegation and interfaces
  5. 13.5 SOLID in practice
  6. 13.6 Polymorphism and type systems
  7. 13.7 Modeling domains with objects
  8. 13.8 Clean code principles
  9. 13.9 OOP across languages and design interviews

Key terms

13.1 Core OOP concepts

Encapsulation
Hiding data behind methods
Polymorphism
One interface, many implementations
Coupling
Degree of dependency between modules
Cohesion
How closely a module's parts belong together
Four pillars
Encapsulation, abstraction, inheritance, polymorphism
Abstraction
What to expose
Message passing
Objects send messages
Object
State, behavior, identity
Information hiding
Hide decisions likely to change
Interface
Contract without implementation
Abstract class
Shared code, can't instantiate
Abstract method
No body; subclasses implement
DTO
Data carrier, no behavior
Invariant
Always-true condition
Design by Contract
Pre/postconditions, invariants
Prototype-based OOP
Objects inherit from objects

13.2 Classes, objects and lifecycle

Constructor
Puts object in a valid state
Static member
Belongs to the class
Identity vs equality
Same object vs same value
Equality contract
Reflexive, symmetric, transitive
Mutable hash key
Lost after field change
Immutable object
State never changes
Wither
Returns a modified copy
Defensive copy
Copy inputs and outputs
Aliasing
Two references, one object
RAII
Resource tied to object lifetime
Rule of zero
Let members manage resources
Move semantics
Transfer instead of copy
friend
Access to private members
Property
Field access with logic
Two-phase init
Separate init() call

13.3 Inheritance and its pitfalls

Composition
Building objects out of other objects
Fragile base class
Base class changes breaking subclasses
God class
Class with far too many responsibilities
Law of Demeter
Talk only to immediate collaborators
virtual/override
Opt-in overriding
Method hiding
new modifier, static binding
Yo-yo problem
Bouncing through a deep hierarchy
Self-use problem
Base methods calling overridables
Design for inheritance
Or prohibit it
Virtual inheritance
Shared diamond base
Virtual destructor
Safe delete via base pointer
Object slicing
Derived parts dropped on copy
Upcast/downcast
To base / to derived
White-box reuse
Inheritance sees internals
Subclass explosion
One class per combination
Field hiding
Fields aren't polymorphic
open
Kotlin opt-in inheritance
NVI idiom
Final public method, protected hook

13.4 Composition, delegation and interfaces

Has-a vs is-a
Field vs subtype
Delegation
Forward work to a held object
Class delegation
Generated forwarding
Trait
Reusable method set
Duck typing
Works if methods exist
Protocol
Structural typing for checkers
Marker interface
Empty tagging interface
Program to an interface
Depend on abstractions
Aggregation vs composition
Shared vs owned lifetime
Self problem
Callbacks bypass the wrapper
Explicit interface implementation
Member only via interface
Capability interface
IReadable, IWritable
Pure virtual function
= 0
Protocol-oriented programming
Protocols with extensions
Role object
Roles gained and lost

13.5 SOLID in practice

SRP
Single Responsibility Principle
OCP
Open/Closed Principle
LSP
Liskov Substitution Principle
DIP
Dependency Inversion Principle
SRP actor
One group requesting changes
Strategic closure
Close against likely changes
Strengthened precondition
LSP violation
Weakened postcondition
LSP violation
History constraint
Subtype can't break base's state promises
Behavioral subtyping
Substitutable behavior
Fat interface
Too many members
Client-owned interface
Shaped by users
Inverted ownership
Policy owns abstraction
SOLID acronym
Coined by Michael Feathers
Common Closure Principle
Change together, package together
Acyclic Dependencies Principle
No package cycles
Stable Dependencies Principle
Depend toward stability

13.6 Polymorphism and type systems

Subtype polymorphism
Overriding
Parametric polymorphism
Generics
Ad hoc polymorphism
Overloading, type classes
Dynamic dispatch
Method chosen at runtime
vtable
Table of function pointers
Single dispatch
Receiver type only
Double dispatch
Two runtime types
Multiple dispatch
All arguments' types
Generic constraint
Required capabilities of T
Expression problem
New types vs new operations
Monomorphization
Specialized code per type
CRTP
Static polymorphism via templates
Megamorphic call site
Many shapes, slower
Coercion
Implicit conversion
Gradual typing
Optional checked annotations

13.7 Modeling domains with objects

Responsibility-driven design
Knows, does, collaborates
CRC card
Class, Responsibilities, Collaborators
Information Expert
Give work to the class with the data
Creator
Container creates its parts
Controller
First object handling a system event
Protected Variations
Stable interface around change
Pure Fabrication
Invented class for good design
Illegal states unrepresentable
Types prevent bad combos
Unit-aware type
Quantity with units
Parse, don't validate
Validated types carry proof
First-class collection
Wrapper owning collection rules
Object Calisthenics
Strict OO practice rules
Impedance mismatch
Objects vs tables
Functional core
Pure logic, I/O at edges
Association class
Relationship with its own data
Layer supertype
Shared base per layer

13.8 Clean code principles

DRY
Don't Repeat Yourself
YAGNI
You Aren't Gonna Need It
Refactoring
Improving structure without changing behavior
Guard clause
Early return for special cases
Command-query separation
Change state or return a value
Flag argument
Boolean parameter smell
Parameter object
Groups related parameters
Data clumps
Values that travel together
Why comments
Explain reasons, not lines
Least astonishment
Behave as expected
Hidden side effect
Unexpected extra action
Empty collection
Return instead of null
Fail fast
Stop early on bad state
Speculative generality
Unused flexibility
Rule of three
Abstract on the third copy
Wrong abstraction
Worse than duplication
Cyclomatic complexity
Independent paths
Cognitive complexity
How hard to understand
Explaining variable
Named condition
Deep module
Simple interface, much behind it

13.9 OOP across languages and design interviews

UML generalization
Hollow triangle arrow
UML visibility
+ - # ~
Multiplicity
1..*, 0..1
Sequence diagram
Messages over time
@property
Method as attribute
Name mangling
__x becomes _Class__x
abc.ABC
Abstract base class
MRO
C3 linearization
data class
Generated equals, copy
object declaration
Singleton
Case class
Immutable, pattern-matchable
Open classes
Reopen any class
Simula 67
First OO language
ECS
Entities, components, systems
Data-oriented design
Cache-friendly bulk data
Metatable
Lua's route to objects
Implicit interface
Every class defines one

Sample practice questions

  1. What is encapsulation?

    1. Hiding an object's data behind methods so its invariants hold
    2. Inheriting behavior from more than one base class
    3. Converting objects to JSON for storage or transport
    4. Putting every class in a separate file of its own

    Answer: A. Hiding an object's data behind methods so its invariants hold A `BankAccount` exposes `Deposit` and `Withdraw`, not a public `balance` field.

  2. What is a constructor's main job?

    1. To put a new object into a valid state, with all its invariants satisfied
    2. To register the object with a global list of all live instances
    3. To free the memory used by the object once it's no longer needed
    4. To decide which subclass the object should become at runtime

    Answer: A. To put a new object into a valid state, with all its invariants satisfied After construction, the object should be ready to use.

  3. What is the fragile base class problem?

    1. A base class that doesn't declare any methods at all
    2. Subclasses that are too small to justify their existence
    3. A base class that fails to compile on newer compilers
    4. Base class changes break subclasses relying on its internals

    Answer: D. Base class changes break subclasses relying on its internals Deep inheritance trees amplify this risk.

  4. What does the "Tell, don't ask" principle suggest?

    1. Always ask the user for confirmation before performing any action
    2. Avoid method parameters, so callers can't pass unexpected values
    3. Expose a public getter for every single field of every class
    4. Tell objects to act on their own data instead of pulling it out to decide

    Answer: D. Tell objects to act on their own data instead of pulling it out to decide It keeps behavior next to the data it uses.

  5. Which situation violates the Liskov Substitution Principle?

    1. A `Dog` class that correctly implements every member of `IAnimal`
    2. An interface that declares exactly one method and nothing else
    3. A `Square` subclass of `Rectangle` whose `SetWidth` also changes height
    4. A class that keeps all of its fields private behind properties

    Answer: C. A `Square` subclass of `Rectangle` whose `SetWidth` also changes height Subtypes must keep the behavior callers expect from the base type.

  6. Which three kinds of polymorphism are usually distinguished?

    1. Static, dynamic and hybrid polymorphism based on the compiler
    2. Public, protected and private polymorphism based on access level
    3. Single, double and triple polymorphism based on parameter count
    4. Subtype (overriding), parametric (generics) and ad hoc (overloading)

    Answer: D. Subtype (overriding), parametric (generics) and ad hoc (overloading) Coercion is sometimes listed as a fourth kind.

  7. What does responsibility-driven design, from Rebecca Wirfs-Brock, focus on?

    1. Which database tables each object maps to, designed before any behavior
    2. Assigning each class to one developer who is responsible for it
    3. Writing the user interface first and deriving objects from its screens
    4. What each object knows and does, and which objects it collaborates with

    Answer: D. What each object knows and does, and which objects it collaborates with It treats objects as role players, not data holders.

  8. What does DRY stand for, and what does it really target?

    1. "Debug, Run, Yield": the standard order of steps when fixing a bug
    2. "Do Repeat Yourself": duplicating code on purpose to keep it readable
    3. "Don't Repeat Yourself": one authoritative home for each piece of knowledge
    4. "Delete Redundant YAML": removing duplicated keys from config files

    Answer: C. "Don't Repeat Yourself": one authoritative home for each piece of knowledge Merging code that only looks alike can couple unrelated things.

  9. In a UML class diagram, what does a solid line with a hollow triangle arrowhead mean?

    1. A database foreign key, pointing from child table to parent table
    2. A method call, pointing from the caller to the class it calls
    3. Inheritance (generalization), pointing from the subclass to the base class
    4. Composition, pointing from the part to the whole that owns it

    Answer: C. Inheritance (generalization), pointing from the subclass to the base class A dashed line with a hollow triangle means implementing an interface.

  10. What is polymorphism?

    1. Code written against a base type works with many concrete types
    2. Copying objects between processes via serialization
    3. Changing a variable's declared type while the program runs
    4. A single class declaring a large number of different fields

    Answer: A. Code written against a base type works with many concrete types Calling `shape.Area()` works for circles and squares alike.

Practice all 261 questions

More Server Side