C# Programs Output Questions

1-  In case of inheritence, what is calls first? 

Base Constructor is called first. But the initializer of fields in derived class is called first.
The calling order is
  1. derived class field initializer
  2. base class field initializer
  3. base class constructor
  4. derived class constructor
Let's say you have classes like this:
class A {}
class B : A {}
class C : B {}
First, field initializers will be called in order of most-derived to least-derived classes. So first field initializers of C, then B, then A.
The constructors will then be called in the opposite order: First A's constructor, then B, then C.
2- What will be the order of execution of below programe.
public class BaseClass {

    private string sentenceOne = null;  // A

    public BaseClass() {
        sentenceOne = "The quick brown fox";  // B
    }
}

public class SubClass : BaseClass {

    private string sentenceTwo = null; // C

    public SubClass() {
        sentenceTwo = "jumps over the lazy dog"; // D
    }
}

Answer: Order of execution is: C, A, B, D.

3-  What will be the output of below Program.

using System;
class Base
{

public Base()
{
    Console.WriteLine("BASE 1");
}
public Base(int x)
{
    Console.WriteLine("BASE 2");
}
}

class Derived : Base
{
public Derived():base(10)
{
    Console.WriteLine("DERIVED CLASS");
}
}

class MyClient
{
public static void Main()
{
    Derived d1 = new Derived();
}
}
Answer: 
BASE2
DERIVED CLASS

4- What will be the output of below Program.

using System;
class A
{
    public A() {
        PrintFields();
    }
    public virtual void PrintFields() {}
}
class B: A
{
    int x = 1;
    int y;
    public B() {
        y = -1;
    }
    public override void PrintFields() {
        Console.WriteLine("x = {0}, y = {1}", x, y);
    }
}
Answer: 
x = 1, y = 0
5- What will be the output of below Program.

  class Base
    {
        public Base()
        {
            Console.WriteLine("Inside Base Constructor");
        }
        ~Base()
        {
            Console.WriteLine("Inside Base Destructor");
        }
    }
    class Derived : Base
    {
        public Derived()
        {
            Console.WriteLine("Inside The Derived Constructor");
        }
        ~Derived()
        {
            Console.WriteLine("Inside The Derived Destructor");
        }

    }

Answer: 
Inside Base Constructor
Inside The Derived Constructor

6- What will be the output of below Program.

    class Class1
    {
        protected int a, b;
        public Class1()
        {
            a = 0;
            b = 0;
            Console.WriteLine("Inside base class default constructor");
        }
        public Class1(int a, int b)
        {
            this.a = a;
            this.b = b;
            Console.WriteLine("Inside base class parameterized constructor");
        }
    }
    class Class2 : Class1
    {
        int c;
        public Class2(int a, int b, int c) : base(a, b)
        {
            this.c = c;
            Console.WriteLine("Inside derived class parametrized constructor");
        }

    }

Answer: 
Inside base class parameterized constructor
Inside derived class parametrized constructor

7- Are C# constructors inherited?
Constructors are not inherited but they get executed because when you create an object of child class then first compiler will create a parent then comes to child and while creation / Execution of parent, Constructor will be called implicitly.

Learn More
https://dailydotnettips.com/





0 comments:

Post a Comment

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (26) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (13) Web Api (9) Web Service (1) XMl (1)