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 47 Discussion

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

}

Show Suggested Answer Hide Answer
Suggested Answer: A

Contribute your Thoughts:

0/2000 characters
Ozell
1 day ago
Wait, how does it print in that order?
upvoted 0 times
...
Hollis
7 days ago
Totally agree, that's the output!
upvoted 0 times
...
Cecily
12 days 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.
upvoted 0 times
...
Walton
17 days 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.
upvoted 0 times
...
Gwen
22 days 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.
upvoted 0 times
...
Carmelina
27 days 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.
upvoted 0 times
...
Queen
1 month 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.
upvoted 0 times
...
Carisa
1 month ago
I practiced a similar question, and I feel like the output should be "Bill0" instead.
upvoted 0 times
...
Leatha
1 month ago
I think it should compile without errors since the friend function is declared in both classes.
upvoted 0 times
...
Marti
2 months ago
This seems straightforward enough. I'll work through it step-by-step and see if I can arrive at the correct answer.
upvoted 0 times
...
Donette
2 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.
upvoted 0 times
...
Leonor
2 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`.
upvoted 0 times
...
Ernie
2 months ago
It prints: 0Bill
upvoted 0 times
...
Hubert
2 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.
upvoted 0 times
...
Gregg
3 months ago
I agree, but what if there's a typo?
upvoted 0 times
...
Bulah
3 months ago
I'm not entirely sure, but I remember something about the order of printing. Could it be that it prints "0Bill"?
upvoted 0 times
...
Yolande
3 months 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.
upvoted 0 times
...
Ceola
3 months ago
This looks like a tricky one. I'll need to carefully trace the code execution to figure out what's happening.
upvoted 0 times
Jame
2 months ago
I think it prints 0Bill.
upvoted 0 times
...
...

Save Cancel