C++ Programming Questions and Answers – Abstract Classes – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Abstract Classes – 2”.

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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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();
View Answer

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

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

#include <iostream>
#include <string>
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
View Answer

Answer: a
Explanation: 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.
advertisement

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

advertisement
#include <iostream>
#include <string>
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
View Answer

Answer: b
Explanation: 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 <iostream>
#include <string>
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
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.