Deal of The Day! 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 6 Question 53 Discussion

What happens when you attempt to compile and run the following code?#include using namespace std;void fun(int &i);int main(){int i=2;fun(i);cout
C) It prints: 4
A) It prints: 2
B) It prints: 0
D) It prints: 16

C++ Institute CPA-21-02 Exam - Topic 6 Question 53 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 53
Topic #: 6
[All CPA-21-02 Questions]

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

#include

using namespace std;

void fun(int &i);

int main()

{

int i=2;

fun(i);

cout<

return 0;

}

void fun(int &i)

{

i+=2;

}

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

0/2000 characters
Gilma
5 days ago
I agree, the reference changes the value.
upvoted 0 times
...
Kenny
10 days ago
Yeah, because fun modifies i directly.
upvoted 0 times
...
Caitlin
15 days ago
I think it prints 4.
upvoted 0 times
...
Dierdre
20 days ago
I thought it would print 0 for some reason!
upvoted 0 times
...
Rory
25 days ago
Definitely C, the reference changes the original.
upvoted 0 times
...
Pamella
1 month ago
Wait, are you sure it doesn't print 2?
upvoted 0 times
...
Yuki
1 month ago
Totally agree, it modifies the value!
upvoted 0 times
...
Mitzie
1 month ago
It prints: 4
upvoted 0 times
...
Alverta
2 months ago
I’m confused about the output. I thought it might print 0, but now I’m leaning towards 4 since the function adds 2 to `i`.
upvoted 0 times
...
Elroy
3 months ago
I practiced a similar question where passing by reference changed the variable's value. I believe it should print 4.
upvoted 0 times
...
Quentin
3 months ago
I'm not entirely sure, but I remember something about references in C++. Could it be that it prints 2 instead?
upvoted 0 times
...
Mariann
3 months ago
I think the function modifies the value of `i` since it's passed by reference, so it should print 4.
upvoted 0 times
...

Save Cancel