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;
}
Suggested Answer: C
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
24 comments · newest first
GE
Gerald
5 days ago
D is way off too, just a distraction.
RI
Rima
10 days ago
Definitely not A or B, those are wrong.
MA
Maira
15 days ago
I was confused at first, but it makes sense now.
CA
Carmen
20 days ago
So, option C is correct!
FE
Felicitas
25 days ago
I agree, the reference changes the value.
JA
Janna
1 month ago
Yeah, because fun modifies i directly.
LE
Lenna
1 month ago
I think it prints 4.
TI
Timmy
1 month ago
Same here, references are powerful in C++.
DE
Desmond
2 months ago
I feel confident about this one, it's clear.
YO
Yoko
2 months ago
I thought it would print 2, but I see the reference works.
DO
Dorathy
2 months ago
I was confused at first, but it makes sense now.
SO
Sophia
2 months ago
Definitely 4, i starts at 2, then adds 2.
GI
Gilma
2 months ago
I agree, the reference changes the value.
KE
Kenny
2 months ago
Yeah, because fun modifies i directly.
CA
Caitlin
3 months ago
I think it prints 4.
DI
Dierdre
3 months ago
I thought it would print 0 for some reason!
RO
Rory
3 months ago
Definitely C, the reference changes the original.
PA
Pamella
3 months ago
Wait, are you sure it doesn't print 2?
YU
Yuki
3 months ago
Totally agree, it modifies the value!
MI
Mitzie
3 months ago
It prints: 4
AL
Alverta
4 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`.
EL
Elroy
5 months ago
I practiced a similar question where passing by reference changed the variable's value. I believe it should print 4.
QU
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?
MA
Mariann
6 months ago
I think the function modifies the value of `i` since it's passed by reference, so it should print 4.