All subjectsFundamentals · 29 questions · 10 junior / 15 mid / 4 senior

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 quiz29 questions in the bank · 10 per round
Level
Question type
Mode

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.

  1. juniorConcepts

    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.

  2. juniorConcepts

    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.

  3. juniorPredict the output

    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.

  4. midPredict the output

    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.

  5. midPredict the output

    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.

  6. juniorConcepts

    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.

  7. juniorFind the bug

    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.

  8. midConcepts

    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.

  9. midConcepts

    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.

  10. midFind the bug

    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.

More subjects

Need software for your business instead? I'm an AI automation & full-stack developer — see what I build.

Chat on WhatsApp