Consider the following C++ code segment: int a = 10, b = 20; int *p = &a, *q =…

2018

Consider the following C++ code segment:
int a = 10, b = 20;
int *p = &a, *q = &b;
p = q;
With respect to the code segment mentioned above, which of the following statement is correct?

  1. A.

    Both a and b will contain 10

  2. B.

    Both a and b will contain 20

  3. C.

    Both p and q will point to a

  4. D.

    Both p and q will point to b

Attempted by 147 students.

Show answer & explanation

Correct answer: D

Initially, pointer p points to variable a and q points to b. The statement p = q copies the address of b into p, so both pointers now reference variable b. The values of a and b remain unchanged.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs