Which code, inserted at line 10, generates the output "2?1"?
#include
#include
using namespace std;
class A {
protected:
int y;
public:
int z;
};
//insert code here
public:
void set() {
y = 2;
z = 3;
}
void Print() { cout << y << z; }
};
int main () {
B b;
b.set();
b.z = ?1;
b.Print();
return 0;
}
Suggested Answer: B
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
21 comments · newest first
RO
Rodrigo
5 days ago
I agree, B makes sense.
VA
Valentine
10 days ago
Because it allows access to public members.
CA
Carolynn
15 days ago
Why B?
VA
Valentine
20 days ago
I think option B is the right choice.
CY
Cyril
25 days ago
Let’s go with B then!
CA
Carry
1 month ago
I agree, B is clear. It’s straightforward.
DE
Deeanna
1 month ago
True, B gives us the output "2?1".
PE
Percy
1 month ago
Exactly, we need public access for z. B is the best fit.
CY
Cyril
2 months ago
But A won't let us access z directly.
DE
Deeanna
2 months ago
I’m leaning towards A. Private inheritance could work too.
PE
Percy
2 months ago
Yeah, B makes sense. It sets z to 1.
CY
Cyril
2 months ago
I think option B is the right choice. It allows access to z.
NA
Nakisha
2 months ago
Really? I thought it would be C for some reason.
TE
Teri
2 months ago
Wait, how does that even print "2?1"?
PO
Pok
3 months ago
Definitely B, since it allows access to z.
TI
Tish
3 months ago
I think A would work too, but not sure.
LA
Laquanda
3 months ago
Option B is the right choice!
JA
Jackie
3 months ago
I feel like option D could work too, but then we wouldn't have access to the protected member y at all.
DE
Della
3 months ago
I'm a bit confused about the protected members. Would option C allow us to access y in the derived class?
VA
Vallie
3 months ago
I remember practicing a similar question where we had to determine access levels in inheritance. I think option A might restrict access too much.
BE
Bethanie
4 months ago
I think option B makes sense because it allows access to the public members of class A, but I'm not entirely sure if that's what we need for the output.