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 4 Question 54 Discussion

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;}
C) cout << A::y << A::z;
A) cout << y << z;
B) cout << y << A::z;
D) cout << B::y << B::z;

C++ Institute CPA-21-02 Exam - Topic 4 Question 54 Discussion

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

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;

}

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

0/2000 characters
Fanny
2 days ago
I think option B is the right choice.
upvoted 0 times
...
Billye
7 days ago
Exactly! B gives "23" because of A::z.
upvoted 0 times
...
Hui
12 days ago
D? But it prints B's z, not A's.
upvoted 0 times
...
Gwen
17 days ago
I feel like D could work too.
upvoted 0 times
...
Stephania
22 days ago
It accesses the base class's z.
upvoted 0 times
...
Hui
28 days ago
Why B?
upvoted 0 times
...
Stephania
1 month ago
I think option B is the right choice.
upvoted 0 times
...
Vicki
1 month ago
I’m not so sure about that, seems tricky.
upvoted 0 times
...
Chara
1 month ago
No way, it's definitely A!
upvoted 0 times
...
Janessa
2 months ago
Wait, does that mean A::z is 3?
upvoted 0 times
...
Marjory
2 months ago
I think it's actually D.
upvoted 0 times
...
Marion
2 months ago
Option B is the right choice!
upvoted 0 times
...
Gilma
2 months ago
Not sure about that, seems tricky.
upvoted 0 times
...
Zona
2 months ago
Totally agree with B!
upvoted 0 times
...
Kimi
2 months ago
Wait, does that mean A::z is different from B::z?
upvoted 0 times
...
Alona
3 months ago
I think it's actually option D.
upvoted 0 times
...
Malcolm
3 months ago
Option B is the right choice!
upvoted 0 times
...
Silvana
3 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.
upvoted 0 times
...
Quinn
3 months ago
I practiced a similar question, and I feel like option D could work since it prints the values from class B directly.
upvoted 0 times
...
Beata
3 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.
upvoted 0 times
...
Jacklyn
5 months ago
I think option B might be correct because it accesses the public variable from class A.
upvoted 0 times
...

Save Cancel