Back-End Programming Exam  >  Back-End Programming Tests  >  Test: Inheritence And Polymorphism - 2 - Back-End Programming MCQ

Test: Inheritence And Polymorphism - 2 - Back-End Programming MCQ


Test Description

15 Questions MCQ Test - Test: Inheritence And Polymorphism - 2

Test: Inheritence And Polymorphism - 2 for Back-End Programming 2024 is part of Back-End Programming preparation. The Test: Inheritence And Polymorphism - 2 questions and answers have been prepared according to the Back-End Programming exam syllabus.The Test: Inheritence And Polymorphism - 2 MCQs are made for Back-End Programming 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Inheritence And Polymorphism - 2 below.
Solutions of Test: Inheritence And Polymorphism - 2 questions in English are available as part of our course for Back-End Programming & Test: Inheritence And Polymorphism - 2 solutions in Hindi for Back-End Programming course. Download more important topics, notes, lectures and mock test series for Back-End Programming Exam by signing up for free. Attempt Test: Inheritence And Polymorphism - 2 | 15 questions in 30 minutes | Mock test for Back-End Programming preparation | Free important questions MCQ to study for Back-End Programming Exam | Download free PDF with solutions
Test: Inheritence And Polymorphism - 2 - Question 1

What are the things are inherited from the base class?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 1

These things can provide necessary information for the base class to make a logical decision.

Test: Inheritence And Polymorphism - 2 - Question 2

What is the output of the program?

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 2

This is a typical example of diamond problem of multiple inheritance. Here the base class member 'a' is inherited through both Derived1 and Derived2. So there are two copies of 'a' in DerivedDerived which makes the statement "cout << a;" ambiguous. The solution in C++ is to use virtual base classes. For example, the following program works fine and prints [sourcecode language="CPP"] #include using namespace std; class Base { protected: int a; public: Base() {a = 0;} }; class Derived1: virtual public Base { public: int c; }; class Derived2: virtual 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; } [/sourcecode]

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Inheritence And Polymorphism - 2 - Question 3

Which symbol is used to create multiple inheritance?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 3

For using multiple inheritance, simply specify each base class (just like in single inheritance), separated by a comma.

Test: Inheritence And Polymorphism - 2 - Question 4

What is the output of the program?

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 4

The base class members cannot be directly assigned using initializer list. We should call the base class constructor in order to initialize base class members. Following is error free program and prints "10 10" [sourcecode language="CPP" highlight="15"] #include 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): Base(i, j) {} void print() {cout << x <<" "<< y; } }; int main(void) { Derived q(10, 10); q.print(); return 0; } [/sourcecode]

Test: Inheritence And Polymorphism - 2 - Question 5

What is the syntax of inheritance of class?

Test: Inheritence And Polymorphism - 2 - Question 6

What is the output of the program?

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 6

When a class inherits from multiple classes, constructors of base classes are called in the same order as they are specified in inheritance.

Test: Inheritence And Polymorphism - 2 - Question 7

What is meant by multiple inheritance?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 7

Multiple inheritance enables a derived class to inherit members from more than one parent.

Test: Inheritence And Polymorphism - 2 - Question 8

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 8

When the inheritance is private, the private methods in base class are inaccessible in the derived class (in C++). For more information on inheritance Refer:Inheritance in C++ Option (A) is correct.

Test: Inheritence And Polymorphism - 2 - Question 9

Which of the following advantages we lose by using multiple inheritance?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 9

The benefit of dynamic binding and polymorphism is that they help making the code easier to extend but by multiple inheritance it makes harder to track.

Test: Inheritence And Polymorphism - 2 - Question 10

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 10

Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases. For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler (See this for details)
#include<iostream>
using namespace std;
 
class base {
  int arr[10];     
};
 
class b1: virtual public base { };
 
class b2: virtual public base { };
 
class derived: public b1, public b2 {};
 
int main(void)

  cout << sizeof(derived);
  return 0;

Test: Inheritence And Polymorphism - 2 - Question 11

What is meant by containership?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 11

class contains objects of other class types as its members

Test: Inheritence And Polymorphism - 2 - Question 12

What is the output of the program?

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

Test: Inheritence And Polymorphism - 2 - Question 13

How many constructors can present in a class?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 13

There can be multiple constructors of the same class, provided they have different signatures.

Test: Inheritence And Polymorphism - 2 - Question 14

What is the output of the program?

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

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 14

A base class pointer can point to a derived class object, but we can only access base class member or virtual functions using the base class pointer.

Test: Inheritence And Polymorphism - 2 - Question 15

What does derived class does not inherit from the base class?

Detailed Solution for Test: Inheritence And Polymorphism - 2 - Question 15

The derived class inherit everything from the base class except the given things.

Information about Test: Inheritence And Polymorphism - 2 Page
In this test you can find the Exam questions for Test: Inheritence And Polymorphism - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Inheritence And Polymorphism - 2, EduRev gives you an ample number of Online tests for practice
Download as PDF