C++ references: evaluate two claims Changing through a reference changes the referent (the bound object). We can create an array of references.

Difficulty: Easy

Correct Answer: Only 1 is correct.

Explanation:


Introduction / Context:
References in C++ provide aliasing. When you modify a reference, you are actually modifying the underlying object. However, the language imposes restrictions on forming certain compound types with references, notably arrays. This question asks you to separate these two facts.


Given Data / Assumptions:

  • A reference is permanently bound to an object at initialization.
  • Operations on the reference operate on the referent.
  • Standard C++ does not support arrays of references.


Concept / Approach:

Because a reference is an alias, any assignment or mutation using the reference applies to the same storage as the original variable. Conversely, arrays of references are not allowed because references are not objects with independent identity and cannot exist “unbound.” If you need array-like behavior, use arrays of pointers or std::reference_wrapper to emulate collections of references.


Step-by-Step Solution:

Check 1: true — writing r = 42; updates the original object. Check 2: false — declarations like int& a[5]; are ill-formed in standard C++. Therefore, only statement 1 is correct.


Verification / Alternative check:

Demonstrate: int x = 7; int& rx = x; rx++; Then x == 8. Attempt to compile int& arr[3]; and observe a compile-time error. Use std::reference_wrapper<int> if you need an array-like container of referents.


Why Other Options Are Wrong:

Only 2 — wrong because arrays of references are disallowed.

Both correct — fails due to statement 2.

Both incorrect — fails because statement 1 is true.


Common Pitfalls:

  • Confusing changing the binding with changing the value; references cannot be reseated but can mutate the referent.
  • Assuming reference collections exist natively; use pointers or reference wrappers instead.


Final Answer:

Only 1 is correct.

More Questions from References

Discussion & Comments

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