250+ TOP MCQs on Abstract Classes and Answers Quiz

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Abstract Classes ”.

1. What is an abstract class in C++?
a) Class specifically used as a base class with atleast one virtual functions
b) Class specifically used as a base class with atleast one pure virtual functions
c) Class from which any class is derived
d) Any Class in C++ is an abstract class

Answer: b
Clarification: An abstract class is defined as a class which is specifically used as a base class. An abstract class should have atleast one pure virtual function.

2. What is a pure virtual function in C++?
a) A virtual function defined in a base class
b) A virtual function declared in a base class
c) Any function in a class
d) A function without definition in a base class

Answer: b
Clarification: Pure virtual function is a virtual function which has no definition/implementation in the base class.

3. Which is the correct syntax of defining a pure virtual function?
a) pure virtual return_type func();
b) virtual return_type func() pure;
c) virtual return_type func() = 0;
d) virtual return_type func();

Answer: c
Clarification: virtual return_type function_name(parameters) = 0; where {=0} is called pure specifier.

4. Which is the correct statement about pure virtual functions?
a) They should be defined inside a base class
b) Pure keyword should be used to declare a pure virtual function
c) Pure virtual function is implemented in derived classes
d) Pure virtual function cannot implemented in derived classes

Answer: c
Clarification: A pure virtual function does not have a definition corresponding to base class. All derived class may or may not have an implementation of a pure virtual function. there is no pure keyword in C++.

5. Pick the correct statement.
a) Pure virtual functions and virtual functions are the same
b) Both Pure virtual function and virtual function have an implementation in the base class
c) Pure virtual function has no implementation in the base class whereas virtual function may have an implementation in the base class
d) The base class has no pure virtual function

Answer: c
Clarification: Pure virtual function has no implementation in the base class whereas virtual function may have an implementation in the base class. The base class has at least one pure virtual function.

6. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A
{
	int a;
    public:
	virtual void func() = 0;
};
 
class B: public A
{
   public:
	void func(){
		cout<<"Class B"<<endl;
	}	
};
 
int main(int argc, char const *argv[])
{
	B b;
	b.func();
	return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

Answer: a
Clarification: The program is correct so no error occurs hence the program runs successfully and b is calling is func() function therefore “Class B” is printed.

7. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A
{
	int a;
   public:
	virtual void func() = 0;
};
 
class B: public A
{
   public:
	void func(){
		cout<<"Class B"<<endl;
	}	
};
 
int main(int argc, char const *argv[])
{
	A a;
	a.func();
	return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

Answer: b
Clarification: The C++ does allows to declare a normal object for an astract class therefore the program throws an error as we are trying to declare an object of abstract class.

8. What will be the output of the following C++ code?

#include 
#include 
using namespace std;
class A
{
	int a;
    public:
	virtual void func() = 0;
};
 
class B: public A
{
    public:
	void func(){
		cout<<"Class B"<<endl;
	}	
};
 
int main(int argc, char const *argv[])
{
	A *a;
	a->func();
	return 0;
}

a) Class B
b) Error
c) Segmentation fault
d) No output

Answer: c
Clarification: As we are allowed to declare a pointer object of an abstract so the program does not give any compilation error but as there is no definition of func() function corresponding to class A, therefore, the program gives segmentation fault as it is not able to call such function from class A.

Leave a Reply

Your email address will not be published. Required fields are marked *