Which code, inserted at line 19, generates the output "23"?
#include
#include
using namespace std;
class A {
int x;
protected:
int y;
public:
int z;
A() { x=1; y=2; z=3; }
};
class B : public A {
string z;
public:
int y;
void set() { y = 4; z = "John"; }
void Print() {
//insert code here
}
};
int main () {
B b;
b.set();
b.Print();
return 0;
}
Suggested Answer: C
Get Premium CPA-21-02 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
31 comments · newest first
CA
Caren
3 days ago
Option A won't work. It tries to access y directly.
KA
Kayleigh
9 days ago
I'm leaning towards B as well. It outputs 2 and 3, which is 23.
RO
Ronna
14 days ago
Option C seems off. A::y is protected, can't access it.
DA
Dallas
19 days ago
I feel like option D could work too. It accesses the public members.
VE
Vernell
24 days ago
I think option B is correct. It uses the right scope.
SH
Shala
29 days ago
True, B's y is 4, and z is a string.
LA
Lai
1 month ago
D prints B's y and z, but won't give "23".
EA
Earlean
1 month ago
But what about option D?
FA
Fanny
1 month ago
It accesses the public z from class A.
SH
Shala
2 months ago
Why B?
FA
Fanny
2 months ago
I think option B is the right choice.
BI
Billye
2 months ago
Exactly! B gives "23" because of A::z.
HU
Hui
2 months ago
D? But it prints B's z, not A's.
GW
Gwen
2 months ago
I feel like D could work too.
ST
Stephania
3 months ago
It accesses the base class's z.
HU
Hui
3 months ago
Why B?
ST
Stephania
3 months ago
I think option B is the right choice.
VI
Vicki
3 months ago
I’m not so sure about that, seems tricky.
CH
Chara
3 months ago
No way, it's definitely A!
JA
Janessa
3 months ago
Wait, does that mean A::z is 3?
MA
Marjory
4 months ago
I think it's actually D.
MA
Marion
4 months ago
Option B is the right choice!
GI
Gilma
4 months ago
Not sure about that, seems tricky.
ZO
Zona
4 months ago
Totally agree with B!
KI
Kimi
4 months ago
Wait, does that mean A::z is different from B::z?
AL
Alona
4 months ago
I think it's actually option D.
MA
Malcolm
5 months ago
Option B is the right choice!
SI
Silvana
5 months ago
I'm a bit confused about the protected members. Does option A even work? I thought protected members couldn't be accessed like that.
QU
Quinn
5 months ago
I practiced a similar question, and I feel like option D could work since it prints the values from class B directly.
BE
Beata
5 months ago
I'm not sure, but I remember something about scope resolution. Maybe option C is the right choice since it uses A::z.
JA
Jacklyn
7 months ago
I think option B might be correct because it accesses the public variable from class A.