MCQ on Inheritance in C++: Multiple Choice Questions on C++ Inheritance

MCQ on inheritance in C++ with answers and explanations for placement tests and job interviews. This C++ MCQ on inheritance is useful for the campus placement for all freshers including Engineering Students, MCA students, Computer and IT Engineers, etc.

Our C++ inheritance quiz (C++ inheritance multiple Choice Questions ) focuses on all areas of C++ inheritance and its concept. We will regularly update the Quiz on the C++ inheritance and most interesting thing is that questions come in a random sequence. So every time you will feel new questions.

You may have come across several C++ courses during your search for learning the C++ language. Our team of experts has carefully analyzed some C++ courses for you. You can check the courses, Trial of some courses is free.

 

Guideline of MCQ on inheritance in C++:

This MCQ on inheritance in C++ is intended for checking your knowledge of C++ inheritance. It takes 20 minutes to pass the C++ quiz on inheritance. If you don’t finish the C++ inheritance quiz within the mentioned time, all the unanswered questions will count as wrong. Every unanswered question will count as wrong. Quiz on inheritance in C++ has randomization features that give you a new question set at every attempt.

In this MCQ on inheritance in C++, we have also implemented a feature that not allowed the user to see the next question or finish the quiz without attempting the current C++ inheritance quiz.

2 votes, 5 avg

You have 20 minutes to take the MCQs on Inheritance in C++

Your time has been Over.


Inheritance Quiz C++

C++ MCQ

MCQ on inheritance in C++: Multiple Choice Questions and Answers on inheritance in C++

1 / 15

#include<iostream>
using namespace std;
 
class Base
{
public:
    void show()
    {
        cout<<" In Base ";
    }
};
 
class Derived: public Base
{
public:
    int x;
    void show()
    {
        cout<<"In Derived ";
    }
    Derived()
    {
        x = 10;
    }
};
 
int main(void)
{
    Base *bp, b;
    Derived d;
    bp = &d;
    bp->show();
    cout << bp->x;    
    return 0;
}

 

2 / 15

Consider the below C++ program.

#include<iostream>
using namespace std;
class A
{
public:
     A(){ cout <<"1";}
     A(const A &obj){ cout <<"2";}
};
 
class B: virtual A
{
public:
    B(){cout <<"3";}
    B(const B & obj){cout<<"4";}
};
 
class C: virtual A
{
public:
   C(){cout<<"5";}
   C(const C & obj){cout <<"6";}
};
 
class D:B,C
{
public:
    D(){cout<<"7";}
    D(const D & obj){cout <<"8";}
};
 
int main()
{
   D d1;
   D d(d1);
}

 

3 / 15

#include<iostream>
using namespace std;
 
class Base {
public:
    void fun()          {    cout << "Base::fun() called";     }
    void fun(int i)     {   cout << "Base::fun(int i) called";  }
};
 
class Derived: public Base  {
public:
    void fun()   {     cout << "Derived::fun() called";   }
};
 
int main()  {
    Derived d;
    d.Base::fun(5);
    return 0;
}

 

4 / 15

Output of following program?

#include <iostream>
#include<string>
using namespace std;
 
class Base
{
public:
    virtual string print() const
    {
        return "This is Base class";
    }
};
 
class Derived : public Base
{
public:
    virtual string print() const
    {
        return "This is Derived class";
    }
};
 
void describe(Base p)
{
    cout << p.print() << endl;
}
 
int main()
{
    Base b;
    Derived d;
    describe(b);
    describe(d);
    return 0;
}

 

1. 

This is Derived class
This is Base class

2. 

This is Base class
This is Derived class

3. 

This is Base class
This is Base class

4. 

Compiler Error

 

 

5 / 15

Output?

#include <iostream>  
using namespace std;
 
class Base1 {
 public:
     ~Base1()  { cout << " Base1's destructor" << endl; }
};
   
class Base2 {
 public:
     ~Base2()  { cout << " Base2's destructor" << endl; }
};
   
