C++ Programming MCQ - Inheritance
11. What is the output of this program?
Note:Includes all required header files
using namespace std;
class Base
{
public:
Base(){}
~Base(){}
protected:
private:
};
class Derived:public Base
{
public:
Derived(){}
Derived(){}
private:
protected:
};
int main()
{
cout << "Executed" << endl;
}
View Answer
12. What is the output of this program?
Note:Includes all required header files
using namespace std;
int main()
{
string s = "a long string";
s.insert(s.size() / 2, " * ");
cout << s << endl;
return 0;
}
View Answer
13. What is the output of this program?
Note:Includes all required header files
using namespace std;
struct a
{
int p;
};
struct b
{
int* x;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->x = 0;
cout << "Inherited";
return 0;
}
View Answer
14. What is the output of this program?
Note:Includes all required header files
using namespace std;
class Base1 {
public:
Base1()
{ cout << " Base1" << endl; }
};
class Base2 {
public:
Base2()
{ cout << "Base2" << endl; }
};
class Derived: public Base1, public Base2 {
public:
Derived()
{ cout << "Derived" << endl; }
};
int main()
{
Derived d;
return 0;
}
View Answer
15. Which of the following is true about the following program
Note:Includes all required header files
using namespace std;
class Base1 {
public:
~Base1() { cout << " Base1" << endl; }
};
class Base2 {
public:
~Base2() { cout << " Base2" << endl; }
};
class Derived: public Base1, public Base2 {
public:
~Derived() { cout << " Derived" << endl; }
};
int main()
{
Derived d;
return 0;
}
View Answer
16. Assume that an integer takes 2 bytes and there is no alignment in following classes, predict the output.
Note:Includes all required header files
using namespace std;
class base {
int arr[15];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}
View Answer
17. What will be the output of this program?
#include <iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Base *p = new Derived;
Derived *q = new Base;
}
View Answer
18. What will be the output of the following program?
#include <iostream>
using namespace std;
class Base
{
public:
int lfc() { cout << "Base::lfc() called"; }
int lfc(int i) { cout << "Base::lfc(int i) called"; }
};
class Derived: public Base
{
public:
int lfc() { cout << "Derived::lfc() called"; }
};
int main()
{
Derived d;
d.lfc(5);
return 0;
}
View Answer
19. ___________ inheritance may lead to duplication of inherited members from a "grandparent" base class.
View Answer
20. C++ Inheritance relationship is?
View Answer
Also check :