Abstract Classes & Interfaces Questions with Answers

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

Sample questions

Question 1Medium

What is meant by pure virtual function?

  1. Function which does not have definition of its own.
  2. Function which does have definition of its own.
  3. Function which does not have any return type.
  4. Function which cannot be overridden in a derived class.

Answer: A. Function which does not have definition of its own.

Explanation

A virtual member is a member function that can be redefined in a derived class, while preserving its calling properties through references. The syntax for a function to become virtual is to precede its declaration with the virtual keyword.
As the name itself implies, it have to depend on other class only.

Step-by-step solution

  1. A pure virtual function is declared in a base class with = 0 and has no definition of its own in that class.
  2. Derived classes must override it, or they stay abstract.
  3. So the function without its own definition is the pure virtual one: option A.

Why the other options are wrong

  • B. A function with its own definition is an ordinary or virtual function, not a pure virtual one.
  • C. Pure virtual functions have return types like any other function, so this is not the meaning.
  • D. A pure virtual function is meant to be overridden in derived classes, not blocked from it.

Tip: A pure virtual function is written with = 0 and makes its class abstract.

Question 2Medium

What is the output of this program?

    #include <iostream>
    using namespace std;
    class MyInterface
    {
        public:
        virtual void Display() = 0;
    };
    class Class1 : public MyInterface
    {
        public:
        void Display()
        {
            int  a = 5;
            cout << a;
        }
    };
    class Class2 : public MyInterface
    {
        public:
        void Display()
        {
            cout <<" 5" << endl;
        }
    };
    int main()
    {
        Class1 obj1;
        obj1.Display();
        Class2 obj2;
        obj2.Display();
        return 0;
    }
  1. 5
  2. 10
  3. 5 5
  4. None of the mentioned

Answer: C. 5 5

Explanation

In this program, We are displaying the data from the two classes by using abstract class.

Output:

$ g++ abs1.cpp
$ a.out
5 5

Step-by-step solution

  1. obj1.Display() runs Class1::Display, which prints 5 with no space or newline.
  2. obj2.Display() runs Class2::Display, which prints a space, then 5, then a newline.
  3. The combined output is '5 5', which is option C.

Why the other options are wrong

  • A. 5 alone misses the output of the second object.
  • B. 10 would need a second 5 with no space, which the code does not print.
  • D. Option C matches the output, so 'none' is wrong.

Tip: Trace the spaces and newlines carefully; a leading space in the string changes the output.

Question 3Medium

Which is also called as abstract class?

  1. virtual function
  2. pure virtual function
  3. derived class
  4. None of the mentioned

Answer: B. pure virtual function

Explanation

Classes that contain at least one pure virtual function are called as abstract base classes.

Step-by-step solution

  1. A class that has at least one pure virtual function is an abstract class.
  2. A pure virtual function is declared with = 0, for example virtual void f() = 0;
  3. An ordinary virtual function has a body and does not make a class abstract.
  4. So the answer is pure virtual function.

Why the other options are wrong

  • A. An ordinary virtual function does not make a class abstract.
  • C. A derived class is a class that inherits from another; it is not abstract by itself.
  • D. Option B is correct, so None of the mentioned is wrong.

Tip: Abstract means a pure virtual function exists, marked with = 0.

Question 4Medium

Pick out the correct option.

  1. We cannot make an instance of an abstract base class
  2. We can make an instance of an abstract base class
  3. Both a & b
  4. None of the mentioned

Answer: A. We cannot make an instance of an abstract base class

Explanation

You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class.
You can, however, declare pointers and references to an abstract class.

Step-by-step solution

  1. An abstract class has at least one pure virtual function.
  2. C++ does not allow creating an object of an abstract class.
  3. So the correct statement is that an instance of an abstract base class cannot be made, which is option A.

Why the other options are wrong

  • B. This is false, because the compiler rejects an object of an abstract class.
  • C. Option B is false, so 'both' is wrong.
  • D. Option A is correct, so 'none' is wrong.

Tip: You can have pointers and references to an abstract class, but not objects of it.

Question 5Medium

Which is used to create a pure virtual function?

  1. $
  2. =0
  3. &
  4. !

Answer: B. =0

Explanation

For making a method as pure virtual function, We have to append (=0) to the class or method.

Step-by-step solution

  1. A pure virtual function is declared in C++ by writing = 0 after the function declaration.
  2. The symbol $ is not a C++ token, so it cannot mark a pure virtual function.
  3. The symbol & is the reference operator and! is logical NOT, so neither marks a pure virtual function.
  4. The answer is =0.

Why the other options are wrong

  • A. $ is not a C++ token.
  • C. & is the reference operator, not a pure virtual marker.
  • D. ! is the logical NOT operator, not a pure virtual marker.

Tip: A pure virtual function ends its declaration with = 0.

Question 6Medium

Where does the abstract class is used?

  1. base class only
  2. derived class
  3. both a & b
  4. None of the mentioned

Answer: A. base class only

Explanation

In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes.
It's a way of forcing a contract between the class designer and the users of that class.

Step-by-step solution

  1. An abstract class has at least one pure virtual function, so objects of it cannot be created.
  2. It is written to be inherited by concrete classes that supply the missing functions.
  3. So it is used as a base class only, and the correct answer is option A.

Why the other options are wrong

  • B. A derived class is concrete; the abstract class is the base, not the derived class.
  • C. Option A is right and option B is not, so both is wrong.
  • D. Option A is correct, so none is wrong.

Tip: An abstract class is a base class for inheritance and cannot be instantiated.

Question 7Medium

Which class is used to design the base class?

  1. abstract class
  2. derived class
  3. base class
  4. None of the mentioned

Answer: A. abstract class

Explanation

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function.
You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

The following is an example of an abstract class:

class AB {
public:
  virtual void f() = 0;
};

Step-by-step solution

  1. An abstract class is designed to be used only as a base class.
  2. It contains at least one pure virtual function, declared with = 0.
  3. A derived class inherits from a base class; it is not the class used to design one.
  4. So the answer is option A, abstract class.

Why the other options are wrong

  • B. A derived class inherits from a base class; it is not the class used to design the base.
  • C. The base class is what is being designed, so it cannot also be the design tool.
  • D. Option A is correct, so 'none of the mentioned' is wrong.

Tip: A class with a pure virtual function (= 0) is abstract and is meant to be a base class.

Practise all 7 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