1. Which class is used to design the base class?
A. abstract class
B. derived class
C. base class
D. derived & base class
Answer: A
Explanation:
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.
2. Which is used to create a pure virtual function?
A. $
B. =0
C. &
D. !
Answer: B
Explanation:
For making a method as pure virtual function, We have to append ‘=0’ to the class or method.
3. Which is also called as abstract class?
A. virtual function
B. pure virtual function
C. derived class
D. base class
Answer: B
Explanation:
Classes that contain at least one pure virtual function are called as abstract base classes.
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. 1020
B. 20
C. 10
D. 2010
Answer: D
Explanation:
In this program, We are calculating the area of rectangle and triangle by using abstract class.
5. 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. 5 5
D. 15
Answer: C
Explanation:
In this program, We are displaying the data from the two classes by using abstract class.
6. 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
B. is awesome
C. ubuntu is awesome
D. ubunt esome
Answer: C
Explanation:
In this program, We are combining the two statements from two classes and printing it by using abstract class.
7. 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. 212
C. 12
D. 215
Answer: B
Explanation:
In this program, We are executing these based on the condition given in array. So it is printing as 212.
8. 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
Answer: A
Explanation:
As the name itself implies, it have to depend on other class only.
9. Pick out the correct option.
A. We cannot make an instance of an abstract base class
B. We can make an instance of an abstract base class
C. We can make an instance of an abstract super class
D. We can make an instance of an abstract derived class
Answer: A
Explanation:
We cannot make an instance of an abstract base class.
10. Where does the abstract class is used?
A. base class only
B. derived class
C. both derived & base class
D. virtual class
Answer: A
Explanation:
As base class only as it helps in encapsulation of similar functioning of derived classes.
- 10+ TOP C++ MCQs on OOPs Concept – 2
- 10+ TOP C++ MCQs on OOPs Concept – 3
- 10+ TOP C++ MCQs on OOPs Concept – 4
- 10+ TOP C++ MCQs on Integer Types
- 10+ TOP C++ MCQs on Floating Point Types
- 10+ TOP C++ MCQs on References – 2
- 10+ TOP C++ MCQs on References – 3
- 10+ TOP C++ MCQs on Objects