class Derived: public Base1, public Base2 {
   public:
     ~Derived()  { cout << " Derived's destructor" << endl; }
};
   
int main()
{
   Derived d;
   return 0;
}

 

1. 

Base1's destructor
Base2's destructor
Derived's destructor

2. 

Derived's destructor
Base2's destructor
Base1's destructor

3. 

Derived's destructor

4. 

Compiler Dependent

 

6 / 15

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

#include<iostream>
using namespace std;
 
class base {
    int arr[10];
};
 
class b1: public base { };
 
class b2: public base { };
 
class derived: public b1, public b2 {};
 
int main(void)
{
  cout << sizeof(derived);
  return 0;
}

 

7 / 15

#include<iostream>
  
using namespace std;
class P {
public:
   void print()  { cout <<" Inside P"; }
};
  
class Q : public P {
public:
   void print() { cout <<" Inside Q"; }
};
  
class R: public Q { };
  
int main(void)
{
  R r; 
  r.print();
  return 0;
}

 

8 / 15

#include<iostream>
using namespace std;
 
class Base {};
 
class Derived: public Base {};
 
int main()
{
    Base *bp = new Derived;
    Derived *dp = new Base;
}

 

9 / 15

#include<iostream>
  
using namespace std;
class Base1 {
 public:
     Base1()
     { cout << " Base1's constructor called" << endl;  }
};
  
class Base2 {
 public:
     Base2()
     { cout << "Base2's constructor called" << endl;  }
};
  
class Derived: public Base1, public Base2 {
   public:
     Derived()
     {  cout << "Derived's constructor called" << endl;  }
};
  
int main()
{
   Derived d;
   return 0;
}

1. 

Compiler Dependent

2. 

Base1′s constructor called
Base2′s constructor called
Derived’s constructor called

3. 

Base2′s constructor called
Base1′s constructor called
Derived’s constructor called

4. 

Compiler Error

10 / 15

#include<iostream>
using namespace std;
 
class Base
{
protected:
    int a;
public:
    Base() {a = 0;}
};
 
class Derived1:  public Base
{
public:
    int c;
};
 
 
class Derived2:  public Base
{
public:
    int c;
};
 
class DerivedDerived: public Derived1, public Derived2
{
public:
    void show()  {   cout << a;  }
};
 
int main(void)
{
    DerivedDerived d;
    d.show();
    return 0;
}

 

11 / 15

When the inheritance is private, the private methods in base class are __________ in the derived class (in C++).

12 / 15

#include<iostream>
using namespace std;
 
class Base1
{
public:
    char c;
};
 
class Base2
{
public:
    int c;
};
 
class Derived: public Base1, public Base2
{
public:
    void show()  { cout << c; }
};
 
int main(void)
{
    Derived d;
    d.show();
    return 0;
}

 

13 / 15

#include<iostream>
using namespace std;
 
class Base
{
public :
    int x, y;
public:
    Base(int i, int j){ x = i; y = j; }
};
 
class Derived : public Base
{
public:
    Derived(int i, int j):x(i), y(j) {}
    void print() {cout << x <<" "<< y; }
};
 
int main(void)
{
    Derived q(10, 10);
    q.print();
    return 0;
}

 

14 / 15

Output?

#include<iostream>
using namespace std;
 
class Base {
private:
     int i, j;
public:
    Base(int _i = 0, int _j = 0): i(_i), j(_j) { }
};
class Derived: public Base {
public:
     void show(){
        cout<<" i = "<<i<<"  j = "<<j;
     }
};
int main(void) {
  Derived d;
  d.show();
  return 0;
}

 

15 / 15

#include<iostream>
using namespace std;
 
class Base
{
public:
    int fun()  { cout << "Base::fun() called"; }
    int fun(int i)  { cout << "Base::fun(int i) called"; }
};
 
class Derived: public Base
{
public:
    int fun() {  cout << "Derived::fun() called"; }
};
 
int main()
{
    Derived d;
    d.fun(5);
    return 0;
}

 

Your score is

The average score is 0%

0%

Recommended Articles for you: