
Quiz 12: Inheritance

Quiz
•
Professional Development
•
University
•
Hard
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
Similar Resources on Wayground
10 questions
Ingeniería

Quiz
•
University
7 questions
Plotting Graphs

Quiz
•
6th Grade - University
7 questions
Clean Code

Quiz
•
University
10 questions
caractériser votre opportunité de marché

Quiz
•
University
10 questions
GFG Quiz

Quiz
•
11th Grade - Professi...
10 questions
KUIS SDN KANDAWATI 2

Quiz
•
University
10 questions
Multithreading

Quiz
•
University
10 questions
INTRODUCCION A LA PROGRAMACION

Quiz
•
6th Grade - University
Popular Resources on Wayground
10 questions
Lab Safety Procedures and Guidelines

Interactive video
•
6th - 10th Grade
10 questions
Nouns, nouns, nouns

Quiz
•
3rd Grade
10 questions
9/11 Experience and Reflections

Interactive video
•
10th - 12th Grade
25 questions
Multiplication Facts

Quiz
•
5th Grade
11 questions
All about me

Quiz
•
Professional Development
22 questions
Adding Integers

Quiz
•
6th Grade
15 questions
Subtracting Integers

Quiz
•
7th Grade
9 questions
Tips & Tricks

Lesson
•
6th - 8th Grade
Discover more resources for Professional Development
21 questions
Spanish-Speaking Countries

Quiz
•
6th Grade - University
20 questions
Levels of Measurements

Quiz
•
11th Grade - University
7 questions
Common and Proper Nouns

Interactive video
•
4th Grade - University
12 questions
Los numeros en español.

Lesson
•
6th Grade - University
7 questions
PC: Unit 1 Quiz Review

Quiz
•
11th Grade - University
7 questions
Supporting the Main Idea –Informational

Interactive video
•
4th Grade - University
12 questions
Hurricane or Tornado

Quiz
•
3rd Grade - University
7 questions
Enzymes (Updated)

Interactive video
•
11th Grade - University