C# Questions & Answers – Method Overriding

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 overidding of base class methods?
a) this
b) virtual
c) override
d) extend
View Answer

Answer :b
Explanation : None.

2. The process of defining a method in subclass having same name & type signature as a method in its superclass?
a) Method overloading
b) Method overriding
c) Method hiding
d) None of the mentioned
View Answer

Answer :b
Explanation : None.

3. Which of given modifiers can be used to prevent Method overriding?
a) Static
b) Constant
c) Sealed
d) final
View Answer

Answer :c
Explanation :When an instance method deceleration includes sealed modifier,the method is said to be sealed method.It means a derived class cannot override this method.

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 types signatures of the override method must be the same as the virtual method that is being overriden
d) We can override virtual as well as non virtual methods
View Answer

Answer :c
Explanation :None.

5. Which of the following can be used to declared as a virtual in a class?
a) Methods
b) Properties
c) Events
d) Fields
View Answer

Answer : a, b, c
Explanation :None.

6. What will be the output for given set of code?

  1.  class A
  2.  {
  3.      public int i;
  4.      public void display() 
  5.      {
  6.          Console.WriteLine(i);
  7.      }
  8.  }    
  9.  class B: A 
  10.  {
  11.      public int j;
  12.      public void display() 
  13.      {
  14.          Console.WriteLine(j);
  15.      }
  16.  }    
  17.  class Program
  18.  {
  19.      static void Main(string[] args)
  20.      {
  21.          B obj = new B();
  22.          obj.i = 1;
  23.          obj.j = 2;
  24.          obj.display();
  25.          Console.ReadLine();
  26.      }
  27.  }

a) 0
b) 2
c) 1
d) Compile time error
View Answer

Answer :b
Explanation :When method display() is called using objects of class ‘B’. The method ‘display()’ for class ‘B’ is called instead of class ‘A’ as class ‘B’ is inherited by class ‘A’.
Output :2

7. What will be the output for given set of code?

  1.  class A 
  2.  {
  3.      public virtual void display() 
  4.      {
  5.          Console.WriteLine("A");
  6.      }    
  7.  }    
  8.  class B: A 
  9.  {
  10.     public override void display() 
  11.     {
  12.         Console.WriteLine(" B ");
  13.     } 
  14.  }    
  15. class Program
  16. {
  17.     static void Main(string[] args)
  18.     {
  19.         A obj1 = new A();
  20.         B obj2 = new B();
  21.         A r;
  22.         r = obj1;
  23.         r.display();
  24.         r = obj2;
  25.         r.display();     
  26.         Console.ReadLine();
  27.     }
  28. }

a) A, A
b) B, B
c) Compile time error
d) A, B
View Answer

Answer :d
Explanation :The method overidding procedure has been used to produce the values from two display().
Output : A B

8. The modifier used to hide the base class methods?
a) Virtual
b) New
c) Override
d) Sealed
View Answer

Answer :b
Explanation :Used in condition when we cannot use virtual to override a base class method hence then we use ‘New’ to hide the base class methods and redefine the method defined in sub class.

9. To override a method in subclass in baseclass method should be defined as?
a) virtual
b) abstract
c) Static
d) Override
View Answer

Answer : a, b, d
Explanation :None.

10. What will be the output for given set of code?

  1.  class a
  2.  {
  3.      public  void fun()
  4.      {
  5.          Console.WriteLine("base method");
  6.      }
  7.  }
  8.  class b: a
  9.  {
  10.      public new void fun()
  11.      {
  12.          Console.WriteLine(" derived method ");
  13.      }
  14.  }
  15.  class Program
  16.  {
  17.      static void Main(string[] args)
  18.      {
  19.          b k = new b();
  20.          k.fun();
  21.          Console.ReadLine();
  22.      }
  23.  }

a) base method
b) derived method
c) Code run successfully print nothing
d) Compile time error
View Answer

Answer :b
Explanation : Use of ‘new’ modifier hide the inherited member i.e it makes only inherited member inaccessible in derived class and hence calls suitable method().
Output :derived method

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Reference Books in C# Programming Language.

To practice all features of C# programming language, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry