Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPA-21-02 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:

Isaac
1 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
7 days ago
User3: Yeah, it's definitely a tricky one
upvoted 0 times
...
Martina
11 days ago
User2: I agree, the output does seem a bit unexpected
upvoted 0 times
...
Novella
12 days ago
User1: I think it prints: 23
upvoted 0 times
...
...
Elise
1 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
7 days ago
Yes, the output will be 4John
upvoted 0 times
...
Serina
13 days ago
I agree, the protected variable y can be accessed in B, so it should be 4John
upvoted 0 times
...
Ryan
16 days ago
I think it prints: 4John
upvoted 0 times
...
...
Princess
2 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
17 days ago
That makes sense. So the correct answer is D.
upvoted 0 times
...
Norah
1 months ago
Yes, the output will be 43 because the z member in B hides the z member in A.
upvoted 0 times
...
Thaddeus
1 months ago
I think you're right. The code will print 43.
upvoted 0 times
...
...
India
2 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
7 days 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
12 days 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
24 days 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
2 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
2 months ago
But doesn't the set() function in class B change the value of y to 4 before printing?
upvoted 0 times
...
Reynalda
2 months ago
I disagree, I believe the answer is B) It prints: 2John.
upvoted 0 times
...
Eleni
2 months ago
I think the answer is A) It prints: 4John.
upvoted 0 times
...

Save Cancel
az-700  pass4success  az-104  200-301  200-201  cissp  350-401  350-201  350-501  350-601  350-801  350-901  az-720  az-305  pl-300  

Warning: Cannot modify header information - headers already sent by (output started at /pass.php:70) in /pass.php on line 77