C++ interview questions and answers focuses on “Abstract Classes”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ interview questions on “Abstract Classes” along with answers, explanations and/or solutions:
Q 1.Which class is used to design the base class?
A. derived class
B. abstract class
C. base class
D. derived & base class
Show Answer
Answer:-B. abstract classExplanation
Abstract class is used to design base class because functions of abstract class can be overridden in derived class hence derived class from same base class can have common method with different implementation, hence forcing encapsulation.Q 2. Which is used to create a pure virtual function?
A. $
B. =0
C. &
D. !
Show Answer
Answer:-B. =0Explanation
For making a method as pure virtual function, We have to append ‘=0’ to the class or method.Q 3.Which is also called as abstract class?
A. virtual function
B. pure virtual function
C. derived class
D. base class
Show Answer
Answer:-B. pure virtual functionExplanation
Classes that contain at least one pure virtual function are called as abstract base classes. advertisementQ 4.What will be the output of the following C++ code? include using namespace std;
class p
{
protected:
int width, height;
public:
void set_values (int a, int b)
{
width = a; height = b;
}
virtual int area (void) = 0;
};
class r: public p
{
public:
int area (void)
{
return (width * height);
}
};
class t: public p
{
public:
int area (void)
{
return (width * height / 2);
}
};
int main ()
{
r rect;
t trgl;
p * ppoly1 = ▭
p * ppoly2 = &trgl;
ppoly1->set_values (4, 5);
ppoly2->set_values (4, 5);
cout << ppoly1 -> area() ;
cout << ppoly2 -> area();
return 0;
}
A.10
B. 20
C. 1020
D. 2010
Show Answer
Answer:-D. 2010Explanation
In this program, We are calculating the area of rectangle and triangle by using abstract class. Output: $ g++ abs.cpp $ a.out 2010Q 5. What is meant by pure virtual function?
A. Function which does not have definition of its own
B. Function which does have definition of its own
C. Function which does not have any return type
D. Function which does not have any return type & own definition
Show Answer
Answer:-A. Function which does not have definition of its ownExplanation
As the name itself implies, it have to depend on other class only.Q 6. Pick out the correct option.
A. We can make an instance of an abstract super class
B. We can make an instance of an abstract base class
C. We cannot make an instance of an abstract base class
D. We can make an instance of an abstract derived class
Show Answer
Answer:-C. We cannot make an instance of an abstract base classExplanation
We cannot make an instance of an abstract base class.Q 7.Where does the abstract class is used?
A. base class only
B. derived class
C. both derived & base class
D. virtual class
Show Answer
Answer:-A. base class onlyExplanation
As base class only as it helps in encapsulation of similar functioning of derived classes.Q 8. What will be the output of the following C++ code? include 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;
}
A. 5
B. 10
C. 15
D. 55
Show Answer
Answer:-D. 55Explanation
In this program, We are displaying the data from the two classes by using abstract class. Output: $ g++ abs1.cpp $ a.out 5 5Q 9. What will be the output of the following C++ code?
include using namespace std;
class sample
{
public:
virtual void example() = 0;
};
class Ex1:public sample
{
public:
void example()
{
cout << “ubuntu”; } }; class Ex2:public sample { public: void example() { cout << ” is awesome”; } }; int main() { sample* arra[2]; Ex1 e1; Ex2 e2; arra[0]=&e1; arra[1]=&e2; arra[0]->example();
arra[1]->example();
}
A. ubuntu is awesome
B. is awesome
C. ubuntu
D. ubunt esome
Show Answer
Answer:-A. ubuntu is awesomeExplanation
In this program, We are combining the two statements from two classes and printing it by using abstract class. Output: $ g++ abs3.cpp $ a.out ubuntu is awesomeQ 10. What will be the output of the following C++ code? include using namespace std;
class Base
{
public:
virtual void print() const = 0;
};
class DerivedOne : virtual public Base
{
public:
void print() const
{
cout << “1”; } }; class DerivedTwo : virtual public Base { public: void print() const { cout << “2”; } }; class Multiple : public DerivedOne, DerivedTwo { public: void print() const { DerivedTwo::print(); } }; int main() { Multiple both; DerivedOne one; DerivedTwo two; Base *array[ 3 ]; array[ 0 ] = &both; array[ 1 ] = &one; array[ 2 ] = &two; for ( int i = 0; i < 3; i++ ) array[ i ] -> print();
return 0;
}
A. 121
B. 12
C. 212
D. 215
Leave a Reply