Object-Oriented Programming quiz and interview questions
Object-oriented programming comes up in almost every developer interview and exam. This quiz covers the four pillars — encapsulation, inheritance, polymorphism and abstraction — plus SOLID, with C# code you have to read and predict.
Object-Oriented Programming: key concepts
- Encapsulation
- Keep data private and expose behaviour through methods and properties, so an object can't be put into an invalid state from outside.
- Inheritance
- A class can reuse and extend another class. The base constructor always runs before the derived constructor.
- Polymorphism
- One call, different behaviour. With virtual/override, the method of the object's real type runs, even through a base-class variable. Overloading is compile-time; overriding is run-time.
- Abstraction
- Abstract classes and interfaces describe what something does without fixing how. You can't create an instance of an abstract class.
- Access modifiers
- private: only this class. protected: this class and derived classes. public: everyone. internal: the same assembly.
- SOLID
- Single responsibility, Open/closed, Liskov substitution, Interface segregation and Dependency inversion — five rules that keep classes easy to change.
All 29 Object-Oriented Programming questions with answers
Try each one first, then open the answer. Making a class test? Use the test maker.
What is encapsulation?
- A. Inheriting from many classes
- B. Hiding an object's data and exposing it only through methods/properties
- C. Writing many constructors
- D. Converting a class to an interface
Show answer
B. Hiding an object's data and exposing it only through methods/properties — Encapsulation bundles data and behaviour and protects the data (private fields) so it can only change in controlled ways.
Which can an abstract class have that an interface (classic definition) cannot?
- A. Method signatures
- B. Fields and constructors
- C. Public members
- D. A name
Show answer
B. Fields and constructors — Abstract classes can hold state (fields) and constructors and share implementation. Interfaces describe a contract that many unrelated classes can implement.
What does this print?
class Animal { public virtual string Speak() => "..."; } class Dog : Animal { public override string Speak() => "Woof"; } Animal a = new Dog(); Console.WriteLine(a.Speak());- A. ...
- B. Woof
- C. Compile error
- D. Animal
Show answer
B. Woof — Speak is virtual and Dog overrides it. The real object is a Dog, so Dog.Speak runs — that's run-time polymorphism.
What does this print?
class A { public void Show() => Console.Write("A"); } class B : A { public new void Show() => Console.Write("B"); } A obj = new B(); obj.Show();- A. A
- B. B
- C. AB
- D. Compile error
Show answer
A. A — new hides the method instead of overriding it. The call is chosen by the variable type (A), so A.Show runs.
What does this print?
class Base { public Base() { Console.Write("Base "); } } class Child : Base { public Child() { Console.Write("Child"); } } new Child();- A. Child
- B. Child Base
- C. Base Child
- D. Base
Show answer
C. Base Child — The base-class constructor always runs first, then the derived constructor.
What is the difference between overloading and overriding?
- A. They're the same
- B. Overloading: same name, different parameters (compile-time). Overriding: a subclass replaces a virtual method (run-time)
- C. Overloading needs inheritance
- D. Overriding changes the parameters
Show answer
B. Overloading: same name, different parameters (compile-time). Overriding: a subclass replaces a virtual method (run-time) — Overloading is resolved by the compiler from the arguments. Overriding is resolved at run time from the object's actual type.
Why won't this compile?
abstract class Shape { } var s = new Shape();- A. Missing semicolon
- B. You can't create an instance of an abstract class
- C. Shape needs a constructor
- D. var isn't allowed here
Show answer
B. You can't create an instance of an abstract class — Abstract classes exist to be inherited. Create a concrete subclass (e.g. Circle : Shape) and instantiate that.
The Liskov Substitution Principle says…
- A. Classes should be small
- B. A subclass must be usable anywhere its base class is expected without breaking behaviour
- C. Use interfaces everywhere
- D. Avoid inheritance
Show answer
B. A subclass must be usable anywhere its base class is expected without breaking behaviour — If code works with a base type, it must keep working with any subtype. The classic violation is a Square that breaks Rectangle's width/height rules.
What does "favour composition over inheritance" mean?
- A. Never use classes
- B. Build behaviour by combining objects (has-a) instead of deep class hierarchies (is-a)
- C. Use more inheritance
- D. Put everything in one class
Show answer
B. Build behaviour by combining objects (has-a) instead of deep class hierarchies (is-a) — Composition keeps classes loosely coupled and easier to change; deep inheritance trees break when the base class changes.
Why won't this compile?
class Account { private decimal balance; } class Savings : Account { public void Add(decimal x) => balance += x; }- A. decimal can't use +=
- B. balance is private, so the derived class can't access it
- C. Savings needs a constructor
- D. Expression-bodied methods can't change fields
Show answer
B. balance is private, so the derived class can't access it — private members are visible only inside Account. Make it protected, or better, expose a protected/public method that changes it safely.
The Open/Closed Principle says a class should be…
- A. Open to modification, closed to extension
- B. Open for extension, closed for modification
- C. Always sealed
- D. Public and static
Show answer
B. Open for extension, closed for modification — Add new behaviour by adding code (new classes, strategies) rather than editing tested code — e.g. a new PaymentMethod class instead of another if/else.
Dependency Inversion means high-level code should depend on…
- A. Concrete classes
- B. Abstractions (interfaces), not concrete implementations
- C. Static helpers
- D. The database
Show answer
B. Abstractions (interfaces), not concrete implementations — OrderService should depend on IEmailSender, not SmtpEmailSender. Then you can swap or mock the implementation — this is what dependency injection enables.
What is the relationship between a class and an object?
- A. They're the same thing
- B. A class is a blueprint; an object is an instance created from it
- C. An object is a blueprint for classes
- D. Classes exist only at run time
Show answer
B. A class is a blueprint; an object is an instance created from it — Car is the class; myCar = new Car() is an object with its own field values.
What does this print?
class Counter { public static int Count; public Counter() { Count++; } } new Counter(); new Counter(); new Counter(); Console.WriteLine(Counter.Count);- A. 1
- B. 0
- C. 3
- D. Compile error
Show answer
C. 3 — A static field belongs to the class, not to each object, so all three constructors increase the same Count.
What are the four pillars of OOP?
- A. Classes, objects, methods, fields
- B. Encapsulation, abstraction, inheritance, polymorphism
- C. SOLID, DRY, KISS, YAGNI
- D. Public, private, protected, internal
Show answer
B. Encapsulation, abstraction, inheritance, polymorphism — They describe how objects hide data, expose simple interfaces, reuse code and behave differently through one interface.
How is abstraction different from encapsulation?
- A. They're the same
- B. Abstraction shows only what an object does; encapsulation hides how it stores and protects its data
- C. Encapsulation is about interfaces only
- D. Abstraction means private fields
Show answer
B. Abstraction shows only what an object does; encapsulation hides how it stores and protects its data — A car's pedals are the abstraction; the engine being sealed under the bonnet is encapsulation.
What is the difference between compile-time and run-time polymorphism?
- A. None
- B. Compile-time = method overloading (chosen by the compiler); run-time = overriding virtual methods (chosen by the object's real type)
- C. Run-time = overloading
- D. Compile-time needs interfaces
Show answer
B. Compile-time = method overloading (chosen by the compiler); run-time = overriding virtual methods (chosen by the object's real type) — Overloads are resolved from argument types; overrides from the actual object at run time.
What does "favour composition over inheritance" mean?
- A. Never use inheritance
- B. Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a)
- C. Use more interfaces only
- D. Put everything in one class
Show answer
B. Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a) — A Car has an Engine — swapping engines is easier than subclassing PetrolCar, ElectricCar, HybridCar…
What is the difference between aggregation and composition?
- A. None
- B. In composition the part can't exist without the whole (House–Room); in aggregation it can (Team–Player)
- C. Aggregation is inheritance
- D. Composition is only for interfaces
Show answer
B. In composition the part can't exist without the whole (House–Room); in aggregation it can (Team–Player) — Delete the house and its rooms go too; disband a team and the players still exist.
What does this print?
class Animal { public virtual string Speak() => "..."; } class Dog : Animal { public override string Speak() => "Woof"; } class Cat : Animal { public override string Speak() => "Meow"; } var pets = new Animal[] { new Dog(), new Cat(), new Animal() }; foreach (var p in pets) Console.Write(p.Speak() + " ");- A. ... ... ...
- B. Woof Meow ...
- C. Woof Meow Woof
- D. Compile error
Show answer
B. Woof Meow ... — Each call uses the real object's override — classic run-time polymorphism.
What does this print?
class Base { public Base() { Console.Write("B"); } } class Child : Base { public Child() { Console.Write("C"); } } new Child();- A. C
- B. CB
- C. BC
- D. B
Show answer
C. BC — The base constructor always runs first, then the derived constructor.
What does the base keyword let a derived class do?
- A. Delete the base class
- B. Call the base class's constructor or its version of an overridden method
- C. Make members private
- D. Create a new base class
Show answer
B. Call the base class's constructor or its version of an overridden method — public override string Describe() => base.Describe() + " (on sale)";
Which access modifier lets a member be used only inside its class and derived classes?
- A. private
- B. protected
- C. internal
- D. public
Show answer
B. protected — internal = same assembly; protected internal = either; private protected = derived classes in the same assembly.
What is cohesion, and why should it be high?
- A. How many classes there are
- B. How closely a class's members belong to one purpose — high cohesion makes classes easier to understand and change
- C. How classes depend on each other
- D. The number of interfaces
Show answer
B. How closely a class's members belong to one purpose — high cohesion makes classes easier to understand and change — An InvoiceCalculator that also sends emails and logs to disk has low cohesion.
Why aim for loose coupling between classes?
- A. To use less memory
- B. Changes in one class are less likely to break others, and parts can be tested or replaced alone
- C. To make code run faster
- D. It's required by C#
Show answer
B. Changes in one class are less likely to break others, and parts can be tested or replaced alone — Depending on an IEmailSender interface instead of a concrete SmtpSender keeps classes loosely coupled.
This design breaks the Liskov Substitution Principle. Why?
class Bird { public virtual void Fly() { /* ... */ } } class Penguin : Bird { public override void Fly() => throw new NotSupportedException(); }- A. Penguin should be sealed
- B. Code that works with any Bird crashes when given a Penguin — not every bird can fly
- C. Fly must be abstract
- D. Nothing is wrong
Show answer
B. Code that works with any Bird crashes when given a Penguin — not every bird can fly — Split the abstraction, e.g. Bird plus an IFlyingBird interface for birds that can fly.
What is an anemic domain model?
- A. A small database
- B. Classes that hold only data while all behaviour lives in separate service classes
- C. A model with no properties
- D. A model that uses inheritance heavily
Show answer
B. Classes that hold only data while all behaviour lives in separate service classes — A rich model puts rules (order.AddItem checks stock) inside the entity itself.
What does "program to an interface, not an implementation" mean?
- A. Always write interfaces first
- B. Declare variables and parameters as abstractions (IRepository), so concrete classes can be swapped
- C. Never use classes
- D. Only use abstract classes
Show answer
B. Declare variables and parameters as abstractions (IRepository), so concrete classes can be swapped — This is what makes dependency injection and mocking in tests possible.
What does this print?
interface IGreeter { string Hi() => "Hi from interface"; } class Greeter : IGreeter { } IGreeter g = new Greeter(); Console.WriteLine(g.Hi());- A. Compile error
- B. Hi from interface
- C. Empty line
- D. NullReferenceException
Show answer
B. Hi from interface — C# 8 default interface methods: the class gets the body, but it's callable only through the interface type.
More subjects
Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.