Cpp Inheritance MCQs 4

16.
Assume that an integer takes 2 bytes and there is no alignment in following classes, predict the output.

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

A. 30
B. 60
C. 0
D. 120

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

A. error: invalid conversion from “Derived*” to “Base*”
B. No Compiler Error
C. error: invalid conversion from “Base*” to “Derived*”
D. Runtime Error

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

A. Base::lfc(int i) called
B. Derived::lfc() called
C. Base::lfc() called
D. Compiler Error


19.
___________ inheritance may lead to duplication of inherited members from a “grandparent” base class.

A. Multipath
B. Multiple
C. Multilevel
D. Hierarchical

20.
C++ Inheritance relationship is?

A. Association
B. Is-A
C. Has-A
D. None of the above