C# abstract class- programs for practice

Q) What is output of the following program?

abstract class Customer
{
    public void Print()
    {
        Console.WriteLine("Print Method");

    }
}

class Program
{
    static void Main(string[] args)
    {
        Customer c = new Customer();
        c.Print();

    }
}

Output:

  1. Print method
  2. Compiler error

Answer: B
We cannot create an instance of the abstract class in c# programming.


Q)What is issue in the below C# class?

abstract class Car
{
    public void Run()
    {
        Console.WriteLine("display");
    }
    public abstract void engine()
    {
        Console.WriteLine("1000 cc");
    }    
}

Answer: A method cannot have a body which is marked as abstract. Below is the correct declaration. The class derived from it will implement the engine method which is abstract.

abstract class Car
{
    public void Run()
    {
        Console.WriteLine("running");
    }
    public abstract void engine();
     
}


Q)What is output of the C# program?

abstract class Car
{
    public void run()
    {
        Console.WriteLine("running");
    }
    public abstract void engine();
     
}
class Honda : Car
{
    public void engine()
    {
        Console.WriteLine("1000 cc");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Car c = new Honda();
        c.engine();
        c.run();
    }
}

Answer: Compiler error.
In Honda class engine method, should have “override” Keyword. As it is in base class abstract. Rule is if in the base class there is an abstract method then it must be overridden and implemented in the derived class.
For example,

class Honda : Car
{
    public override void engine()
    {
        Console.WriteLine("1000 cc");
    }
} 

Now output will be 1000 cc and running


Q) What is output of C# program?

abstract class A
{
    protected A() { Console.WriteLine("Abstract class constructor"); }
}

class B : A
{
    public B() { Console.WriteLine("Derived class constructor"); }
}

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

  1. Compiler error – An abstract class cannot have constructor as we cannot create object of an abstract class in c# programming.
  2. Comiler error – protected constructor of an abstract class cannot be accessed by derived object.
  3. Program will compile and it will call only derived class constructor.
  4. Abstract class constructor, Derived class constructor

Answer: D
Explanation: An abstract class can have constructor in C# language. This is also true that we cannot create an object of an abstract class.



Q) What is output of below C# code?

abstract class Animal
{
    static Animal()
    {
        Console.WriteLine("Static constructor");
    }
    public Animal()
    {
        Console.WriteLine("Constructor");
    }
    public abstract void run();

}
class Dog:Animal
{
    public Dog()
    {
        Console.WriteLine("Dog Constructor");
    }
    public override void run(){
        Console.WriteLine("Dog running");
    }
    
}


class Program
{
    static void Main(string[] args)
    {
        Animal dog = new Dog();
        dog.run();       
    }
}

  1. compiler error as static constructor in abstract class is private
  2. compiler error as an abstract class can not have static constructor
  3. program is ok and output will be as per call.

Output: C
Static constructor
Constructor
Dog Constructor
Dog running
Explanation:
An abstract call can have static constructor and to note that the static constructor should be private not public or else compiler will flash an error. Hence, program is fine. You can read the C# interview question that is Can an abstract class have constructor in C# programming?


Q) In below C# code example, abstract class connect () method is getting called. What changes we can make so it can call SQL Server class connect method without changing code in main () function.

abstract class Database
{
    public virtual void connect()
    {
        Console.WriteLine("Connect");
    }
    public abstract void configure();
    
}

class SQLServer : Database
{
    public void connect()
    {
        Console.WriteLine("Connect SQL Server");
    }

    public override void configure()
    {
        Console.WriteLine("Configure database");
    }
   
}

class Program
{
    static void Main(string[] args)
    {
        Database db = new SQLServer();
        db.configure();
        db.connect();

    }
}

Answer: connect method of SQLServer class should be overriden. Since abstract base class connect method is virtual it can be overriden in derived class. e.g. public override void connect()



Q) What is output of below C# program?

abstract class X
{
    public virtual void foo()
    {
        Console.WriteLine("abstract base foo");
    }
}

class Y :X
{
    public override void foo()
    {
        Console.WriteLine("derived foo");
    }   
}
class Program
{
    static void Main(string[] args)
    {
        X obj = new Y();
        obj.foo();
    }
}

Output:

  1. Compiler error – abstract class must contain an abstract method.
  2. Compiler error – virtual function is not allowed in abstract class but only abstract method.
  3. derived foo

Answer: C
An abstract class may not have an abstract method. It can contain both virtual and abstract method. Note: Virtual and abstract methods both can be overridden.


Related Posts