BLACK FRIDAY SALE – Unlock Up to 25% OFF on All Exam Prep! - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - Topic 7 Question 47 Discussion

Posted by Sabra on Nov 22, 2025 at 08:39 PM

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

#include

#include

using namespace std;

class Second;

class Base {

int age;

public:

Base () { age=5; };

friend void set(Base &ob, Second &so);

void Print() { cout << age;}

};

class Second {

string name;

public:

friend void set(Base &ob, Second &so);

void Print() { cout << name;}

};

void set(Base &ob, Second &so) {

ob.age = 0; so.name = "Bill";

}

int main () {

Base a;

Second b;

set(a,b);

a.Print();

b.Print();

return 0;

}

Suggested Answer: A

Get Premium CPA-21-02 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
30 comments · newest first
MA
Malissa 7 months ago
Definitely 0Bill. The order of Print matters.
PE
Pearlie 8 months ago
No, it should compile fine. The friend function works.
ER
Ernest 8 months ago
I feel like there might be a compilation error.
AD
Adolph 8 months ago
Yeah, the set function modifies both objects.
ER
Erick 8 months ago
I think it prints 0Bill.
NO
Nobuko 8 months ago
No way, it works perfectly fine!
KR
Krissy 9 months ago
I think it should give a compilation error.
OZ
Ozell 9 months ago
Wait, how does it print in that order?
HO
Hollis 9 months ago
Totally agree, that's the output!
CE
Cecily 9 months ago
A) It prints: 0Bill - Yep, that's the one! The friend function gives the set() function access to the private members of both classes, so it can modify them directly.
WA
Walton 10 months ago
Hmm, I'm not sure about this one. The friend function is a bit confusing, but I think A is the right answer. Let me double-check the code just to be sure.
GW
Gwen 10 months ago
Haha, this is a classic trick question! I bet a lot of people will get tripped up by the friend function and try to say it's a compilation error.
CA
Carmelina 10 months ago
I think the correct answer is A as well. The set() function directly modifies the private members of both classes, so the output should be 0Bill.
QU
Queen 10 months ago
I’m confused about the friend function. Does it really allow access to private members? I hope it doesn’t lead to a compilation error.
CA
Carisa 10 months ago
I practiced a similar question, and I feel like the output should be "Bill0" instead.
LE
Leatha 11 months ago
I think it should compile without errors since the friend function is declared in both classes.
MA
Marti 11 months ago
This seems straightforward enough. I'll work through it step-by-step and see if I can arrive at the correct answer.
DO
Donette 11 months ago
I'm a bit confused by the order of the `Print()` calls in `main()`. I'll need to double-check the expected output.
LE
Leonor 11 months ago
Okay, I think I've got it. The friend function `set()` is modifying the private member `age` of the `Base` class, so the output should be `0Bill`.
ER
Ernie 11 months ago
It prints: 0Bill
HU
Hubert 12 months ago
The correct answer is A) It prints: 0Bill. The set() function modifies the age member of the Base object and the name member of the Second object, so the output will be 0Bill.
GR
Gregg 12 months ago
I agree, but what if there's a typo?
BU
Bulah 1 year ago
I'm not entirely sure, but I remember something about the order of printing. Could it be that it prints "0Bill"?
YO
Yolande 1 year ago
Hmm, the use of friend functions is throwing me off a bit. I'll need to review how those work with class member access.
CE
Ceola 1 year ago
This looks like a tricky one. I'll need to carefully trace the code execution to figure out what's happening.
LA
Lashon 7 months ago
↳ replying to Ceola
So, it's definitely A) It prints: 0Bill.
MA
Marion 7 months ago
↳ replying to Ceola
Right! First a.Print(), then b.Print().
LA
Lavina 7 months ago
↳ replying to Ceola
But the order of Print matters too!
AS
Ashlee 7 months ago
↳ replying to Ceola
Yeah, the set function modifies both objects.
JA
Jame 11 months ago
↳ replying to Ceola
I think it prints 0Bill.
0/2000 characters

Save Cancel