C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Abstract Classes – 2”.
Q 1. What is an abstract class in C++?
A. Class specifically used as a base class with at least one virtual functions
B. Class specifically used as a base class with at least one pure virtual functions
C. Class from which any class is derived
D. Any Class in C++ is an abstract class
Show Answer
Answer:-B. Class specifically used as a base class with at least one pure virtual functionsExplanation
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.Q 2. What is a pure virtual function in C++?
A. A virtual function defined in a base class
B. Any function in a class
C. A virtual function declared in a base class
D. A function without definition in a base class
Show Answer
Answer:-C. A virtual function declared in a base classExplanation
Pure virtual function is a virtual function which has no definition/implementation in the base class.Q 3. Which is the correct syntax of defining a pure virtual function?
A. virtual return_type func() = 0;
B. virtual return_type func() pure;
C. pure virtual return_type func();
D. virtual return_type func();
Show Answer
Answer:-A. virtual return_type func() = 0;Explanation
virtual return_type function_name(parameters) = 0; where {=0} is called pure specifier. advertisementQ 4. Which is the correct statement about pure virtual functions?
A. They should be defined inside a base class
B. Pure virtual function is implemented in derived classes
C. Pure keyword should be used to declare a pure virtual function
D. Pure virtual function cannot implemented in derived classes
Show Answer
Answer:-B. Pure virtual function is implemented in derived classesExplanation
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++. Q 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
Show Answer
Answer:-C. Pure virtual function has no implementation in the base class whereas virtual function may have an implementation in the base classExplanation
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.Q 6. What will be the output of the following C++ code?
#include
using namespace std;
class A
{
int a; // This remains uninitialized, which is not a problem but can be explicitly initialized in a constructor
public:
virtual void func() = 0; // Pure virtual function
};
class B : public A
{
public:
void func() override {
cout << “Class B” << endl;
}
};
int main()
{
B b;
b.func(); // Calls the overridden function in class B
return 0;
}
A. Segmentation fault
B. Class B
C. Error
D. No output
Show Answer
Answer:-B. Class BExplanation
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.Q 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. No output
C. Segmentation fault
D. Error
Show Answer
Answer:-D. ErrorExplanation
The C++ does allows to declare a normal object for an abstract class therefore the program throws an error as we are trying to declare an object of abstract class.Q 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. No output
D. Segmentation fault
Leave a Reply