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
Rima
2 days ago
Definitely not A or B, those are wrong.
upvoted 0 times
...
Maira
7 days ago
I was confused at first, but it makes sense now.
upvoted 0 times
...
Carmen
12 days ago
So, option C is correct!
upvoted 0 times
...
Felicitas
17 days ago
I agree, the reference changes the value.
upvoted 0 times
...
Janna
22 days ago
Yeah, because fun modifies i directly.
upvoted 0 times
...
Lenna
28 days ago
I think it prints 4.
upvoted 0 times
...
Timmy
1 month ago
Same here, references are powerful in C++.
upvoted 0 times
...
Desmond
1 month ago
I feel confident about this one, it's clear.
upvoted 0 times
...
Yoko
1 month ago
I thought it would print 2, but I see the reference works.
upvoted 0 times
...
Dorathy
2 months ago
I was confused at first, but it makes sense now.
upvoted 0 times
...
Sophia
2 months ago
Definitely 4, i starts at 2, then adds 2.
upvoted 0 times
...
Gilma
2 months ago
I agree, the reference changes the value.
upvoted 0 times
...
Kenny
2 months ago
Yeah, because fun modifies i directly.
upvoted 0 times
...
Caitlin
2 months ago
I think it prints 4.
upvoted 0 times
...
Dierdre
2 months ago
I thought it would print 0 for some reason!
upvoted 0 times
...
Rory
3 months ago
Definitely C, the reference changes the original.
upvoted 0 times
...
Pamella
3 months ago
Wait, are you sure it doesn't print 2?
upvoted 0 times
...
Yuki
3 months ago
Totally agree, it modifies the value!
upvoted 0 times
...
Mitzie
3 months ago
It prints: 4
upvoted 0 times
...
Alverta
3 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
5 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
5 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
5 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