U.S. Independence Day Deal! Unlock 25% OFF Today – Limited-Time Offer - 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
Elroy
2 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
2 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
2 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