Inheritance & Polymorphism
1. What is Inheritance in C++?
a) Wrapping of data into a single class
b) Deriving new classes from existing classes
c) Overloading of classes
d) Classes with same names
Answer: b
Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing
classes in order to reuse the properties of classes defined earlier.
2. How many specifiers are used to derive a class?
a) 1
b) 2
c) 3
d) 4
Answer: c
Explanation: There are 3 specifiers used to derive a class. They are private, protected and public.
3. Which specifier makes all the data members and functions of base class inaccessible by
the derived class?
a) private
b) protected
c) public
d) both private and protected
Answer: a
Explanation: Private access specifier is used to make all the data members and functions of the
base class inaccessible.
4. If a class is derived privately from a base class then ______________________________
a) no members of the base class is inherited
b) all members are accessible by the derived class
c) all the members are inherited by the class but are hidden and cannot be accessible
d) no derivation of the class gives an error
Answer: c
Explanation: Whenever a class is derived, all the members of the base class is inherited by the
derived class but are not accessible by the derived class.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;
, float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: private A
{
};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
a) 8
b) 12
c) Error
d) Segmentation fault
Answer: b
Explanation: As class B is derived from class A and class A has three members with each of 4
bytes size hence size of B equal to 3 * 4 = 12 bytes.
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
int a;
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: public A
{
int a = 15;
public:
void print(){
cout<<a;
}
};
int main(int argc, char const *argv[])
,{
B b;
b.change(10);
b.print();
b.value_of_a();
return 0;
}
a) 1010
b) 1510
c) 1515
d) 5110
Answer: b
Explanation: When change() is called it sets parents class ‘a’ variable = 10. When print() is called
then ‘a’ from class B is printed and wehn value_of_a() is called then ‘a’ from class A is printed.
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};
class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
return 0;
}
a)
Constructor of class A
Constructor of class B
b) Constructor of class A
c) Constructor of class B
d)
Constructor of class B
Constructor of class A
, Answer: a
Explanation: When a derived class is declared it calls both its constructor and the base class
constructor. It first calls the base class constructor and then its own constructor.
8. What is a virtual function in C++?
a) Any member function of a class
b) All functions that are derived from the base class
c) All the members that are accessing base class data members
d) All the functions which are declared in the base class and is re-defined/overridden by the
derived class
Answer: d
Explanation: Virtual function is a function that is declared inside the base class and is re-defined
inside the derived class.
9. Which is the correct syntax of declaring a virtual function?
a) virtual int func();
b) virtual int func(){};
c) inline virtual func();
d) inline virtual func(){};
Answer: a
Explanation: To make a function virtual function we just need to add virtual keyword at the
starting of the function declaration.
10. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;
1. What is Inheritance in C++?
a) Wrapping of data into a single class
b) Deriving new classes from existing classes
c) Overloading of classes
d) Classes with same names
Answer: b
Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing
classes in order to reuse the properties of classes defined earlier.
2. How many specifiers are used to derive a class?
a) 1
b) 2
c) 3
d) 4
Answer: c
Explanation: There are 3 specifiers used to derive a class. They are private, protected and public.
3. Which specifier makes all the data members and functions of base class inaccessible by
the derived class?
a) private
b) protected
c) public
d) both private and protected
Answer: a
Explanation: Private access specifier is used to make all the data members and functions of the
base class inaccessible.
4. If a class is derived privately from a base class then ______________________________
a) no members of the base class is inherited
b) all members are accessible by the derived class
c) all the members are inherited by the class but are hidden and cannot be accessible
d) no derivation of the class gives an error
Answer: c
Explanation: Whenever a class is derived, all the members of the base class is inherited by the
derived class but are not accessible by the derived class.
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;
, float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: private A
{
};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
a) 8
b) 12
c) Error
d) Segmentation fault
Answer: b
Explanation: As class B is derived from class A and class A has three members with each of 4
bytes size hence size of B equal to 3 * 4 = 12 bytes.
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
int a;
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: public A
{
int a = 15;
public:
void print(){
cout<<a;
}
};
int main(int argc, char const *argv[])
,{
B b;
b.change(10);
b.print();
b.value_of_a();
return 0;
}
a) 1010
b) 1510
c) 1515
d) 5110
Answer: b
Explanation: When change() is called it sets parents class ‘a’ variable = 10. When print() is called
then ‘a’ from class B is printed and wehn value_of_a() is called then ‘a’ from class A is printed.
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};
class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
return 0;
}
a)
Constructor of class A
Constructor of class B
b) Constructor of class A
c) Constructor of class B
d)
Constructor of class B
Constructor of class A
, Answer: a
Explanation: When a derived class is declared it calls both its constructor and the base class
constructor. It first calls the base class constructor and then its own constructor.
8. What is a virtual function in C++?
a) Any member function of a class
b) All functions that are derived from the base class
c) All the members that are accessing base class data members
d) All the functions which are declared in the base class and is re-defined/overridden by the
derived class
Answer: d
Explanation: Virtual function is a function that is declared inside the base class and is re-defined
inside the derived class.
9. Which is the correct syntax of declaring a virtual function?
a) virtual int func();
b) virtual int func(){};
c) inline virtual func();
d) inline virtual func(){};
Answer: a
Explanation: To make a function virtual function we just need to add virtual keyword at the
starting of the function declaration.
10. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};
class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};
int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;