Home Subjects C# Programming OOP in C#

C# Programming
OOP in C#

C# and .NET for campus placement

25 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–25 of 25
Topics in C# Programming
Q.21 Hard OOP in C#
In C#, what happens when you apply the 'static' modifier to a method inside an interface (C# 11+)?
A The method must be implemented by all implementing classes
B The method provides a default implementation and can be called without instantiation
C It causes a compile-time error
D The method becomes abstract and requires override
Correct Answer:  B. The method provides a default implementation and can be called without instantiation
EXPLANATION

C# 11 allows static abstract members in interfaces. Static methods in interfaces provide default implementations and can be called via the interface type.

Test
Q.22 Hard OOP in C#
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();
A Base
B Derived
C BaseDerived
D Compilation Error
Correct Answer:  C. BaseDerived
EXPLANATION

The 'base' keyword calls the parent class method first, printing 'Base', then the derived method prints 'Derived'. Output: 'BaseDerived'.

Test
Q.23 Hard OOP in C#
What will happen if you try to override a non-virtual method?
A It will override successfully
B Compilation error - cannot override non-virtual method
C It will use method hiding instead
D Runtime exception
Correct Answer:  B. Compilation error - cannot override non-virtual method
EXPLANATION

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.

Test
Q.24 Hard OOP in C#
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();
A C
B Compilation Error
C I1
D I2
Correct Answer:  A. C
EXPLANATION

A single method implementation satisfies both interface contracts. The method is called and prints 'C'.

Test
Q.25 Hard OOP in C#
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();
A A
B B
C AB
D Compilation Error
Correct Answer:  A. A
EXPLANATION

Using 'new' keyword hides the method. Since reference is of type A, A's Test() is called. Output is 'A'.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips