Multiple choice

What does the following code do?

void afunction(int *x) { x=new int; *x=12; } int main() { int v=10; afunction(&v); printf(“%d”,v); }

  1. Outputs 12

  2. Outputs 10

  3. Outputs the address of v

  4. No output

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The function afunction receives a pointer to v but immediately reassigns x to point to newly allocated memory (x=new int). This does NOT change the original pointer in main - it only changes the local copy x. The *x=12 assigns to the new memory, not to v. When printf prints v in main, v still has its original value 10. The new int is leaked (memory not freed).