Difficulty: Easy
Correct Answer: Both 1 and 2 are correct.
Explanation:
Introduction / Context:
This item distinguishes C++ references from pointers and highlights how expression semantics differ. References behave like aliases to objects, removing the need for explicit * dereferencing that pointers require. The question asks you to validate two common statements about references.
Given Data / Assumptions:
Concept / Approach:
References are not pointer variables; they do not store an address that you can reseat. Instead, the reference name is another name for the same object. When you write operations using a reference, the language automatically accesses the underlying object; hence there is no need to write *r to get the referent. This automatic dereferencing is why code using references often looks cleaner than the equivalent pointer code. Therefore, both statements align with core C++ rules.
Step-by-Step Solution:
Verification / Alternative check:
Example: int x = 10; int& r = x; r++; After increment, x == 11. No *r was needed. Attempting to reseat r to another variable fails, confirming it is not a pointer that can be reassigned.
Why Other Options Are Wrong:
Only 1 or only 2 — each ignores the correctness of the other statement.
Both incorrect — contradicts well-defined reference semantics.
Common Pitfalls:
Final Answer:
Both 1 and 2 are correct.
Discussion & Comments