Need a Translation?

Guess the correct output - OOPS Inheritance (Base Class, Sub-Class)

class A
{
    B objBA = new B();
    public A()
    {
        Console.Write("A");
    }
}

class B
{
    public B()
    {
        Console.Write("B");
    }
}

class C : A
{
    B objB = new B();
    public C()
    {
    Console.Write("C");
    }   
}

class program
{
    static void Main(string [] args)
    {
        new C();
    }
}

Option 1: C
Option 2: BC
Option 3: BCBA
Option 4: BABC
Option 5: BBAC

Answer: Option 5: BBAC

Explanation: Base class constructor will be invoked before derived class constructor.
First constructor of class B will be called, then the execution transfers to class A, where constructor of class B will be called again, then constructor of class A will be called. At Last constructor of class C will be invoked.
So, the output comes to BBAC.

No comments:

Post a Comment