Search Header Logo

Quiz 12: Inheritance

Authored by Dr. Pal

Professional Development

University

Used 2+ times

Quiz 12: Inheritance
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

7 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Which access modifier in C++ allows the derived class to access the public members of the base class?

public

protected

private

friend

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the output of the following C++ code?

#include <iostream>

using namespace std;

class Base {

public:

int publicVar = 5;

protected:

int protectedVar = 10;

private:

int privateVar = 15;

};

class Derived : public Base {

public:

void display() {

cout << "Public Variable: " << publicVar << endl;

cout << "Protected Variable: " << protectedVar << endl;

cout << "Private Variable: " << privateVar << endl;

}

};

int main() {

Derived obj;

obj.display();

return 0;

}

Compilation Error

Public Variable: 5, Protected Variable: 10, Private Variable: 15

Public Variable: 5, Protected Variable: 10

Private Variable: 15

Answer explanation

The output of the code will be "Public Variable: 5, Protected Variable: 10". Since the derived class is inheriting publicly from the base class, it can access the public members of the base class. However, it cannot access the private members of the base class.

3.

MULTIPLE CHOICE QUESTION

10 sec • 1 pt

Which access modifier in C++ allows only the base class to access its members?

private

public

protected

friend

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the output of the following C++ code?

#include <iostream>

using namespace std;

class Base {

public:

int publicVar = 5;

protected:

int protectedVar = 10;

private:

int privateVar = 15;

};

class Derived : private Base {

public:

void display() {

cout << "Public Variable: " << publicVar << endl;

cout << "Protected Variable: " << protectedVar << endl;

cout << "Private Variable: " << privateVar << endl;

}

};

int main() {

Derived obj;

obj.display();

return 0;

}

Compilation Error

Public Variable: 5, Protected Variable: 10, Private Variable: 15

Public Variable: 5, Protected Variable: 10

Private Variable: 15

Answer explanation

The code will not compile because the derived class is inheriting privately from the base class, which means that the public and protected members of the base class will become private members of the derived class. As a result, the derived class cannot access the public and protected members of the base class using the dot operator.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the output of the following C++ code?

#include <iostream>

using namespace std;

class Base

{

public: int publicVar = 5;

protected: int protectedVar = 10;

private: int privateVar = 15;

};

class Derived : Base

{

public:

void display() {

cout << "Public Variable: " << publicVar << endl;

cout << "Protected Variable: " << protectedVar << endl;

cout << "Private Variable: " << privateVar << endl; }

};

int main()

{ Derived obj;

obj.display();

return 0; }

Compilation Error

Public Variable: 5, Protected Variable: 10, Private Variable: 15

Public Variable: 5, Protected Variable: 10

Private Variable: 15

6.

MULTIPLE CHOICE QUESTION

20 sec • 1 pt

Which access modifier in C++ allows a function in the base class to access the private members of the derived class?

public

protected

private

friend

7.

MULTIPLE CHOICE QUESTION

45 sec • 1 pt

What is the output of the following C++ code?

#include <iostream>

using namespace std;

class Base {

public:

void display(Derived obj) {

cout << "Public Variable: " << obj.publicVar << endl;

cout << "Protected Variable: " << obj.protectedVar << endl;

cout << "Private Variable: " << obj.privateVar << endl;

}

};

class Derived : public Base {

private:

int privateVar = 15;

public:

int publicVar = 5;

protected:

int protectedVar = 10;

friend void Base::display(Derived obj);

};

int main() {

Derived obj;

Base b;

b.display(obj);

return 0;

}

Compilation Error

Public Variable: 5, Protected Variable: 10, Private Variable: 15

Public Variable: 5, Protected Variable: 10

Private Variable: 15

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?