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
[su_spoiler title=”View Answer” style=”fancy” icon=”folder-2″] Answer: B
We cannot create an instance of the abstract class in c# programming.
[/su_spoiler]

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");
    }    
}

[su_spoiler title=”View Answer” style=”fancy” icon=”folder-2″] 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();
     
}

[/su_spoiler]


Leave a Comment