Abstract Classes & Interfaces Questions with Answers

6 practice questions on Abstract Classes & Interfaces (C#) with answers and step-by-step solutions. Try 6 samples below, then practise all 6 free in the AptiNest app.

Sample questions

Question 1Medium

Which of the following statements are correct about an interface used in C#.NET?

I. An interface can contain properties, methods and events.
II. The keyword must implement forces implementation of an interface.
III. Interfaces can be overloaded.
IV. Interfaces can be implemented by a class or a struct.
V. Enhanced implementations of an interface can be developed without breaking existing code.

  1. 1, 2
  2. 1, 4, 5
  3. 3, 4
  4. 3 only

Answer: B. 1, 4, 5

Explanation

A n interface contains only the methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface

Step-by-step solution

  1. I is true: interfaces hold properties, methods and events.
  2. II is false: there is no 'must implement' keyword.
  3. III is false: interfaces are not overloaded in this sense.
  4. IV and V are true: classes and structs can implement them, and implementations can be enhanced without breaking code.
  5. So option B (1, 4, 5) is correct.

Why the other options are wrong

  • A. II is false.
  • C. III is false, and it leaves out I.
  • D. III alone is false.

Tip: Interfaces can be implemented by classes and structs.

Question 2Medium

What will be the output for given set of code?

namespace ConsoleApplication4
 {
abstract class A
     {
int i;
public  abstract void display();
     }
class B: A
     {
public  int j;
public override void display()
         {
Console.WriteLine(j);
         }
     }
class Program
     {
static void Main(string[] args)
         {
             B obj = new B();
obj.j = 2;
obj.display();
Console.ReadLine();
         }
     }
 }
  1. 0.
  2. 2
  3. Compile time error.
  4. 1

Answer: B. 2

Explanation

Here in abstract class abstract method display() is declared and its full implementation i.e definition is given in subclass of class
Output :2

Step-by-step solution

  1. Class B inherits the abstract class A and overrides display().
  2. In Main, obj.j is set to 2.
  3. obj.display() runs B's override, which prints the value of j.
  4. The output is 2, which is option B.

Why the other options are wrong

  • A. Nothing sets j to 0 before the call, so 0 is not printed.
  • C. The code compiles: the abstract method is overridden in B.
  • D. The value 1 is never assigned anywhere.

Tip: Trace the object's field value at the moment the method is called.

Question 3Medium

What will be the output of given code snippet?

interface calc
{
    void cal(int i);
}
class displayA : calc
{
    public int x;
    public void cal(int i)
    {
        x = i * i;
    }
}
class displayB : calc
{
    public int x;
    public void cal(int i)
    {
        x = i / i;
    }
}
class Program
{
    public static void Main(string[] args)
    {
        displayA arr1 = new displayA();
        displayB arr2 = new displayB();
        arr1.x = 0;
        arr2.x = 0;
        arr1.cal(2);
        arr2.cal(2);
        Console.WriteLine(arr1.x + " " + arr2.x);
        Console.ReadLine();
    }
}
  1. 0 0
  2. 2
  3. 4 1
  4. 1 4

Answer: C. 4 1

Explanation

class display A executes the interface calculate by doubling the value of item similarly class display B implements the interface by dividing item by item. So, variable x of class display
A stores 4 and variable x of class display B
stores 1.
Output: 4, 1

Step-by-step solution

  1. The class headers in the question are garbled; read A and B as classes that implement the calc interface.
  2. arr1.cal(2) sets x = 2 × 2 = 4 in class A.
  3. arr2.cal(2) sets x = 2 ÷ 2 = 1 in class B.
  4. Printing arr1.x and arr2.x gives 4 1, so the answer is option C (4 1).

Why the other options are wrong

  • A. Cal changes both values, so 0 0 is wrong.
  • B. The output has two numbers, not one.
  • D. A gives 4 and B gives 1, so the order 1 4 is wrong.

Tip: Trace each object's own method call separately; the same method can do different maths in different classes.

Question 4Medium

Which of the following modifier is used when an abstract method is redefined by a derived class?

  1. Overloads
  2. Override
  3. Base
  4. Virtual

Answer: B. Override

Explanation

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method.

Step-by-step solution

  1. Override modifier is used to redefine abstract methods in derived classes.
  2. It allows changing or adding functionality to inherited abstract methods.
  3. Derived class must use override to implement abstract method from base class.
  4. Other modifiers like virtual are for non-abstract methods only.
  5. Correct answer is Override as it redefines the abstract method.

Why the other options are wrong

  • A. Overloads are used for method overloading, not redefining abstract methods.
  • C. Base is used to access base class members, not redefine them.
  • D. Virtual is used for non-abstract methods, not abstract ones.

Tip: Use override when redefining abstract methods in inheritance.

Question 5Medium

What will be the output for given set of code?

namespace ConsoleApplication4
{
    abstract class A
    {
        public int i;
        public abstract void display();
    }
    class B : A
    {
        public int j;
        public int sum;
        public override void display()
        {
            sum = i + j;
            Console.WriteLine(+i + "\n" + +j);
            Console.WriteLine("sum is:" + sum);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
            obj.i = 2;
            B obj1 = new B();
            obj1.j = 10;
            obj.display();
            Console.ReadLine();
        }
    }
}
  1. 2, 10,12.
  2. 0, 10, 10.
  3. 2, 0, 2
  4. 0, 0, 0.

Answer: C. 2, 0, 2

Explanation

Abstract method implementation processed in subclass B also the object obj of abstract class A initialize value of i as 2.

The object of class B also intialize value of j as 10.

Since, the method display() is called using object of class A which is obj and hence i = 2 whereas j = 0.

So, sum = 2.

Output: 2 0
sum is: 2

Step-by-step solution

  1. Obj has type A and holds a B object, so obj.i = 2 sets i to 2.
  2. obj1.j = 10 sets j on a different object, obj1, not on obj.
  3. obj.display() runs B's override: sum = i + j = 2 + 0 = 2, because obj.j is 0.
  4. It prints 2 and 0 on separate lines, then sum is:2, so the answer is option C (2, 0, 2).

Why the other options are wrong

  • A. J is 0 for obj, not 10, and sum is 2, not 12.
  • B. I is 2, not 0, because obj.i was set to 2.
  • D. I is 2 and sum is 2, so the output is not all zeros.

Tip: With a base-class variable, the object's own fields are used; a field set on another object does not change it.

Question 6Medium

Which of the following statements is correct about an interface used in C#.NET?

  1. One class can implement only one interface.
  2. In a program if one class implements an interface then no other class in the same program can implement this interface.
  3. From two base interfaces a new interface cannot be inherited.
  4. Properties can be declared inside an interface.

Answer: D. Properties can be declared inside an interface.

Explanation

Properties can be used inside an interface. In the above example theaccessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property isread-write, read-only, or write-only.

public interface ISampleInterface
{
// Property declaration:
string Name
{
get;
set;
}
}

Step-by-step solution

  1. A C# class can implement more than one interface, so option A is false.
  2. Many classes can implement the same interface, so option B is false.
  3. An interface can inherit from two or more base interfaces, so option C is false.
  4. Properties can be declared inside an interface, so the answer is option D.

Why the other options are wrong

  • A. A class can implement more than one interface.
  • B. Many classes can implement the same interface.
  • C. An interface can inherit from several base interfaces.

Tip: An interface declares members (methods, properties, events) with no body; a class supplies the code.

Practise all 6 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C# topics · All topics