
Quiz 12: Inheritance
Authored by Dr. Pal
Professional Development
University
Used 2+ times

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

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?
Similar Resources on Wayground
10 questions
LingoTalk X Binus
Quiz
•
University
11 questions
TECNICAS ESTADISTICAS DE MUESTREO
Quiz
•
University
11 questions
Animal Legislation Re-cap
Quiz
•
University
10 questions
Quiz 1 NCM 104 CHN
Quiz
•
University - Professi...
10 questions
PHP BASIC
Quiz
•
University
10 questions
CNS-2nd QUIZ
Quiz
•
University
10 questions
Code of Ethics for Teachers
Quiz
•
KG - Professional Dev...
10 questions
ARCHv7 Quiz - Day 1
Quiz
•
University - Professi...
Popular Resources on Wayground
15 questions
Fractions on a Number Line
Quiz
•
3rd Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
54 questions
Analyzing Line Graphs & Tables
Quiz
•
4th Grade
22 questions
fractions
Quiz
•
3rd Grade
20 questions
Main Idea and Details
Quiz
•
5th Grade
20 questions
Context Clues
Quiz
•
6th Grade
15 questions
Equivalent Fractions
Quiz
•
4th Grade
Discover more resources for Professional Development
7 questions
How James Brown Invented Funk
Interactive video
•
10th Grade - University
5 questions
Helping Build the Internet: Valerie Thomas | Great Minds
Interactive video
•
11th Grade - University
12 questions
IREAD Week 4 - Review
Quiz
•
3rd Grade - University
23 questions
Subject Verb Agreement
Quiz
•
9th Grade - University
7 questions
Renewable and Nonrenewable Resources
Interactive video
•
4th Grade - University
19 questions
Review2-TEACHER
Quiz
•
University
15 questions
Pre2_STUDENT
Quiz
•
University
20 questions
Ch. 7 Quadrilateral Quiz Review
Quiz
•
KG - University