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 2 Question 36 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 36
Topic #: 2
[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;

};

class B : public A {

string name;

public:

void set() {

y = 2;

z = 3;

}

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

};

int main () {

B b;

b.set();

b.Print();

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: D

Contribute your Thoughts:

0/2000 characters
Kayleigh
3 months ago
I’m not sure about this, seems off to me.
upvoted 0 times
...
Natalie
3 months ago
Wait, how does it even access 'y' like that?
upvoted 0 times
...
Della
3 months ago
Nah, I think it should print 12 instead.
upvoted 0 times
...
Lili
4 months ago
Totally agree, option C is correct!
upvoted 0 times
...
Stephaine
4 months ago
It prints: 23
upvoted 0 times
...
Merilyn
4 months ago
I feel like it should print 12 because `y` is protected and might not be initialized properly. But I could be mixing it up with another example.
upvoted 0 times
...
Rolande
4 months ago
I practiced a similar question where the output was based on default values. Could it be that `y` starts at 0 and `z` at 0 too?
upvoted 0 times
...
Eden
4 months ago
I'm not entirely sure, but I remember something about access specifiers. Does `y` being protected affect anything in the `Print()` method?
upvoted 0 times
...
Casandra
5 months ago
I think the code should print 23 because the `set()` function assigns values to `y` and `z`, right?
upvoted 0 times
...
Thaddeus
5 months ago
I'm feeling confident about this. The protected member `y` is accessible in the derived class `B`, and the public member `z` is also accessible, so the output should be `23`.
upvoted 0 times
...
Mindy
5 months ago
I'm not entirely sure about this one. I'll need to think through the inheritance hierarchy and access rules step-by-step to determine the correct output.
upvoted 0 times
...
Izetta
5 months ago
Okay, I've got this. The protected member `y` is accessible in the derived class `B`, so the `Print()` function can access it. And the public member `z` is also accessible, so the output should be `23`.
upvoted 0 times
...
Fabiola
5 months ago
Hmm, I'm a bit confused about the access modifiers here. I'll need to review my notes on inheritance and visibility to figure this out.
upvoted 0 times
...
Patti
5 months ago
This looks like a classic inheritance question. I'll need to carefully trace the code to understand how the protected and public members are accessed.
upvoted 0 times
...
Leslee
10 months ago
Wait, wait, wait... you guys are telling me this code actually compiles? I thought we were supposed to be looking for bugs and typos! Looks like the compiler is getting smarter than us programmers.
upvoted 0 times
Elli
8 months ago
The compiler is able to handle the code properly.
upvoted 0 times
...
Carissa
9 months ago
The code is correct and does not have any bugs or typos.
upvoted 0 times
...
Therese
9 months ago
Yes, the code compiles and runs without any errors.
upvoted 0 times
...
...
Vi
10 months ago
This code is as clear as mud! But I'm going to go with C, because it just feels right, you know? The protected and public variables are the key here.
upvoted 0 times
...
Ashley
10 months ago
Hmm, I'm not sure about this one. Let me think it through... Oh, I got it! The 'y' variable is protected, so the derived class can access it. And the 'z' variable is public, so it can be printed. C is the right answer, I think.
upvoted 0 times
Elvera
9 months ago
Exactly, C is the right answer.
upvoted 0 times
...
Cristina
9 months ago
So, the output should be 23.
upvoted 0 times
...
Haley
9 months ago
Yes, that makes sense. The output will be 23 because 'y' is set to 2 and 'z' is set to 3 in the derived class.
upvoted 0 times
...
Laura
9 months ago
I think you're right, the protected variable 'y' can be accessed by the derived class and the public variable 'z' can be printed. So, the output should be 23.
upvoted 0 times
...
Truman
9 months ago
Yes, that's correct. And the public variable 'z' can be printed as well.
upvoted 0 times
...
Tamra
10 months ago
I think the protected variable 'y' can be accessed by the derived class.
upvoted 0 times
...
...
Jerry
10 months ago
Haha, looks like someone forgot to initialize the variables. I bet the code will just print a bunch of zeros, right? Option B for the win!
upvoted 0 times
...
Stephen
11 months ago
The correct answer is C. The code will print 23 because the 'y' variable is protected and can be accessed by the derived class 'B', and the 'z' variable is public and can be accessed by any class.
upvoted 0 times
...
Bernadine
11 months ago
So the correct answer is C) It prints: 23. Got it!
upvoted 0 times
...
Adria
11 months ago
I agree with Alesia. The Print function will then output the values of y and z, which are 2 and 3 respectively.
upvoted 0 times
...
Alesia
11 months ago
I think it prints: 23 because the set function initializes y to 2 and z to 3.
upvoted 0 times
...

Save Cancel