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?
- A.
Both a and b will contain 10
- B.
Both a and b will contain 20
- C.
Both p and q will point to a
- 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.