New Year Sale 2026! 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 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:

0/2000 characters
Cassi
2 months ago
Just to clarify, it dereferences the pointer, right?
upvoted 0 times
...
Jules
2 months ago
No way, it should be 2!
upvoted 0 times
...
Tamera
3 months ago
Totally agree, it's 4!
upvoted 0 times
...
Caprice
3 months ago
Wait, how does it add up to 4?
upvoted 0 times
...
Loreta
3 months ago
It prints: 4
upvoted 0 times
...
Serina
3 months ago
I’m confused about the output. I thought it might print 2 since it’s just printing x, but now I’m not so sure.
upvoted 0 times
...
Eura
4 months ago
This seems similar to a practice question we did in class. I feel like the answer is 4, but I could be mixing it up with another example.
upvoted 0 times
...
Erasmo
4 months ago
I'm not entirely sure, but I remember something about pointers. If y points to x, then *y should give me the value of x, right?
upvoted 0 times
...
Dottie
4 months ago
I think the code should print 4 because it adds the value of x and the value pointed to by y, which both are 2.
upvoted 0 times
...
Louisa
4 months ago
Ah, I see what's happening now. The pointer y is pointing to the same memory location as x, so when we dereference y and add it to x, we're effectively doubling the value. The answer must be C, it prints 4.
upvoted 0 times
...
Joseph
4 months ago
I'm a bit confused here. Pointers can be tricky, but I'll give it my best shot. Let me work through this line by line and see if I can arrive at the right answer.
upvoted 0 times
...
Rana
5 months ago
Okay, let me think this through step-by-step. We have an integer x initialized to 2, and a pointer y that points to x. Then we print the sum of the value pointed to by y and the value of x. I think the answer is B, it prints 4.
upvoted 0 times
...
Wai
5 months ago
Hmm, this looks like a tricky one. I'll need to carefully trace the pointer operations and addition to figure out the correct output.
upvoted 0 times
...
Marva
8 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
8 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
8 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
7 months 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
7 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
7 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
8 months ago
I believe it prints: 4 as well, the dereference operator * gets the value pointed to by y.
upvoted 0 times
...
Elliott
8 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
8 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
7 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
8 months ago
It prints: 2
upvoted 0 times
...
...
Paris
8 months ago
I think it prints: 4 because it adds the value pointed to by y and x.
upvoted 0 times
...

Save Cancel