C++ Programming Questions and Answers – Inheritance – 2

«
»

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

1. What will be the output of the following C++ code?

advertisement
#include <iostream>
#include <string>
using namespace std;
class Mammal
{
   public:
	Mammal(){
		cout<<"I'm a Mammal\n";
	}
	~Mammal(){}
};
 
class Human: public Mammal
{
   public:
	Human(){
		cout<<"I'm a Human\n";
	}
	~Human(){}
};
 
class Male: public Human
{
   public:
	Male(){
		cout<<"I'm a Male\n";
	}
	~Male(){}
};
 
class Female: public Human
{
   public:
	Female(){
		cout<<"I'm a Female\n";
	}
	~Female(){}
};
 
int main(int argc, char const *argv[])
{
	Male M;
	return 0;
}

a)

I'm a Mammal
I'm a Human
I'm a Male

b)

I'm a Mammal
I'm a Human
I'm a Female

c)

I'm a Human
I'm a Male

d)

I'm a Mammal
I'm a Male
View Answer
Answer: a
Explanation: As the Male class is derived from Human class and Human class is derived from the Mammal class. Therefore when an object of Male is declared then three constructors will be called namely Mammal(), Human() and Male() in the given order.
 
 

2. What is the order of Constructors call when the object of derived class B is declared, provided class B is derived from class A?
a) Constructor of A followed by B
b) Constructor of B followed by A
c) Constructor of A only
d) Constructor of B only
View Answer

Answer: a
Explanation: Firstly the Constructor of class A is called then class B because the Constructor of the base class is called before derived class.
advertisement

3. What is the order of Destructors call when the object of derived class B is declared, provided class B is derived from class A?
a) Destructor of A followed by B
b) Destructor of B followed by A
c) Destructor of A only
d) Destructor of B only
View Answer

Answer: b
Explanation: Order of Destructor call is just reverse of the order of Destructors call. First, the destructor of the derived class is called then Destructor of the base class is called.

4. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Mammal
{
   public:
	virtual void Define(){
		cout<<"I'm a Mammal\n";
	}
};
 
class Human: public Mammal
{
   public:
	void Define(){
		cout<<"I'm a Human\n";
	}
};
 
class Male: public Human
{
   public:
	void Define(){
		cout<<"I'm a Male\n";
	}
};
 
class Female: public Human
{
   public:
	void Define(){
		cout<<"I'm a Female\n";
	}
};
 
int main(int argc, char const *argv[])
{
	Mammal *M;
	Male m;
	Female f;
	*M = m;
	M->Define();
	return 0;
}

a) I’m a Male
b) I’m a Mammal
c) Error
d) Segmentation Fault
View Answer

Answer: d
Explanation: As the Mammal pointer *M is not Initialized memory therefore program results into segmentation faults.

5. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
 
class Mammal
{
   public:
	virtual void Define(){
		cout<<"I'm a Mammal\n";
	}
};
 
class Human: public Mammal
{
   public:
	void Define(){
		cout<<"I'm a Human\n";
	}
};
 
class Male: public Human
{
   public:
	void Define(){
		cout<<"I'm a Male\n";
	}
};
 
class Female: public Human
{
   public:
	void Define(){
		cout<<"I'm a Female\n";
	}
};
 
int main(int argc, char const *argv[])
{
	Mammal *M = new Mammal();
	Male m;
	Female f;
	*M = m;
	M->Define();
	M = &m;
	M->Define();
	return 0;
}

a)

I'm a Male
I'm a Male
advertisement

b)

I'm a Male
I'm a Mammal

c)

I'm a Mammal
I'm a Male

d)

I'm a Mammal
I'm a Mammal
View Answer
Answer: c
Explanation: There is a difference between pointer and references. Pointer stores the address of a variable so we need dereferencing operator to access the pointed variable whereas references are another name for that variable so we don’t need any dereferencing operator, they are dereference by compiler itself therefore when we are using pointer then Mammal class definition is called and when reference is used then Male class definition is used.
 
 

6. Virtual functions in C++ tells the compiler to perform ______________________ on such functions.
a) static binding
b) late binding
c) compile time binding
d) no binding
View Answer

