C# Interview Question and Answers with Example - Part 6


Below is top C# interview question and answers with examples for freshers and intermediate developers.

What is Attribute in C#?

Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection. All the atributes must be inherited from Attribute class. Attribues can be applied to class, method, property and assembly.

What are two main types of attributes?

In-built and Custom.

Give the examples of in-built attributes.

  • Obsolete - Marks types and type members outdated
  • WebMethod - To expose a method as an XML Web service method
  • Serializable - Indicates that a class can be serialized

What is an abstract class?

An abstract class is an incomplete class and must be implemented in a derived class. When you want to provide some default functionality to related classes then use abstract class. When a class contains at least one abstract method, then the class must be declared as an abstract class.

Can you create an instance of an abstract class?

No, abstract classes are incomplete and you cannot create an instance of an abstract class.

What are abstract methods?

Abstract methods are methods that only have the declaration of the method and no implementation.

Will the following code compile?

using System;
public abstract class Customer
{
    public abstract void Test()
    {
        Console.WriteLine("I am customer");
    }
}
public class MainClass
{
    public static void Main()
    {
    }
}

No, abstract methods cannot have body. Hence, the above code will generate a compile time error stating "Customer.Test() cannot declare a body because it is marked abstract"

Is the following code legal?

using System;
public class Customer
{
    public abstract void Test();
}
public class MainClass
{
    public static void Main()
    {
    }
}

No, if a class has even a single abstract member, the class has to be marked abstract. Hence the above code will generate a compile time error stating "Customer.Test() is abstract but it is contained in non-abstract class Customer".

Will the following code compile?

using System;
public abstract sealed class Test
{
    public virtual void Method()
    {
    }
}

No, a class cannot be marked as sealed and abstract at the same time. This is because by definition, a sealed class cannot be a base class and an abstract class can only be a base class.

Can an abstract class have a constructor?

Yes

Can abstract contain non-abstract method?

Yes

Can an abstract class have only non-abstract method?

Yes

Can abstract contain abstract and non-abstract method together?

Yes

Can abstract class be defined as static?

No, abstract class can not be defined as static or virtual.

Can abstract method or abstract property of an abstract class be static?

No

Give the example of abstract class.

using System;
abstract class Vehicle
{
    public abstract double Distance { get; set; }
    public abstract double Time { get; set; }
    public abstract double Speed();
}
class Car : Vehicle
{
    double mDistance, mTime = 0.0;
    public override double Distance
    {
        get
        {
            return mDistance;
        }
        set
        {
            if (value <= 0)
            {
                mDistance = 1;
            }
            else
            {
                mDistance = value;
            }
        }
    }
    public override double Time
    {
        get
        {
            return mTime;
        }
        set
        {
            if (value <= 0)
            {
                mTime = 1;
            }
            else
            {
                mTime = value;
            }
        }
    }
    public override double Speed()
    {
        double speed = 0.0;
        double hours = mTime / 60;
        speed = mDistance / hours;
        return speed;
    }
}
class Program
{
    static void Main(string[] args)
    {
        double speed = 0.0;
        Car objCar = new Car();
        Console.WriteLine("Enter the Distance");
        objCar.Distance = Double.Parse(Console.ReadLine());
        Console.WriteLine("Enter the time in minutes");
        objCar.Time = Double.Parse(Console.ReadLine());
        speed = objCar.Speed();
        Console.WriteLine("Car speed is {0:0.00}", speed);
        Console.Read();
    }
}

How to implement abstract method in derived class?

By using override keyword with method in derived class.

Can abstract class inherit other abstract class?

Yes, an abstract class can inherit another abstract class. Also it is not necessary to provide implementation of abstract method of base abstract class in derived abstract class.

using System;
abstract class absClassA
{
    public abstract void SomeMethod();
}
abstract class absClassB : absClassA
{
    // not necessary to implement SomeMethod() here
    // if SomeMethod() implemented here then not necessary to implement in non abstract class
}
class Program : absClassB
{
    public override void SomeMethod()
    {
        Console.WriteLine("Called"); // implementation
    }
    public static void Main(string[] args)
    {
    }
}

What is interface?

An interface in C# is declared with interface keyword. An interface contains only the declaration of the methods, properties, and events, but not the implementation. Interface does not conatins fields.

What are the characterstics of interface?

  • Interface can be declared with public or internal, by default is internal.
  • Interface members are public by default, no access modifiers can be applied to the members.
  • Interface does not contains fields.

Can an interface inherit from another interface?

Yes

If a class inherits an interface, what are the 2 options available for that class?

Option 1: Provide Implementation for all the members inheirted from the interface.

namespace Interfaces
{
    interface I1
    {
        void I1Method();
    }
    class BaseClass : I1
    {
        public void I1Method()
        {
            Console.WriteLine("I1 Method");
        }
        public void BaseClassMethod()
        {
            Console.WriteLine("BaseClass Method");
        }
    }
}

Option 2: If the class does not wish to provide Implementation for all the members inheirted from the interface, then the class has to be marked as abstract.

namespace Interfaces
{
    interface I1
    {
        void I1Method();
    }
    abstract class BaseClass : I1
    {
        abstract public void I1Method();
        public void BaseClassMethod()
        {
            Console.WriteLine("BaseClass Method");
        }
    }
}

What do you mean by "Implimenting Interface Explicitly"?

Explicit interface implementation is useful when class is implementing multiple interface thereby it is more readable and eliminates the confusion. You can implement interface explicitly by prefixing interface name with method name. The disadvantage of Explicit interface implementation is that, the class object has to be type casted to the interface type to invoke the interface member. An example is shown below.

namespace Interfaces
{
    interface Car
    {
        void Drive();
    }
    class Demo : Car
    {
        // Explicit implementation of an interface member
        void Car.Drive()
        {
            Console.WriteLine("Drive Car");
        }
        static void Main()
        {
            Demo DemoObject = new Demo();
            //DemoObject.Drive(); // error
            // using the class object.
            // Type cast the demo object to interface type Car
            ((Car)DemoObject).Drive();
        }
    }
}

Can we create instance of an interface?

No, but interface reference variable can point to derived class object as shown below.

namespace Interfaces
{
    interface Car
    {
        void Drive();
    }
    class Demo : Car
    {
        void Car.Drive()
        {
            Console.WriteLine("Drive Car");
        }
        static void Main()
        {
            Car c; // interface reference
            c = new Demo();  // derived class object
            c.Drive();
        }
    }
}

Page 6 of 7