What will be the output of the following C# code? [code] namespace ConsoleApplication4 { abstract class A { public

What will be the output of the following C# code?
Code:
 namespace ConsoleApplication4
 {
     abstract class A
     {
         public int i ;
         public int j ;
         public abstract void display();
     }    
     class B: A    
     {
         public int j = 5;
         public override void display() 
         {
             this.j = 3;
             Console.WriteLine(i + "  " + j);
         }
     }    
     class Program
     {
         static void Main(string[] args)
         {
             B obj = new B();
             obj.i = 1;
             obj.display();     
             Console.ReadLine();
         }
     }
 }
A.
Code:
 1
, 5
B. 0, 5
C.
Code:
 1
, 0
D.
Code:
1, 3
 
Answer: d
Explanation: obj.i =
Code:
 1
initializes value of i as
Code:
 1
as it is the abstract member of abstract class ‘A’. Now,’j’ is also a same member as class ‘A’. Since it is initialized the value of 5 when declared in subclass. But since abstract method is redefined in subclass using ‘this’ keyword as this. j = 3, method will execute only abstract class member ‘j’ not subclass ‘B’ own defined data member ‘j’.
Output :
Code:
1, 3
 

Tìm kiếm

Back
Top