In C#, what is the default access modifier for class members?
Answer: B
The default access modifier for class members in C# is 'private', which restricts access to within the class only.
Q.2Easy
Which keyword is used to create a derived class in C#?
Answer: C
In C#, the colon (:) is used to indicate inheritance. For example, 'class Child : Parent' creates a derived class.
Q.3Medium
What will be the output of the following code?
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
Answer: B
Due to polymorphism, the overridden method in class B is called, not the virtual method in class A. Output is 'B'.
Q.4Medium
What is the difference between 'new' keyword and 'override' keyword in C#?
Answer: A
'new' keyword hides the base class method (method hiding), while 'override' provides true polymorphic behavior by replacing the method.
Q.5Medium
In C#, which interface is used to implement custom iteration logic?
Answer: A
IEnumerable interface in C# is used to provide iteration capability through the GetEnumerator() method.
Advertisement
Q.6Easy
In C#, can a class inherit from multiple classes?
Answer: B
C# does not support multiple class inheritance to avoid the diamond problem. However, a class can implement multiple interfaces.
Q.7Hard
What is the output of the following?
class A { public void Test() { Console.WriteLine("A"); } }
class B : A { public new void Test() { Console.WriteLine("B"); } }
A a = new B();
a.Test();
Answer: A
Using 'new' keyword hides the method. Since reference is of type A, A's Test() is called. Output is 'A'.
Q.8Medium
Which of the following is true about properties in C#?
Answer: C
Properties in C# allow controlled access to fields with validation logic while maintaining clean, intuitive syntax like field access.
Q.9Medium
What is the purpose of the 'sealed' keyword in C#?
Answer: A
The 'sealed' keyword prevents a class from being inherited or a method from being overridden in derived classes.
Q.10Hard
Consider the following code. What will be the output?
interface I1 { void Show(); }
interface I2 { void Show(); }
class C : I1, I2 { public void Show() { Console.WriteLine("C"); } }
C obj = new C();
obj.Show();
Answer: A
A single method implementation satisfies both interface contracts. The method is called and prints 'C'.
Q.11Medium
In C#, what is the difference between a class and a struct?
Answer: A
Classes are reference types stored on heap, structs are value types stored on stack. This affects memory management and behavior.
Q.12Hard
What will happen if you try to override a non-virtual method?
Answer: B
To override a method, it must be marked as 'virtual' in the base class. Attempting to override a non-virtual method causes a compilation error.
Q.13Medium
Which of the following represents proper use of constructor chaining in C#?
Answer: B
Constructor chaining uses 'this()' to call another constructor in the same class. Option B shows proper syntax for calling a parameterless constructor.
Q.14Hard
What is the output of the following code?
class Base { public virtual void Method() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Method() { base.Method(); Console.WriteLine("Derived"); } }
Derived d = new Derived();
d.Method();
Answer: C
The 'base' keyword calls the parent class method first, printing 'Base', then the derived method prints 'Derived'. Output: 'BaseDerived'.
Q.15Medium
In C# 2024-25, which feature allows defining read-only properties with automatic backing fields?
Answer: A
C# auto-properties with 'init' accessor allow creating read-only properties that can only be set during initialization, improving encapsulation.
Q.16Medium
In C# 11, which feature allows you to define required members in a class?
Answer: A
C# 11 introduced the 'required' keyword that enforces callers to initialize specific members during object instantiation.
Q.17Medium
What is the output when you access a property marked with 'init' accessor after object initialization in C#?
Answer: B
The 'init' accessor (introduced in C# 9) allows setting the property only during initialization. Any modification attempt post-initialization causes a compile-time error.
Q.18Easy
Which of the following correctly demonstrates an abstract class with an abstract method in C#?
Answer: A
An abstract class uses the 'abstract' keyword, and abstract methods must be declared without implementation inside abstract classes.
Q.19Hard
In C#, what happens when you apply the 'static' modifier to a method inside an interface (C# 11+)?
Answer: B
C# 11 allows static abstract members in interfaces. Static methods in interfaces provide default implementations and can be called via the interface type.
Q.20Medium
What is the primary difference between method overloading and method overriding in C#?
Answer: A
Overloading creates multiple methods with the same name but different parameters. Overriding replaces a base class method in a derived class.