Sample questions
Question 1Easy
What is the default access level of a class member (field or method) declared without a modifier?
- public
- internal
- private
- protected
Answer: C. private
Explanation
Members default to private, while top-level types default to internal.
Step-by-step solution
- Member has no modifier.
- 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?
- abstract
- sealed
- readonly
- 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
- Look for the keyword that blocks inheritance.
- sealed marks a class as the last class in its branch.
- 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());
- A
- B
- Compile error
- 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
- Name() in B1 uses new, so it is a separate method, not an override.
- x is declared as A1, so the compiler binds the call to A1.Name.
- 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?
- A struct is a reference type allocated only on the heap
- A struct can inherit from any class
- A struct always needs the new keyword to be used
- 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
- Value types hold their data directly.
- Assignment duplicates the value.
- 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());
- ...
- Animal
- Compile error
- 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
- a is declared as Animal but holds a new Dog.
- Sound() is virtual, so C# picks the override at run time.
- 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"; }
}
- PI P C
- C P PI
- P PI C
- 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
- C1() calls base(5), which selects P1(int).
- P1(int) calls this(), which runs P1() and logs P.
- 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();
- It prints "moving" to the console and the call succeeds
- Compile error: Car has no accessible Move method
- It compiles, then throws a missing-method exception at run time
- 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
- Move is implemented explicitly as IMover.Move.
- Explicit members are not public members of Car.
- 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 →