My Report

C++ Inheritance Test – 2


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

2. Which statement is incorrect about virtual function.

3. Which specifier makes all the data members and functions of base class inaccessible by the derived class?

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

5. What is Inheritance in C++?

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

7. What is a virtual function in C++?

8. What is a pure virtual function?

9. The concept of deciding which function to invoke during runtime is called ______________________

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

11. If a class is derived privately from a base class then ______________________________

12. How many specifiers are used to derive a class?

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

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

15. Which is the correct syntax of declaring a virtual function?


 

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.