LearnThatStack Ace your next interview
Computer Science Fundamentals
Object-Oriented Programming.
Change topic Change
Practice · Questions

All questions

Showing of 79
Beginner 26
01

What are the four pillars of object-oriented programming?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Encapsulation, abstraction, inheritance and polymorphism are the four, and each buys something different. Encapsulation keeps an object's data and the code that changes it together, behind a guarded door. Abstraction shows what an object does and hides how it does it. Inheritance lets one class build on another. Polymorphism lets one piece of code work with several types.

These are not four independent features. Encapsulation and abstraction are two views of the same boundary. One hides state, the other hides steps. Inheritance is only one route to polymorphism, and often not the best one.

Treat them as tools, not as goals. Applying all four to every class produces deep hierarchies and wrapper types that buy nothing. The real payoff is narrow and measurable. A change lands in one file instead of twenty, and a new case plugs in without touching old code.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

What is encapsulation, and what does hiding an object's state actually buy you?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

An object owns its data, and outside code reaches that data only through methods the object controls. Fields stay private, and the class decides which changes are legal. The private keyword is the mechanism; the discipline is the point.

Hiding state buys three things. You enforce invariants in one place, so an account refuses a withdrawal that goes below zero and no caller can skip that check. You swap the internal representation, a list for a map, without touching callers. And when a balance is wrong, the suspect list is one class, not the whole codebase.

The cost is ceremony. Not every type needs a wall around it. A plain data holder passed between two functions gains nothing from private fields and pass-through accessors, and the extra layer just makes the code longer to read.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

What is abstraction, and what techniques do you use to achieve it?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Abstraction is deciding what to leave out. You name an operation by its intent and hide the steps behind that name. A caller writes save(order) and never learns whether that is a SQL insert, a file write or an HTTP call.

The techniques are ordinary. Interfaces state a contract with no implementation. Abstract classes fix a shape and leave gaps for subclasses. Ordinary methods give one name to a block of steps. Access modifiers back all of this up by keeping the internals out of reach.

A wrong abstraction costs more than none at all. If the contract you picked does not match how callers use the thing, every caller writes a workaround. Flags and escape hatches follow. Duplicated code is easier to fix later than a bad abstraction everyone depends on.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

What is inheritance for, and what types of inheritance exist?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Inheritance lets one class take another's fields and behaviour as a starting point, then add to them or replace parts. It gives two things at once: code reuse, and a subtype relationship, so a dog can be passed anywhere an animal is expected. The second is the part that matters.

Textbooks name the shapes by how the arrows point:

  • Single: one class extends one parent.
  • Multilevel: a chain, where a child is itself a parent to something else.
  • Hierarchical: several classes extend the same parent.
  • Multiple: one class extends more than one parent, which most languages allow for interfaces but not classes.

The split worth remembering is implementation inheritance versus interface inheritance. Taking a parent's code ties you to its internals, so its next change can break you. Taking only its contract does not. Every subclass is also a standing promise to keep behaving like the parent, which is easy to make and hard to keep.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

What is polymorphism, and what types of polymorphism do languages support?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Code written against one name can work with many different types, and the right version runs automatically. A loop calling shape.area() gets the circle's formula for circles and the square's for squares. The calling code never branches on type.

Languages support several flavours:

  • Subtype polymorphism: a subclass or implementation supplies its own version, and the object picks at runtime.
  • Ad-hoc polymorphism: one name, several parameter lists, resolved when the code is compiled. Operator overloading is the same idea.
  • Parametric polymorphism: generics, where one List<T> works for every element type.
  • Duck typing: in dynamic languages, any object with the right method fits, with no declared type.

The gain is that adding a type does not mean editing existing branches, so old code stays untouched. The cost is indirection. Reading a call site no longer tells you which method actually runs, and a debugger or a stack trace becomes the only honest answer.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

What is the difference between composition and inheritance?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Composition means one object holds another and asks it to do work. Inheritance means one class is a specialised kind of another and receives its members automatically. A car has an engine; an electric car is a car.

Three differences bite in practice. Inheritance is fixed when the code is written, while a composed part can be swapped at runtime. A subclass can see protected members, while a holder sees only the public surface. A subclass inherits the whole parent interface, wanted or not.

