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.

What will be the output of below code

int a1;
int i,j ;

i = a++; //1
j = ++a; //3
Console.WriteLine(i+j);

Output: 4  

int ij = 1k;
for (i = 0i < 5i++)
{
    k = j++ + ++j;
    Console.Write(k + " ");
}
Console.Read();

Output: 4 8 12 16 20

delegate void Printer();

static void Main()
{
    List<Printerprinters = new List<Printer>();
    int i = 0;
    for (; i < 10i++)
    {
        printers.Add(delegate { Console.WriteLine(i); });
    }

    foreach (var printer in printers)
    {
        printer();
    }
    Console.Read();
}

Output: 10 (10 times)
Learn More
https://dailydotnettips.com/





0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives