OOP Questions with Answers

7 practice questions on OOP (C#) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What is the default access level of a class member (field or method) declared without a modifier?

  1. public
  2. internal
  3. private
  4. protected

Answer: C. private

Explanation

Members default to private, while top-level types default to internal.

Step-by-step solution

  1. Member has no modifier.
  2. Language applies private.

Why the other options are wrong

  • A. Public must be written explicitly for members.
  • B. Internal is the default for top-level types, not members.
  • D. Protected must be written explicitly.

Question 2Easy

Which keyword stops other classes from inheriting from a class?

  1. abstract
  2. sealed
  3. readonly
  4. virtual

Answer: B. sealed

Explanation

The sealed keyword stops a class from being used as a base class. A class that tries to inherit from a sealed class does not compile.

Step-by-step solution

  1. Look for the keyword that blocks inheritance.
  2. sealed marks a class as the last class in its branch.
  3. Any class that inherits from it fails to compile.

Why the other options are wrong

  • A. abstract classes exist to be inherited, so the keyword does the opposite.
  • C. readonly applies to fields, not to classes.
  • D. virtual lets a member be overridden; it does not block inheritance.

Tip: Think: sealed means no more children.

Question 3Medium

What is printed?

class A1 { public string Name() => "A"; }
class B1 : A1 { public new string Name() => "B"; }

A1 x = new B1();
Console.WriteLine(x.Name());
  1. A
  2. B
  3. Compile error
  4. BA

Answer: A. A

Explanation

The new modifier hides Name() instead of overriding it. Because x is declared as A1, the call is bound to A1.Name and prints A.

Step-by-step solution

  1. Name() in B1 uses new, so it is a separate method, not an override.
  2. x is declared as A1, so the compiler binds the call to A1.Name.
  3. The output is A.

Why the other options are wrong

  • B. new hides the member but does not override it, so a variable of type A1 still calls A1.Name.
  • C. Hiding a member with the new keyword is allowed and compiles.
  • D. Name() returns one string, so nothing is joined together.

Tip: new hides by the declared type; override changes behaviour for the real object.

Question 4Medium

Which statement about struct versus class in C# is correct?

  1. A struct is a reference type allocated only on the heap
  2. A struct can inherit from any class
  3. A struct always needs the new keyword to be used
  4. A struct is a value type, so assignment copies its data

Answer: D. A struct is a value type, so assignment copies its data

Explanation

Assigning a struct to another variable copies its contents, whereas assigning a class copies only the reference.

Step-by-step solution

  1. Value types hold their data directly.
  2. Assignment duplicates the value.
  3. Changes to the copy do not affect the original.

Why the other options are wrong

  • A. Structs are value types, not reference types.
  • B. Structs cannot inherit from classes; they derive from ValueType.
  • C. Structs can be created without new, if all fields are assigned.

Question 5Medium

What is printed?

class Animal { public virtual string Sound() => "..."; }
class Dog : Animal { public override string Sound() => "Woof"; }

Animal a = new Dog();
Console.WriteLine(a.Sound());
  1. ...
  2. Animal
  3. Compile error
  4. Woof

Answer: D. Woof

Explanation

Sound is virtual in Animal and overridden in Dog. The variable a has type Animal, but the object is a Dog, so the Dog version runs.

Step-by-step solution

  1. a is declared as Animal but holds a new Dog.
  2. Sound() is virtual, so C# picks the override at run time.
  3. Dog.Sound returns "Woof".

Why the other options are wrong

  • A. The override in Dog replaces the base version, so the base text is not printed.
  • B. Animal is a class name, not the value that Sound() returns.
  • C. Overriding a virtual method is valid C# and compiles.

Tip: The declared type picks the name; for a virtual method, the real object picks the code.

Question 6Medium

What does P1.Log contain after new C1() runs?

class P1 {
    public static string Log = "";
    public P1() { Log += "P "; }
    public P1(int x) : this() { Log += "PI "; }
}
class C1 : P1 {
    public C1() : base(5) { Log += "C"; }
}
  1. PI P C
  2. C P PI
  3. P PI C
  4. P C PI

Answer: C. P PI C

Explanation

new C1() calls C1(), which calls base P1(int). That calls this() to run P1() first, which logs P. Then P1(int) logs PI, and the C1 body logs C last.

Step-by-step solution

  1. C1() calls base(5), which selects P1(int).
  2. P1(int) calls this(), which runs P1() and logs P.
  3. Back in P1(int), PI is logged. C1 then logs C.

Why the other options are wrong

  • A. The no-argument constructor is reached through this(), so P is logged before PI.
  • B. The derived body runs after the base constructor chain, so C is logged last.
  • D. C is logged only after the whole base chain finishes, so it cannot come before PI.

Tip: Constructor chains run base code first, and this() runs before the rest of the current constructor.

Question 7Hard

What happens when this code is compiled?

interface IMover { void Move(); }
class Car : IMover {
    void IMover.Move() { Console.WriteLine("moving"); }
}

var c = new Car();
c.Move();
  1. It prints "moving" to the console and the call succeeds
  2. Compile error: Car has no accessible Move method
  3. It compiles, then throws a missing-method exception at run time
  4. It compiles, and the call silently does nothing at all

Answer: B. Compile error: Car has no accessible Move method

Explanation

An explicit interface implementation is only visible through a variable of the interface type. Calling c.Move() on a Car variable fails to compile.

Step-by-step solution

  1. Move is implemented explicitly as IMover.Move.
  2. Explicit members are not public members of Car.
  3. c has type Car, so c.Move() has no matching method and the build fails.

Why the other options are wrong

  • A. An explicit interface implementation is hidden from Car, so c.Move() cannot reach it.
  • C. The problem is found at compile time, so the program never runs.
  • D. The method body does print, and the call itself does not compile.

Tip: Cast to the interface (IMover m = c;) to call an explicit implementation.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics