What will be the output of the following C++ code?

int x = 10;
int *p = &x;
*p = 20;
cout << x;
                        

Options

  • A. 10
  • B. 20
  • C. Address of x
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

B. 20

Detailed Explanation

Answer: (B) 20

The pointer p stores the address of x.

When we write *p = 20, we modify the value stored at that address.

Because the address belongs to x, the value of x becomes 20.

Therefore, cout << x prints 20.