C++ references: evaluate the two claims Once a variable and a reference are linked, they remain tied. 2) After declaring one reference to a variable, another reference to the same variable is not allowed.

Difficulty: Easy

Correct Answer: Only 1 is correct.

Explanation:


Introduction / Context:
References in C++ act as aliases for existing objects. They are widely used in function parameters, operator overloads, and APIs to avoid copying. This question asks you to judge two statements about reference binding and whether multiple references can refer to the same object.


Given Data / Assumptions:

  • A reference must be initialized to bind to an object.
  • Once initialized, a reference cannot be reseated to a different object.
  • There is no language limit that allows only one reference per object.


Concept / Approach:

Statement 1 is true: binding is permanent for a reference. Statement 2 is false: any number of references may be created to the same underlying object. The language does not restrict multiple references; in fact, functions often return or accept references simultaneously, all aliasing the same variable. Therefore, the correct choice is “Only 1 is correct.”


Step-by-Step Solution:

Check 1: int x; int& r = x; — r cannot later be bound to y ⇒ true. Check 2: int& r2 = x; — compiles and is valid; multiple references may alias x ⇒ false claim. Thus, exactly statement 1 is correct. Choose “Only 1 is correct.”


Verification / Alternative check:

Demonstrate with code: create r1 and r2 both referring to x; modifications via either alias are visible via the other, confirming permissibility and aliasing behavior. No rule forbids additional references.


Why Other Options Are Wrong:

Only 2: incorrect because you can create multiple references to one variable.

Both correct: wrong due to statement 2.

Both incorrect: wrong because statement 1 is right.


Common Pitfalls:

  • Confusing references with unique ownership concepts (which are modeled by smart pointers, not references).
  • Assuming references imply thread safety or uniqueness; they do not.


Final Answer:

Only 1 is correct.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion