C++ Programming Questions and Answers – Inheritance – 1

«
»

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

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

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.
advertisement

2. How many specifiers are used to derive a class?
a) 1
b) 2
c) 3
d) 4
View Answer

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

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

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

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.
advertisement

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

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

advertisement

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

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(){};
View Answer

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;
}

a) Hello this is class B
b) Hello this is class A
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: Normal execution of the program and object calls func() from class B.

11. 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[])
{
	A *a;
	a->func();
	return 0;
}

a) Hello this is class A
b) Hello this is class B
c) Error
d) Segmentation Fault
View Answer

Answer: d
Explanation: As object ‘a’ is a pointer object and we know every pointer needs to be initialised memory before use. Hence segmentation fault. Use A *a = new A(); to initialise memory to the object.
advertisement

12. 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[])
{
	A *a = new A();
	B b;
	a = &b;
	a->func();
	return 0;
}

a) Hello this is class A
b) Hello this is class B
c) Error
d) Segmentation Fault
View Answer

Answer: b
Explanation: As pointer object a is pointing to the object b hence the definition of virtual function defined inside the class B will be class. This is one of the use of virtual function.

13. Which statement is incorrect about virtual function.
a) They are used to achieve runtime polymorphism
b) They are used to hide objects
c) Each virtual function declaration starts with the virtual keyword
d) All of the mentioned
View Answer

Answer: b
Explanation: Virtual function are used to achieve runtime polymorphism by calling the right function during runtime. Their declaration starts with a virtual keyword.

14. The concept of deciding which function to invoke during runtime is called ______________________
a) late binding
b) dynamic linkage
c) static binding
d) both late binding and dynamic linkage
View Answer

Answer: d
Explanation: The concept of deciding which function to invoke during runtime is called late binding or dynamic linkage. Late binding because function binding to the object is done during runtime. Dynamic linkage because this binding is done during runtime.

15. What is a pure virtual function?
a) A virtual function defined inside the base class
b) A virtual function that has no definition relative to the base class
c) A virtual function that is defined inside the derived class
d) Any function that is made virtual
View Answer

Answer: b
Explanation: A virtual function that has no definition relative to the base class is called a pure virtual function.

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

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn