ADVERTISEMENT
ADVERTISEMENT

C++ Inheritance aptitude questions and answers

C++ Polymorphism & Function Overloading Aptitude: This section contains C++ Polymorphism & Function Overloading Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 01, 2021

1) Which concept of OOP creates a hierarchy of classes?

  1. Enumeration
  2. Abstraction
  3. Inheritance
  4. None of the above


3) How many specifiers are used in to derived a class?

  1. 1
  2. 2
  3. 3
  4. 4

4) What is multiple inheritance?

  1. Creating multiple classes
  2. Deriving a derived class from more than one base class
  3. Deriving a derived class from base class
  4. Deriving a base class from derived class

5) What will be the output of the following code?

#include <iostream>
#include <string>
using namespace std;

class A {
    int a, b;
    float d;

public:
    void change(int i)
    {
        a = i;
    }
    void value_of_a()
    {
        cout << a;
    }
};

class B : private A {
};

int main(int argc, char const* argv[])
{
    B b;
    cout << sizeof(B);
    return 0;
}

Options:

  1. 1
  2. 12
  3. ERROR
  4. Segmentation fault

6) Choose the correct answer (not-output) with the following code.

#include <iostream>
using namespace std;

class polygon {
protected:
    int width, height;

public:
    void set_values(int a, int b)
    {
        width = a;
        height = b;
    }
};

class output1 {
public:
    void output(int i);
};

void output1::output(int i)
{
    cout << i << endl;
}

class rectangle : public polygon, public output1 {
public:
    int area()
    {
        return (width * height);
    }
};

class triangle : public polygon, public output1 {
public:
    int area()
    {
        return (width * height / 2);
    }
};

int main()
{
    rectangle rect;
    triangle trgl;
    rect.set_values(4, 5);
    trgl.set_values(4, 5);
    rect.output(rect.area());
    trgl.output(trgl.area());
    return 0;
}

Options:

  1. Inherited
  2. Compile-time Error
  3. Runtime Error
  4. None of the mentioned

7) Which of the following are correct types of inheritance in C++?

  1. Single Inheritance
  2. Multiple Inheritance
  3. Canonical Inheritance
  4. Hybrid Inheritance

Options:

  1. A and B
  2. A, B, and C
  3. A, B, and D
  4. A, B, C, and D

8) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
public:
    Base()
    {
        cout << "Base Ctor Called" << endl;
    }
};

class Derived::Base {
public:
    Derived()
    {
        cout << "Derived Ctor Called" << endl;
    }
};

int main()
{
    Derived D;

    return 0;
}

Options:

  1. Derived Ctor Called
  2. Base Ctor Called
    Derived Ctor Called
  3. Compile-time error
  4. Runtime error

9) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
public:
    Base()
    {
        cout << "Base Ctor Called" << endl;
    }
};

class Derived : Base {
public:
    Derived()
    {
        cout << "Derived Ctor Called" << endl;
    }
};

int main()
{
    Derived D;

    return 0;
}

Options:

  1. Derived Ctor Called
  2. Base Ctor Called
    Derived Ctor Called
  3. Compile-time error
  4. Runtime exception

10) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
public:
    Base()
    {
        cout << "Base Ctor Called" << endl;
    }
    ~Base()
    {
        cout << "Base Dtor Called" << endl;
    }
};

class Derived : Base {
public:
    Derived()
    {
        cout << "Derived Ctor Called" << endl;
    }
    ~Derived()
    {
        cout << "Derived Dtor Called" << endl;
    }
};

int main()
{
    Derived D;

    return 0;
}

Options:

  1. Base Ctor Called
    Derived Ctor Called
    Base Dtor Called
    Derived Dtor Called
  2. Base Ctor Called
    Derived Ctor Called
    Derived Dtor Called
    Base Dtor Called
  3. Compile-time error
  4. Runtime error

11) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    void fun1()
    {
        cout << "Fun1() called ";
    }
};

class Derived : Base {
public:
    void fun2()
    {
        cout << "Fun2() called ";
    }
};

int main()
{
    Derived D;

    D.fun2();

    return 0;
}

Options:

  1. Fun2() called
  2. Fun1() called Fun2() called
  3. Compile-time error
  4. Runtime error

12) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
public:
    void fun1()
    {
        cout << "Base Fun1() called ";
    }
};

class Derived : Base {
public:
    void fun1()
    {
        cout << "Derived Fun1() called ";
    }
};

int main()
{
    Derived D;

    D.fun1();

    return 0;
}

Options:

  1. Base Fun1() called
  2. Derived Fun1() called
  3. Compile-time error
  4. Runtime error

13) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
    int ABC;
};

class Derived1 : Base {
};

class Derived2 : Derived1 {
};

int main()
{
    Derived2 D;

    cout << sizeof(D);

    return 0;
}

Options:

  1. 4
  2. 8
  3. 16
  4. Error

14) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base {
};

class Derived1 : Base {
};

class Derived2 : Base {
};

class Derived3 : Derived1, Derived2 {
};

int main()
{
    cout << sizeof(Derived3);

    return 0;
}

Options:

  1. 0
  2. 2
  3. 4
  4. Error

15) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
    int A;
};

class Derived1 : Base {
};

class Derived2 : Base {
};

class Derived3 : Derived1, Derived2 {
};

int main()
{
    cout << sizeof(Derived3);

    return 0;
}

Options:

  1. 2
  2. 4
  3. 8
  4. 16

16) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
    int A;
};

class Derived1 : Base {
    int B;
};

class Derived2 : Base {
};

class Derived3 : Derived1, Derived2 {
};

int main()
{
    cout << sizeof(Derived3);

    return 0;
}

Options:

  1. 8
  2. 12
  3. 16
  4. 32

17) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base1 {
public:
    void fun()
    {
        cout << "Base1 fun() called";
    }
};

class Base2 {
public:
    void fun()
    {
        cout << "Base2 fun() called";
    }
};

class Derived : Base1, Base2 {
};

int main()
{
    Derived D;

    D.fun();

    return 0;
}

Options:

  1. Base1 fun() called
  2. Base2 fun() called
  3. Compile-time error
  4. Runtime error

18) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Base1 {
public:
    void fun()
    {
        cout << "Base1 fun() called";
    }
};

class Base2 {
public:
    void fun()
    {
        cout << "Base2 fun() called";
    }
};

class Derived : public Base1, public Base2 {
};

int main()
{
    Derived D;

    D.Base1::fun();

    return 0;
}

Options:

  1. Base1 fun() called
  2. Base2 fun() called
  3. Compile-time error
  4. Runtime error

19) The protected and public member of the base class becomes protected members in the child class?

  1. Yes
  2. No

20) What is the correct output of the following code?

#include <iostream>
using namespace std;

class Sample {
protected:
    void fun()
    {
        cout << "fun() called";
    }
};

int main()
{
    Sample S;

    cout << S.fun();

    return 0;
}

Options:

  1. fun() called
  2. Compile-time error
  3. Syntax error
  4. Runtime error

21) Hybrid inheritance is the combination of any two types of inheritance?

  1. Yes
  2. No



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.