Answer: b
Explanation: Virtual function in C++ adds the power of late binding by deciding the type of object during run-time.

7. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class Mammal
{
   public:
	virtual void Define(){
		cout<<"I'm a Mammal\n";
	}
};
 
class Human: public Mammal
{
   private:
	void Define(){
		cout<<"I'm a Human\n";
	}
};
 
int main(int argc, char const *argv[])
{
	Mammal *M = new Mammal();
	Human H;
	M = &H;
	M->Define();
	return 0;
}

a) Error
b) Segmnetation fault
c) I’m a Human
d) Garbage Value
View Answer

Answer: c
Explanation: Using base class pointer we can call private functions of derived by using virtual keyword because virtual function asks compiler performs late binding i.e. bind function at run-time and at run-time there is no checking of access specifiers. Hence it can access private members.

8. Which concept of OOPs is shown by Virtual Functions?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Abstraction
View Answer

Answer: c
Explanation: Virtual function allows us to give different definitions of the same function i.e. overloading of functions which is known as Polymorphism.
advertisement

9. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A
{
   public: 
	virtual A(){
		cout<<"A's Constructor\n";
	}
};
 
class B: public A
{
   public:
	A(){
		cout<<"Present inside the class B\n";
	}
};
 
int main(int argc, char const *argv[])
{
	A a;
	return 0;
}

a) A’s Constructor
b) Present inside the class B
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: C++ does not allows programmers to make constructor a virtual function.

10. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A
{
   public: 
	virtual static void show(){
		cout<<"class A\n";
	}
};
 
class B: public A
{
   public:
	static void show(){
		cout<<"class B\n";
	}
};
 
int main(int argc, char const *argv[])
{
	A *a = new A();
	B b;
	a = &b;
	a->show();
	return 0;
}

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

Answer: b
Explanation: A function cannot be made virtual and static at the same time.

11. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
   public: 
	A(){
		a = 0;
	}
	static void show(){
		a++;
		cout<<a;
	}
};
 
class B: public A
{
   public:
 
};
 
int main(int argc, char const *argv[])
{
	B b;
	b.show();
	return 0;
}

a) 1
b) 0
c) Segmentation fault
d) Error
View Answer

Answer: d
Explanation: Non-static members of class cannot be used inside a static functions of class.

12. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
   public: 
	A(){
		a = 0;
	}
	void show(){
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
class B: public A
{
   public:
};
 
int main(int argc, char const *argv[])
{
	B b;
	b.show();
	return 0;
}

a) a: 1
b) a: 0
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: Though the constructor of class A is not called to initialize variable ‘a’ but as we know whenever we create an object of the derived class, constructors of both base and derived classes are called hence variable ‘a’ is initialized and program runs perfectly.

13. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
   public: 
	A(){
		a = 0;
	}
	void show(){
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
class B: private A
{
   public:
 
};
 
int main(int argc, char const *argv[])
{
	B b;
	b.show();
	return 0;
}

a) Error
b) Segmentation fault
c) a: 1
d) a: 0
View Answer

Answer: a
Explanation: As class B is derived privately from A hence all the members of class A cannot be accessible by the object of class B hence the program gives error.

14. What happens if the following C++ program is compiled?

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;
   public: 
	A(){
		a = 0;
	}
	void show(){
		a++;
		cout<<"a: "<<a<<endl;
	}
};
 
class B: private A
{
   public:
	void show(){
		show();
	}
};
 
int main(int argc, char const *argv[])
{
	B b;
	b.show();
	return 0;
}

a) Error because of the conflicts between two show() function in class B
b) Program will compile successfully
c) Error due to self call in show() function
d) Error because show() function from class A is derived privately
View Answer

Answer: b
Explanation: As the program is syntactically correct and as one show() function is in class A and other in class B therefore no conflicts in same name function. Therefore program compiles successfully.

15. Pick the correct statement.
a) Virtual function can have different names in the base and derived class
b) Virtual function cannot be applied in Multiple Inheritance classes
c) Virtual function are different in definitions only
d) Virtual function does early binding
View Answer

Answer: c
Explanation: Virtual functions differ in definitions only, prototype are similar. They does the late binding. They are applicable to all types of inheritance.

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