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 6 Question 58 Discussion

What happens when you attempt to compile and run the following code?#include using namespace std;class A {public:int x;A() { x=0;}};class B : protected A {public:int y;using A::x;B(int y) {this?>y = y;}void Print() { cout
A) It prints: 05
B) It prints: 0
C) It prints: 5
D) It prints: 15

C++ Institute CPA-21-02 Exam - Topic 6 Question 58 Discussion

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

What happens when you attempt to compile and run the following code?

#include

using namespace std;

class A {

public:

int x;

A() { x=0;}

};

class B : protected A {

public:

int y;

using A::x;

B(int y) {this?>y = y;}

void Print() { cout << x << y; }

};

int main () {

B b(5);

b.Print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

0/2000 characters
Dick
3 days ago
I think it prints 05. The x is initialized to 0.
upvoted 0 times
...
Diane
8 days ago
Definitely prints 05, that's how inheritance works!
upvoted 0 times
...
Venita
13 days ago
Wait, how does it even access x like that?
upvoted 0 times
...
Alishia
19 days ago
No way, it should be 15!
upvoted 0 times
...
Shawn
24 days ago
I think it should print 0, right?
upvoted 0 times
...
Francesco
29 days ago
It prints: 05
upvoted 0 times
...
Glenn
1 month ago
I feel like it should print 05 since x is initialized to 0 and y is set to 5 in the constructor.
upvoted 0 times
...
Skye
1 month ago
I practiced a similar question where the output was affected by member access. This one seems tricky!
upvoted 0 times
...
Alease
1 month ago
I'm not entirely sure, but I remember something about protected members. Maybe it won't print y at all?
upvoted 0 times
...
Yoko
2 months ago
I think the constructor for B isn't setting x correctly, so it might just print 0.
upvoted 0 times
...

Save Cancel