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

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

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

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

int main()

{

int i=2;

fun(&i);

cout<

return 0;

}

void fun(int *i)

{

*i = *i**i;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

0/2000 characters
Kati
3 days ago
Yeah, because *i becomes 2*2.
upvoted 0 times
...
Dylan
8 days ago
I think it prints 4.
upvoted 0 times
...
Lino
13 days ago
This seems off, are you sure about that?
upvoted 0 times
...
Arlette
18 days ago
I thought it would print 10, but I guess not!
upvoted 0 times
...
Dan
23 days ago
Wait, how does it even get to 4?
upvoted 0 times
...
Virgie
28 days ago
Totally agree, it's definitely 4!
upvoted 0 times
...
Shaniqua
1 month ago
It prints: 4
upvoted 0 times
...
Eric
1 month ago
I'm confused about the order of operations here. Could it be that it prints 10? I need to double-check how the multiplication works with pointers.
upvoted 0 times
...
Rory
1 month ago
Wait, does the `*i = *i * *i;` mean it squares the value? If so, then it should print 4, right?
upvoted 0 times
...
Brock
2 months ago
I remember something similar where a pointer was used to change a variable's value. I think it might print 4 because of the multiplication.
upvoted 0 times
...
Latricia
2 months ago
I think the function modifies the value of `i`, but I'm not sure what the final output will be.
upvoted 0 times
...

Save Cancel