Both reuse code, but only inheritance also declares substitutability. That promise is what lets a subclass be passed where the parent is expected, and it is the part you cannot take back later. If all you want is reuse, composition asks for less and gives back more room to change.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

07

How does an abstract class differ from an interface?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

An interface and an abstract class can both ship working code today. The real dividing lines are state, constructors and the single-inheritance slot.

Java 8 added default and static methods to interfaces. C# 8 added default interface implementations.

interface Greeter {
    String name();
    default String greet() { return "Hi, " + name(); }
}

Interfaces cannot hold instance fields (per-object data) or define constructors. An abstract class holds both, plus protected helpers subclasses share.

Feature Interface Abstract class
Method bodies Yes, default and static Yes
Instance fields No Yes
Constructors No Yes
Allowed per class Many One

A class can implement many interfaces but extend only one abstract class. That makes an interface cheap to add to a type that already has a parent. An abstract class can carry state and enforce an order of steps.

The choice comes down to whether subclasses genuinely share code, or only share a capability that callers depend on.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

08

What are the SOLID principles, and why do teams bother with them?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Five design rules, one per letter, all aimed at code that people keep changing.

  • Single responsibility: a class should have one reason to change.
  • Open/closed: you should be able to add behaviour without editing code that already works.
  • Liskov substitution: a subtype must work wherever its base type does, with no surprises.
  • Interface segregation: several small contracts beat one fat contract that forces empty methods.
  • Dependency inversion: high-level code should depend on abstractions, not on concrete classes.

Teams bother because each letter names a specific pain they have already felt. Shotgun edits across five files for one change. An if chain that grows with every new feature. A subclass that throws from a method it inherited. Stubs full of empty methods. Logic that cannot run without a real database. Applied blindly, though, SOLID produces a class per line of logic, which is its own mess.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

09

What is the Single Responsibility Principle, and what does it ask of a class?

Part of Pro
10

What is the Open/Closed Principle, and what is a real example of it?

Part of Pro
11

What does the Liskov Substitution Principle ask of a subclass, and why?

Part of Pro
12

What is the Interface Segregation Principle trying to prevent?

Part of Pro
13

What is dependency injection, and how does it differ from creating objects yourself?

Part of Pro
14

What are the three forms of dependency injection, and how do they differ?

Part of Pro
15

What is a dependency injection container, and what work does it take over?

Part of Pro
16

What do coupling and cohesion mean, and why aim for low coupling and high cohesion?

Part of Pro
17

What is the difference between reference equality and value equality?

Part of Pro
18

When you override equals, what other method must you override, and why?

Part of Pro
19

What must a correct equals method guarantee, including when it is given null?

Part of Pro
20

What is an immutable class, and how do you recognise one?

Part of Pro
21

What are the rules a method must follow to be a valid overload?

Part of Pro
22

Give a real-world example of abstraction and one of encapsulation

Part of Pro
23

What do public, private and protected mean, and when would you use each?

Part of Pro
24

What is the difference between a class and an object?

Part of Pro
25

What are the SOLID principles and why are they important in software development?

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

SOLID is an acronym representing five fundamental design principles for object-oriented programming that promote clean, maintainable, and extensible code:

  • S - Single Responsibility Principle: A class should have only one reason to change
  • O - Open/Closed Principle: Software entities should be open for extension but closed for modification
  • L - Liskov Substitution Principle: Objects should be replaceable with instances of their subtypes
  • I - Interface Segregation Principle: Clients should not depend on interfaces they don't use
  • D - Dependency Inversion Principle: Depend on abstractions, not concretions

Benefits of following SOLID principles:

  • Maintainability: Changes are localized and predictable
  • Flexibility: Easy to extend and modify existing functionality
  • Testability: Components can be tested in isolation
  • Reusability: Well-designed components can be reused across different contexts
  • Reduced coupling: Components are loosely connected and independently changeable
  • Code clarity: Intent and structure are clearer and easier to understand
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

26

Explain the Dependency Inversion Principle.

Part of Pro
Intermediate 35
27

Why prefer composition over inheritance, and what does each approach cost you?

Part of Pro
28

