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

A. Executed
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;
    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


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

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

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

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

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

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

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

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

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

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


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

View Answer


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

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

View Answer


20. C++ Inheritance relationship is?

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

View Answer





Also check :


Discussion


* You must be logged in to add comment.