C++ Programming Multiple Choice Questions - Inheritance

This section focuses on the "Inheritance" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

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

A. Inaccessible
B. Accessible
C. Protected
D. Public

View Answer


2. Which design patterns benefit from the multiple inheritances?

A. Adapter and observer pattern
B. Code pattern
C. Glue pattern
D. None of the mentioned

View Answer


3. What is meant by multiple inheritance?

A. Deriving a base class from derived class
B. Deriving a derived class from base class
C. Deriving a derived class from more than one base class
D. None of the mentioned

View Answer


4. What will be the order of execution of base class constructors in the following method of inheritance.class a: public b, public c {...};

A. b(); c(); a();
B. c(); b(); a();
C. a(); b(); c();
D. b(); a(); c();

View Answer


5. Inheritance allow in C++ Program?

A. Class Re-usability
B. Creating a hierarchy of classes
C. Extendibility
D. All of the above

View Answer


6. Can we pass parameters to base class constructor though derived class or derived class constructor?

A. Yes
B. No
C. May Be
D. Can't Say

View Answer


7. What are the things are inherited from the base class?

A. Constructor and its destructor
B. Operator=() members
C. Friends
D. All of the above

View Answer


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

A. Dynamic binding
B. Polymorphism
C. Both Dynamic binding & Polymorphism
D. None of the mentioned

View Answer


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

A. Executed
B. Error
C. Runtime error
D. None of the mentioned

View Answer


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

A. long* string
B. a long st*ring
C. Depends on compiler
D. None of the mentione

View Answer


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

A. Inherited
B. Error
C. Runtime error
D. None of the mentioned

View Answer


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

A. Compiler Dependent
B. Base1 Base2 Derived
C. Base2 Base1 Derived
D. Compiler Error

View Answer


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

A. Base1 Base2 Derived
B. Derived Base2 Base1
C. Derived
D. Compiler Dependent

View Answer


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

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

View Answer


15. 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

View Answer


16. 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

View Answer


17. State whether the following statements about inheritance are True or False. i) A public member of a class can be accessed by its own objects using the dot operator. ii) While inheriting, the private members of the base class will never become the members of its derived class.

A. True, False
B. False, True
C. True, True
D. False, False

View Answer


18. In inheritance, order of execution of base class and derived class destructors are:

A. Base to derived
B. Derived to base
C. Random order
D. None of the above

View Answer


19. What is the difference between protected and private access specifiers in inheritance?

A. Private member is not inheritable and not accessible in derived class.
B. Protected member is inheritable and also accessible in derived class.
C. Both are inheritable but private is accessible in the derived class.
D. Both are inheritable but protected is not accessible in the derived class.

View Answer


20. Which value is placed in the base class?

A. Derived values
B. Default type values
C. Both A & B
D. None of the mentioned

View Answer


21. The friend functions and the member functions of a friend class can directly access the______________ data.

A. Private and protected
B. Private and public
C. Protected and public
D. Private, protected and public

View Answer


22. What will be the order of execution of base class constructors in the following method of inheritance. class a: public b, virtual public c {...};

A. b(); c(); a();
B. c(); b(); a();
C. a(); b(); c();
D. b(); a(); c();

View Answer


23. class X, class Y and class Z are derived from class BASE. This is ______ inheritance.

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

View Answer


24. Reusability of the code can be achieved in CPP through ______ .

A. Polymorphism
B. Encapsulation
C. Inheritance
D. Both A and C

View Answer


25. What will be the output of the following program?

         
Note:Includes all required header files
class find {
public:
   void print()  { cout <<" In find"; }
};
  
class course : public find {
public:
   void print() { cout <<" In course"; }
};
  
class tech: public course { };
  
int main(void)
{
  tech t; 
  t.print();
  return 0;
}

A. In find
B. In course
C. Compiler Error: Ambiguous call to print()
D. None of the above

View Answer


26. Which symbol is used to create multiple inheritance?

A. Dot
B. Comma
C. Dollar
D. None of the above

View Answer


27. ___________ inheritance may lead to duplication of inherited members from a "grandparent" base class.

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

View Answer


28. C++ Inheritance relationship is?

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

View Answer


29. Private members of the class are not inheritable.

A. TRUE
B. FALSE
C. May Be
D. Can't Say

View Answer





Also check :


Discussion



* You must be logged in to add comment.