Vidyalelo
C++ Programming · all questions

Inheritance in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

80

Questions

4/4

Page

Pick an option on a question to see the right answer and solution.

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

Select an option to see the answer and solution.

What is Inheritance in C++?

Select an option to see the answer and solution.

What is a virtual function in C++?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Which concept of OOPs is shown by Virtual Functions?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What are the things are inherited from the base class?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class polygon 
{
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    { 
        width = a; height = b;}
    };
    class output1 
    {
        public:
           void output (int i);
    };
void output1::output (int i) 
{
    cout << i << endl;
}
class rectangle: public polygon, public output1 
{
    public:
    int area ()
    { 
        return (width * height); 
    }
};
class triangle: public polygon, public output1 
{
    public:
    int area ()
    {
        return (width * height / 2); 
    }
};
int main () 
{
    rectangle rect;
    triangle trgl;
    rect.set_values (4, 5);
    trgl.set_values (4, 5);
    rect.output (rect.area());
    trgl.output (trgl.area());
    return 0;
}

Select an option to see the answer and solution.

Which symbol is used to create multiple inheritances?

Select an option to see the answer and solution.

How many specifiers are used to derive a class?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is meant by multiple inheritance?

Select an option to see the answer and solution.