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 1 Question 37 Discussion

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

public:

void set() {

y = 4; z = 2;

}

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
Yvonne
2 months ago
So, y gets updated but x is private? Interesting!
upvoted 0 times
...
Gary
2 months ago
Totally agree, that's the output!
upvoted 0 times
...
Colette
2 months ago
It prints: 42
upvoted 0 times
...
Cassi
3 months ago
I thought it would print 44.
upvoted 0 times
...
Yvonne
3 months ago
Wait, are you sure about that?
upvoted 0 times
...
Rory
3 months ago
I feel like it should print 44 because z is public and gets set to 2, but I’m not entirely confident about the initial values of y and z.
upvoted 0 times
...
Eric
3 months ago
I practiced a similar question, and I think the output depends on the order of initialization. I’m a bit confused about what gets printed first.
upvoted 0 times
...
Tyisha
4 months ago
I remember something about protected members being accessible in derived classes, so y should be set to 4 in set(). That might mean it prints 42?
upvoted 0 times
...
Frank
4 months ago
I think the constructor of class A initializes x, y, and z, but I'm not sure how that affects the output when we call Print in class B.
upvoted 0 times
...
Bonita
4 months ago
Hmm, I'm not entirely sure about this one. I think the protected member y in A is accessible to B, but I'm not 100% confident. I'll need to carefully walk through the code step-by-step to make sure I understand how the inheritance and access modifiers are working here.
upvoted 0 times
...
Margurite
4 months ago
I've got this! The key is understanding how the protected member y in the base class A is accessible to the derived class B. When B calls the set() method, it sets y to 4 and z to 2. Then the Print() method outputs those values, so the final output is 42.
upvoted 0 times
...
Huey
4 months ago
I'm a bit confused by the access modifiers here. I know protected members can be accessed by derived classes, but I'm not sure how that applies to the y and z members. I'll need to review my notes on inheritance and access control to figure this one out.
upvoted 0 times
...
William
5 months ago
Okay, let me think this through step-by-step. The base class A has a private member x, a protected member y, and a public member z. The derived class B inherits publicly from A and has access to y and z. The set() method in B sets y to 4 and z to 2. The Print() method then prints y and z, so I think the output should be 42.
upvoted 0 times
...
Sylvia
5 months ago
Hmm, this looks like a tricky one. I'll need to carefully trace the code to understand how the inheritance and access modifiers are working here.
upvoted 0 times
...
Alease
10 months ago
Ha! This is a classic trick question. The answer is obviously C) It prints: 22. Why, you ask? Because the protected member y is set to 4, and the public member z is set to 2. So when we print them, we get 42. Easy as pie!
upvoted 0 times
Yoko
10 months ago
No way, it prints: 22. Trust me on this one.
upvoted 0 times
...
Tula
10 months ago
I think you're mistaken. It actually prints: 42
upvoted 0 times
...
...
Vincenza
11 months ago
I believe it prints: 44 too, because the Print function in class B outputs the values of y and z
upvoted 0 times
...
Claudia
11 months ago
Hmm, this is tricky. I'm going to have to think about it carefully. Wait, I've got it! The protected member y is accessible in the derived class, so we can modify it. And since z is public, we can also change its value. The output should be 44. I'll go with B) It prints: 44.
upvoted 0 times
Yvonne
9 months ago
So, the correct answer is B) It prints: 44.
upvoted 0 times
...
Kina
9 months ago
Yes, and since z is public, its value can also be changed.
upvoted 0 times
...
Bethanie
9 months ago
I agree, the protected member y can be modified in the derived class.
upvoted 0 times
...
Celestine
9 months ago
I think the output will be 44.
upvoted 0 times
...
...
Avery
11 months ago
I'm not sure, but I think it prints: 44 as well
upvoted 0 times
...
Pearly
11 months ago
I agree with Cammy, because the set function in class B changes the value of y to 4 and z to 2
upvoted 0 times
...
Cammy
11 months ago
I think it prints: 44
upvoted 0 times
...
Shayne
11 months ago
Okay, let's think this through. The default constructor of class A initializes x to 1, y to 2, and z to 3. In the set() function, we change y to 4 and z to 2. So the output should be 42. I'll go with B) It prints: 44.
upvoted 0 times
...
Daryl
11 months ago
Ah, a classic inheritance question. The protected member y is accessible in the derived class B, so we can modify it in the set() function. And since z is public, we can also change its value. The output should be 44. This is an easy one!
upvoted 0 times
...
Melina
11 months ago
Hmm, let's see. The code creates an object of class B, which inherits from class A. The set() function sets the value of the protected member y to 4 and the public member z to 2. The Print() function then prints the values of y and z. I think the answer is B) It prints: 44.
upvoted 0 times
Yvonne
10 months ago
Actually, it prints: 42.
upvoted 0 times
...
Laticia
10 months ago
Actually, it prints: 44.
upvoted 0 times
...
Ricarda
10 months ago
No, I believe it prints: 42.
upvoted 0 times
...
Amber
10 months ago
No, I believe it prints: 42.
upvoted 0 times
...
Willodean
10 months ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
Brynn
11 months ago
I think the answer is B) It prints: 44.
upvoted 0 times
...
...

Save Cancel