Multiple choice

Identify the output of the code given below:

using System;
class Demo1 {
 public void X() {
  Console.WriteLine("2");
 }
}
class Demo2: Demo1 {
 public new void X() {
  Console.WriteLine("4");
 }
}
class Test {
 static void Main(string[] args) {
  Demo1 s = new Demo1();
  s.X();
  Demo1 a = new Demo2();
  a.X();
 }
}

  1. 2 2

  2. 2 4

  3. 4 4

  4. Null

  5. It will not compile.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The method in the class Demo1 will be called twice, as new keyword is used for method hiding of the child class.