Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - Topic 7 Question 57 Discussion

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 herepublic:void set() {y = 2;z = 3;}void Print() { cout
B) class B : public A {
A) class B : private A {
C) class B : protected A {
D) class B {

C++ Institute CPA-21-02 Exam - Topic 7 Question 57 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 57
Topic #: 7
[All CPA-21-02 Questions]

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;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

0/2000 characters
Valentine
2 days ago
Because it allows access to public members.
upvoted 0 times
...
Carolynn
7 days ago
Why B?
upvoted 0 times
...
Valentine
12 days ago
I think option B is the right choice.
upvoted 0 times
...
Cyril
17 days ago
Let’s go with B then!
upvoted 0 times
...
Carry
22 days ago
I agree, B is clear. It’s straightforward.
upvoted 0 times
...
Deeanna
28 days ago
True, B gives us the output "2?1".
upvoted 0 times
...
Percy
1 month ago
Exactly, we need public access for z. B is the best fit.
upvoted 0 times
...
Cyril
1 month ago
But A won't let us access z directly.
upvoted 0 times
...
Deeanna
1 month ago
I’m leaning towards A. Private inheritance could work too.
upvoted 0 times
...
Percy
2 months ago
Yeah, B makes sense. It sets z to 1.
upvoted 0 times
...
Cyril
2 months ago
I think option B is the right choice. It allows access to z.
upvoted 0 times
...
Nakisha
2 months ago
Really? I thought it would be C for some reason.
upvoted 0 times
...
Teri
2 months ago
Wait, how does that even print "2?1"?
upvoted 0 times
...
Pok
2 months ago
Definitely B, since it allows access to z.
upvoted 0 times
...
Tish
2 months ago
I think A would work too, but not sure.
upvoted 0 times
...
Laquanda
3 months ago
Option B is the right choice!
upvoted 0 times
...
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.
upvoted 0 times
...
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?
upvoted 0 times
...
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.
upvoted 0 times
...
Bethanie
3 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.
upvoted 0 times
...

Save Cancel