Quiz 12: Inheritance

Quiz 12: Inheritance

University

7 Qs

quiz-placeholder

Similar activities

Gráficos estadísticos

Gráficos estadísticos

University

10 Qs

Simple Easy Basic Lua Quiz

Simple Easy Basic Lua Quiz

4th Grade - Professional Development

10 Qs

Transducers

Transducers

University

12 Qs

Performance Management

Performance Management

University

10 Qs

ServSafe Ch. 5 Review Quiz

ServSafe Ch. 5 Review Quiz

10th Grade - Professional Development

10 Qs

TEST SEGUNDO PARCIAL

TEST SEGUNDO PARCIAL

2nd Grade - University

10 Qs

Funciones

Funciones

7th Grade - University

10 Qs

Variáveis javascript

Variáveis javascript

University

9 Qs

Quiz 12: Inheritance

Quiz 12: Inheritance

Assessment

Quiz

Professional Development

University

Hard

Created by

Dr. Pal

Used 2+ times

FREE Resource

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