Should a class hierarchy start from an interface or an abstract class?

Part of Pro
29

Why do most languages allow many interfaces but only one base class?

Part of Pro
30

What is the diamond problem, and how do languages resolve it?

Part of Pro
31

What are the limits of inheritance, including the fragile base class problem?

Part of Pro
32

What is the difference between method overloading and method overriding?

Part of Pro
33

Can a static method be overridden, or is it only hidden?

Part of Pro
34

What is compile-time polymorphism, and how does it differ from runtime polymorphism?

Part of Pro
35

Which methods can a subclass never override, and why not?

Part of Pro
36

Can an overriding method declare a different return type than the original?

Part of Pro
37

Why is calling a static method through an instance considered bad practice?

Part of Pro
38

How is encapsulation different from abstraction when they look so similar?

Part of Pro
39

Describe a class that violates the Single Responsibility Principle and how you would split it

Part of Pro
40

A subclass cannot honour a method it inherits - why does that break LSP?

Part of Pro
41

An interface has ten methods but a class needs two - how do you fix that?

Part of Pro
42

What does the Dependency Inversion Principle mean, and can you give an example?

Part of Pro
43

How do you add a new payment type without editing the existing classes?

Part of Pro
44

What is inversion of control, and how does dependency injection relate to it?

Part of Pro
45

What are the downsides and hidden costs of dependency injection?

Part of Pro
46

How do you reduce coupling between two classes that know too much about each other?

Part of Pro
47

Can two objects that are not equal share the same hash code?

Part of Pro
48

Should an entity's id field take part in its equals method?

Part of Pro
49

What steps turn an ordinary class into a genuinely immutable one?

Part of Pro
50

Are immutable objects automatically thread safe, and what makes them so?

Part of Pro
51

Why do immutable objects make better hash map keys than mutable ones?

Part of Pro
52

When you pass an object to a method, is it by value or reference?

Part of Pro
53

What is the difference between a shallow copy and a deep copy of an object?

Part of Pro
54

What is the Singleton pattern, and what problems does it cause?

Part of Pro
55

In what order do field initialisers and constructors run when you create a subclass?

Part of Pro
56

How do SOLID principles relate to code maintainability and testability?

Part of Pro
57

What design patterns help achieve the Open/Closed Principle?

Part of Pro
58

What are the key rules for maintaining LSP compliance?

Part of Pro
59

How does ISP relate to cohesion and coupling?

Part of Pro
60

What's the difference between ISP and SRP?

Part of Pro
61

What is Dependency Injection and how does it relate to DIP?

Part of Pro
Expert 18
62

How do you keep a class immutable when one of its fields is mutable?

Part of Pro
63

Should equals accept subclasses in its type check, or require an exact class match?

Part of Pro
64

What breaks when equality and ordering comparison disagree about two objects?

Part of Pro
65

Do getters and setters really give you encapsulation, or just expose the fields?

Part of Pro
66

A class that fetches, processes and formats a report - which SOLID principles break?

Part of Pro
67

A method switching over a type code - which SOLID principles does that violate?

Part of Pro
68

How does a service locator compare with dependency injection?

Part of Pro
69

What are the benefits and potential drawbacks of strictly following SRP?

Part of Pro
70

How do you balance OCP with YAGNI (You Aren't Gonna Need It) principle?

Part of Pro
71

How do you identify and fix LSP violations in existing code?

Part of Pro
72

How do you apply DIP in scenarios without a DI container?

Part of Pro
73

What are the trade-offs of applying DIP extensively?

Part of Pro
74

How do SOLID principles interact with each other? Can following one principle violate another?

Part of Pro
75

How do you introduce SOLID principles to a legacy codebase?

Part of Pro
76

How do SOLID principles apply to microservices architecture?

Part of Pro
77

What metrics or code smells indicate SOLID principle violations?

Part of Pro
78

How do you balance SOLID principles with performance requirements?

Part of Pro
79

Can you provide an example of refactoring a complex class to follow all SOLID principles?

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for Object-Oriented Programming? Send them this set.
Pro · $10/mo

70 of 79 Object-Oriented Programming answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime

Change topic

Pick a different technology or stack. Your current topic stays put until you choose a new one.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git