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 Exam CPA-21-02 Topic 8 Question 40 Discussion

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

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

#include

using namespace std;

int main()

{

int x=2, *y;

y = &x;

cout << *y + x;

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

Marva
1 months ago
B) It prints: 2. Duh, anyone who's been paying attention in their C++ class should know this. It's like pointer 101, really. Although, I have to say, the person who wrote this question must have a twisted sense of humor. 'Attempt to compile and run' - what is this, a trick question?
upvoted 0 times
...
Deeanna
1 months ago
I'm not sure, but I think it prints: 4 too. The addition of *y and x should result in 4.
upvoted 0 times
...
Kassandra
1 months ago
Hmm, I'm going with D) It prints: 0. The code is clearly trying to confuse us with pointers and stuff, but I bet it's just a trick question and the answer is 0. You know, the classic 'return 0' at the end.
upvoted 0 times
Glenna
20 days ago
I agree with D) It prints: 0. The code is adding the value pointed to by y and x, which are the same.
upvoted 0 times
...
Tawna
1 months ago
I'm going with B) It prints: 2. The code is dereferencing the pointer y and adding it to x.
upvoted 0 times
...
Marjory
1 months ago
I think it's actually C) It prints: 4. The code is adding the value pointed to by y and x.
upvoted 0 times
...
...
Fidelia
2 months ago
I believe it prints: 4 as well, the dereference operator * gets the value pointed to by y.
upvoted 0 times
...
Elliott
2 months ago
I think the answer is C) It prints: 4. When you dereference the pointer 'y', it gives you the value of 'x', which is 2. Then, you add 'x' to that, which gives you 4. Simple math, folks!
upvoted 0 times
...
Mammie
2 months ago
The correct answer is B) It prints: 2. The code creates a pointer variable 'y' and assigns the address of 'x' to it. Then, it prints the sum of the value of 'x' and the value pointed to by 'y', which is the value of 'x'.
upvoted 0 times
Alecia
1 months ago
That makes sense, the value of x is 2 and the value pointed to by y is also 2.
upvoted 0 times
...
Alecia
1 months ago
It prints: 2
upvoted 0 times
...
...
Paris
2 months ago
I think it prints: 4 because it adds the value pointed to by y and x.
upvoted 0 times
...

Save Cancel