C# class and constructor – programs for practice

Q) What is output of C# code?

class Base
{
    public Base()
    {
        Console.WriteLine("Base class constructor");
    }
}
class Child : Base
{   
    public Child()
    {
        Console.WriteLine("Child class constructor");
    }

}
class Program
{
    static void Main(string[] args)
    {
        Child c = new Child();
    }
}

Output:
Base class constructor
Child class constructor
Explanation: In inheritance C# oops relationship, when we create object of a derived or child class then first base class constructor then derived class constructor get called.


Q) What is output of below C# code example?

class A
{
    static A()
    {
        Console.WriteLine("Static A()"); 
    }
    
    public A() { 
        Console.WriteLine("A()"); 
    }   
}

class Program
{
    static void Main(string[] args)
    {
        A obj = new A();       
    }
}

Output:

  1. Compiler error – constructor re-definition
  2. Compiler error – constructor cannot be static
  3. Will print A()
  4. Will print Static A()then A()

Answer: D
Explanation: if a class have static constructor then on creation of an object of the class static constructor will be called then class constructor.



Q) What is output of C# program example?

public class Printer
{

    public Printer(): this(string.Empty)
    {
        Console.Write("A");
    }

    public Printer(string name)
    {
        Console.Write("B");
    }   
}
class Program
{
    static void Main(string[] args)
    {
        Printer obj = new Printer();       

    }
}

Output:BA
This C# program example has implemented constructor chaining. When we create object of the class then one argument constructor via empty cosntructor will be called and B will be printed then control will move to empty constructor body and will print A.
NOTE: if we create one argument constructor e.g. Printer obj = new Printer(“Hello”); then one argument constructor will be called directly and will program will print B.


Q) What is output of C# program?

class A
{
    String str;

    public A(String s)
    {
        this.str = s;     
    }
   public void display()
    {
        Console.Write(str);
    }
}
class B : A
{
    public B(String s)
        : base(s)
    {
        Console.Write(s);
    }
}
class Program
{
    static void Main(string[] args)
    {
        B obj = new B("Hello World");
        obj.display();

    }
}

Output:
Hello World Hello World
Explanation: In this C# code example, base class variable str is getting initialized via derived class constructor. Hence, on object creation with Hello World parameter, this string is available to both base class and derived class constructor body. Hence output will be Hello World Hello World.



Q) What is output of C# code?

class A
{
    public A(String s)
    {
        Console.WriteLine(s);
    }
    public A(int a)
    {
        Console.WriteLine(a);
    }
    public void display()
    {
        Console.WriteLine("Display");
    }
}

class Program
{
    static void Main(string[] args)
    {
        A obj = new A();
        obj.display();

    }
}

Output:

  1. compiler error as two constructors wih same number of arguments cannot be overloaded
  2. Compiler error as empty constructor is not written.
  3. Both A & B are reasons for compiler error
  4. Program is ok

Answer: B
In this program two overloaded constructors win same number of argument with different data type is valid. compiler error will be flashed for empty constructor as we are creating empty object and it is missing in the program. if we write constructor like public A () {} then program will be fine.


Related Posts