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

Linh Chi

New member
What will be the output of the following C# code?
Code:
namespace ConsoleApplication4
{   
    abstract class A 
    {
        public int i;
        public abstract void display();
    }    
    class B: A 
    {
        public  int j;
        public int sum;
        public override void display() 
        {
            sum = i + j;
            Console.WriteLine(+i + "\n" + +j);
            Console.WriteLine("sum is:" +sum);
        }
    }    
    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.i = 2;
            B obj1 = new B();
            obj1.j = 10;
            obj.display();
            Console.ReadLine();
        }
    }
}
A.
2,
Code:
 1
0
12
B.
0,
Code:
 1
0
10
C.
2, 0
2
D.
0, 0
0
 
Answer: c
Explanation: Abstract method implementation is processed in subclass ‘B’. Also the object ‘obj’ of abstract class ‘A’ initializes value of i as 2. The object of class ‘B’ also initializes value of j as
Code:
 1
0. Since, the method display() is called using object of class A which is ‘obj’ and hence i = 2 whereas j = 0. So, sum = 2.
Output :
Code:
         2  0
         sum is : 2
 

Tìm kiếm

Back
Top