Multiple choice practice

OOP Concepts MCQ questions

Take a short practice test with instant feedback, then revise every question with its answer and the reason behind it.

Questions
124
From lessons
104
Your last test
—

Practice test

OOP Concepts practice testTrue or false Question 1 of 10

Depending on interfaces makes it easy to use fake implementations in unit tests.

Score so far: 0/0

All questions with answers

  1. Which OOP principle hides internal state behind methods?

    • AInheritance
    • BEncapsulation
    • CPolymorphism
    • DOverloading
    Show answer

    Answer: B. Encapsulation

    Encapsulation keeps data private and exposes controlled operations.

  2. An object is…

    • AA blueprint
    • BAn instance of a class
    • CA method
    • DA namespace
    Show answer

    Answer: B. An instance of a class

    The class is the blueprint; each object is a concrete instance.

  3. Which relationship does inheritance represent?

    • Ahas-a
    • Bis-a
    • Cuses-a
    • Dpart-of
    Show answer

    Answer: B. is-a

    A Dog is an Animal. Composition is has-a.

  4. Which feature lets one interface have many implementations called the same way?

    • AEncapsulation
    • BPolymorphism
    • CAbstraction
    • DStatic binding
    Show answer

    Answer: B. Polymorphism

    Polymorphism lets code call shape.Area() without knowing the concrete shape.

  5. Method overloading is resolved…

    • AAt runtime
    • BAt compile time
    • CBy the garbage collector
    • DBy the database
    Show answer

    Answer: B. At compile time

    The compiler picks the overload from the argument types.

  6. Which can you NOT instantiate directly?

    • AA sealed class
    • BAn abstract class
    • CA static method
    • DA struct
    Show answer

    Answer: B. An abstract class

    Abstract classes are meant to be inherited.

  7. How many classes can a C# class inherit from?

    • AOne
    • BTwo
    • CUnlimited
    • DNone
    Show answer

    Answer: A. One

    C# supports single class inheritance but multiple interfaces.

  8. Which access modifier allows access from subclasses only (plus the class itself)?

    • Aprivate
    • Bpublic
    • Cprotected
    • Dinternal
    Show answer

    Answer: C. protected

    protected members are visible to the class and derived classes.

  9. What does the S in SOLID stand for?

    • ASubstitution
    • BSingle Responsibility
    • CSeparation of Concerns
    • DSimplicity
    Show answer

    Answer: B. Single Responsibility

    A class should have one reason to change.

  10. Which principle says subtypes must be usable in place of their base type?

    • AOpen/Closed
    • BLiskov Substitution
    • CInterface Segregation
    • DDependency Inversion
    Show answer

    Answer: B. Liskov Substitution

    Derived classes must honour the base class’s contract.

  11. Which pattern ensures a class has only one instance?

    • AFactory
    • BSingleton
    • CObserver
    • DStrategy
    Show answer

    Answer: B. Singleton

    In modern apps a DI container’s singleton lifetime is usually preferred.

  12. Which pattern swaps an algorithm at runtime through a common interface?

    • AStrategy
    • BSingleton
    • CRepository
    • DAdapter
    Show answer

    Answer: A. Strategy

    For example different IShippingCalculator implementations.

  13. Which pattern notifies subscribers when state changes?

    • AObserver
    • BFactory
    • CDecorator
    • DBuilder
    Show answer

    Answer: A. Observer

    Events and pub/sub systems are observers.

  14. What is wrong with this design?

    C# Illustrative
    public class ReportService
    {
        private readonly SmtpEmailSender _email = new SmtpEmailSender("smtp.example.com");
    }
    
    • ANothing
    • BReportService creates its own dependency, so it cannot be tested with a fake
    • CConstructors cannot create objects
    • DIt violates encapsulation by being public
    Show answer

    Answer: B. ReportService creates its own dependency, so it cannot be tested with a fake

    Inject an IEmailSender instead of calling new inside the class.

  15. In composition, the part’s lifetime…

    • AIs independent of the whole
    • BDepends on the whole
    • CIs static
    • DIs longer than the whole
    Show answer

    Answer: B. Depends on the whole

    An OrderLine does not exist without its Order.

  16. Which keyword lets a C# subclass replace a virtual method?

    • Anew
    • Boverride
    • Cvirtual
    • Dbase
    Show answer

    Answer: B. override

    override replaces the implementation polymorphically; new only hides it.

  17. An interface in C# can contain…

    • AInstance fields
    • BConstructors
    • CMethod signatures (and default implementations)
    • DPrivate state only
    Show answer

    Answer: C. Method signatures (and default implementations)

    Interfaces describe members; C# 8+ allows default implementations but not instance fields.

  18. What does a constructor return?

    • Avoid
    • BThe class type implicitly — it initialises the new object
    • CA boolean
    • DNothing can call a constructor
    Show answer

    Answer: B. The class type implicitly — it initialises the new object

    Constructors have no declared return type; new returns the created object.

  19. Which UML symbol marks a private member?

    • A+
    • B-
    • C#
    • D~
    Show answer

    Answer: B. -

    + public, - private, # protected, ~ package/internal.

  20. “Favour composition over inheritance” mainly improves…

    • AExecution speed
    • BFlexibility and loose coupling
    • CMemory usage
    • DCompile time
    Show answer

    Answer: B. Flexibility and loose coupling

    Behaviour can be combined and swapped without deep hierarchies.

  21. What is the relationship between a class and an object?

    • AThey are the same thing
    • BAn object is a blueprint; a class is an instance
    • CA class is a blueprint; an object is an instance of it
    • DA class can only create one object
    Show answer

    Answer: C. A class is a blueprint; an object is an instance of it

    Classes describe structure and behaviour; objects are concrete instances created from them.

    From the lesson: Classes and Objects
  22. What is logged?

    JavaScript Illustrative
    class Counter { constructor() { this.n = 0; } inc() { this.n++; } }
    const a = new Counter();
    const b = new Counter();
    a.inc(); a.inc(); b.inc();
    console.log(a.n, b.n);
    
    • A3 3
    • B2 1
    • C1 2
    • D0 0
    Show answer

    Answer: B. 2 1

    Each object has its own n. a was incremented twice, b once.

    From the lesson: Classes and Objects
  23. What is the main goal of encapsulation?

    • AMake code run faster
    • BHide internal state and control how it changes
    • CAllow a class to inherit from many classes
    • DCreate objects without classes
    Show answer

    Answer: B. Hide internal state and control how it changes

    Encapsulation protects an object’s internal data and only allows changes through controlled methods.

    From the lesson: Encapsulation
  24. In JavaScript, a field declared as #secret can be read from outside the class with obj.#secret.

    • ATrue
    • BFalse
    Show answer

    Answer: B. False

    Private fields are only accessible inside the class body; outside access is a syntax error.

    From the lesson: Encapsulation
  25. What is logged?

    JavaScript Illustrative
    class Wallet {
      #money = 100;
      get money() { return this.#money; }
    }
    console.log(Object.keys(new Wallet()));
    
    • A[ 'money' ]
    • B[]
    • C[ '#money' ]
    • Dundefined
    Show answer

    Answer: B. []

    Private fields are not properties, and the getter lives on the prototype, so the object has no own keys.

    From the lesson: Encapsulation