My Report

C++ Inheritance Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

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

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

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

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

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

6. What is the order of Destructors call when the object of derived class B is declared, provided class B is derived from class A?

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

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

9. Which concept of OOPs is shown by Virtual Functions?

10. Virtual functions in C++ tells the compiler to perform ______________________ on such functions.

11. What is the order of Constructors call when the object of derived class B is declared, provided class B is derived from class A?

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

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

14. Pick the correct statement.

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

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

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.