New Year Sale 2026! 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 38 Discussion

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

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

#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:

void set() {

y = 4;

z = "John";

}

void Print() {

cout << y << z;

}

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

0/2000 characters
Phung
2 months ago
The protected variable y is accessible in B, so 4 makes sense.
upvoted 0 times
...
Joanna
2 months ago
I thought the output would be 23, not 4John.
upvoted 0 times
...
Irma
3 months ago
Totally agree, it's definitely 4John!
upvoted 0 times
...
Keshia
3 months ago
Wait, how does it print 4? I thought it would be 2.
upvoted 0 times
...
Tamar
3 months ago
It prints: 4John
upvoted 0 times
...
Edmond
3 months ago
I’m confused about the `z` variable in class B. Does it overshadow the `z` in class A? I might lean towards 2John, but I’m not certain.
upvoted 0 times
...
Benedict
4 months ago
I practiced a similar question where protected members were accessed in derived classes. I feel like it should be 4John.
upvoted 0 times
...
Ronald
4 months ago
I'm not entirely sure, but I remember something about variable shadowing. Maybe it prints 2John instead?
upvoted 0 times
...
Luisa
4 months ago
I think the output will be 4John because the `set()` function modifies the protected variable `y` and the `z` in class B.
upvoted 0 times
...
Kaycee
4 months ago
I'm not entirely sure about this one. There seem to be a few things going on with the variable names and access modifiers. I'll need to work through it step-by-step to figure out the correct output.
upvoted 0 times
...
Jamal
4 months ago
Okay, I've got this! The key is understanding that the protected member `y` in the base class `A` can be accessed by the derived class `B`. So the output should be `4John`.
upvoted 0 times
...
Luther
5 months ago
Hmm, I'm a bit confused by the class hierarchy and access specifiers here. I'll need to review my notes on inheritance and think through how the derived class can access the base class members.
upvoted 0 times
...
Shalon
5 months ago
This looks like a question on inheritance and access modifiers. I'll need to carefully trace the code to understand how the variables and methods are being accessed across the derived class.
upvoted 0 times
...
Isaac
10 months ago
Wow, talk about a trick question! I'm going to guess C, just because the output seems a bit unexpected. Who knows, maybe the compiler will throw a curveball!
upvoted 0 times
Hildegarde
9 months ago
User3: Yeah, it's definitely a tricky one
upvoted 0 times
...
Martina
9 months ago
User2: I agree, the output does seem a bit unexpected
upvoted 0 times
...
Novella
9 months ago
User1: I think it prints: 23
upvoted 0 times
...
...
Elise
10 months ago
This is a tricky one, but I'm going with A. The protected `y` is accessible in `B`, and the `z` in `B` hides the `z` in `A`, so it should print 4John.
upvoted 0 times
Ramonita
9 months ago
Yes, the output will be 4John
upvoted 0 times
...
Serina
9 months ago
I agree, the protected variable y can be accessed in B, so it should be 4John
upvoted 0 times
...
Ryan
9 months ago
I think it prints: 4John
upvoted 0 times
...
...
Princess
10 months ago
I'm pretty sure it's D. The `z` member in `B` hides the `z` member in `A`, so it will print 43.
upvoted 0 times
Yun
9 months ago
That makes sense. So the correct answer is D.
upvoted 0 times
...
Norah
9 months ago
Yes, the output will be 43 because the z member in B hides the z member in A.
upvoted 0 times
...
Thaddeus
10 months ago
I think you're right. The code will print 43.
upvoted 0 times
...
...
India
10 months ago
Hmm, I think the answer is B. The protected member `y` is accessible in the derived class `B`, so it will print 4John.
upvoted 0 times
Wei
9 months ago
Exactly, and the string `z` in the derived class `B` is set to 'John' in the set() function, which is then printed along with the modified value of `y`.
upvoted 0 times
...
Jean
9 months ago
That makes sense. The code snippet provided initializes `y` to 2 in the base class `A`, but it gets changed to 4 in the derived class `B`.
upvoted 0 times
...
Janna
9 months ago
Yes, you are correct. The code will print 4John because the protected member `y` can be accessed and modified in the derived class `B`.
upvoted 0 times
...
Lizette
10 months ago
I think the answer is B. The protected member `y` is accessible in the derived class `B`, so it will print 4John.
upvoted 0 times
...
...
Eleni
11 months ago
But doesn't the set() function in class B change the value of y to 4 before printing?
upvoted 0 times
...
Reynalda
11 months ago
I disagree, I believe the answer is B) It prints: 2John.
upvoted 0 times
...
Eleni
11 months ago
I think the answer is A) It prints: 4John.
upvoted 0 times
...

Save Cancel