This section of our 1000+ C# multiple choice questions focuses on method overriding in C# Programming Language.
1. Which keyword is used to declare a base class method while performing overriding of base class methods?
a) this
b) virtual
c) override
d) extend
2. The process of defining a method in a subclass having same name & type signature as a method in its superclass is known as?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
3. Which of the given modifiers can be used to prevent Method overriding?
a) Static
b) Constant
c) Sealed
d) final
4. Select the correct statement from the following?
a) Static methods can be a virtual method
b) Abstract methods can be a virtual method
c) When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overridden
d) We can override virtual as well as nonvirtual methods
5. Which of the following cannot be used to declare a class as a virtual?
a) Methods
b) Properties
c) Events
d) Fields
6. What will be the output of the following C# code?
class A
{
public int i;
public void display()
{
Console.WriteLine(i);
}
}
class B: A
{
public int j;
public void display()
{
Console.WriteLine(j);
}
}
class Program
{
static void Main(string[] args)
{
B obj = new B();
obj.i = 1;
obj.j = 2;
obj.display();
Console.ReadLine();
}
}
a) 0
b) 2
c) 1
d) Compile time error
2
7. What will be the output of the following C# code?
class A
{
public virtual void display()
{
Console.WriteLine("A");
}
}
class B: A
{
public override void display()
{
Console.WriteLine(" B ");
}
}
class Program
{
static void Main(string[] args)
{
A obj1 = new A();
B obj2 = new B();
A r;
r = obj1;
r.display();
r = obj2;
r.display();
Console.ReadLine();
}
}
a) A, A
b) B, B
c) Compile time error
d) A, B
A B
8. The modifier used to hide the base class methods is?
a) Virtual
b) New
c) Override
d) Sealed
9. To override a method in the subclass, the base class method should be defined as?
a) Virtual
b) Abstract
c) Override
d) All of the mentioned
10. What will be the output of the following C# code?
class a
{
public void fun()
{
Console.WriteLine("base method");
}
}
class b: a
{
public new void fun()
{
Console.WriteLine(" derived method ");
}
}
class Program
{
static void Main(string[] args)
{
b k = new b();
k.fun();
Console.ReadLine();
}
}
a) Base method
b) Derived method
c) Code runs successfully prints nothing
d) Compile time error
derived method