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;
}
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
25 comments · newest first
CA
Cassi
10 months ago
Just to clarify, it dereferences the pointer, right?
JU
Jules
10 months ago
No way, it should be 2!
TA
Tamera
10 months ago
Totally agree, it's 4!
CA
Caprice
11 months ago
Wait, how does it add up to 4?
LO
Loreta
11 months ago
It prints: 4
SE
Serina
11 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.
EU
Eura
11 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.
ER
Erasmo
11 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?
DO
Dottie
12 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.
LO
Louisa
12 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.
JO
Joseph
12 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.
RA
Rana
1 year 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.
WA
Wai
1 year 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.
MA
Marva
1 year 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?
DE
Deeanna
1 year ago
I'm not sure, but I think it prints: 4 too. The addition of *y and x should result in 4.
KA
Kassandra
1 year 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.
GL
Glenna
1 year ago
I agree with D) It prints: 0. The code is adding the value pointed to by y and x, which are the same.
TA
Tawna
1 year ago
I'm going with B) It prints: 2. The code is dereferencing the pointer y and adding it to x.
MA
Marjory
1 year ago
I think it's actually C) It prints: 4. The code is adding the value pointed to by y and x.
FI
Fidelia
1 year ago
I believe it prints: 4 as well, the dereference operator * gets the value pointed to by y.
EL
Elliott
1 year 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!
MA
Mammie
1 year 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'.
AL
Alecia
1 year ago
That makes sense, the value of x is 2 and the value pointed to by y is also 2.
AL
Alecia
1 year ago
It prints: 2
PA
Paris
1 year ago
I think it prints: 4 because it adds the value pointed to by y and x.