In C++, which statement about references is correct? Select the rule that always applies to reference initialization and binding.

Difficulty: Easy

Correct Answer: A reference must always be initialized.

Explanation:

Introduction / Context:References in C++ are aliases to existing objects. Unlike pointers, a reference must be bound to an object at the point of its declaration. This question focuses on the unconditional requirement for reference initialization.

Given Data / Assumptions:

  • A reference binds to an existing, valid object.
  • Environment: standard C++ (no compiler extensions).
  • Local scope or global scope does not change the fundamental rule.

Concept / Approach:The language requires that a reference be initialized as part of its declaration. This is independent of whether the reference appears in a function body, as a global/static, or as a class data member (members can be initialized in constructors’ initializer lists). There is no 'null reference' by default and no way to leave a reference unbound.

Step-by-Step Solution:1) Evaluate scope-based claims (inside vs outside functions): these do not change the rule.2) Recognize the universal constraint: binding must occur at declaration.3) The only answer that reflects this universal constraint is 'A reference must always be initialized.'4) Therefore select option C.

Verification / Alternative check:Attempt to compile 'int &r;' without an initializer. The compiler will emit an error requiring initialization. For class members, use constructor initializer lists: 'MyType(int &x) : ref(x) {}'.

Why Other Options Are Wrong:'Inside functions' or 'outside all functions': scope is irrelevant to the rule.'Both A and C': A is not universally true; the correct universal rule is C alone.

Common Pitfalls:Confusing references with pointers and expecting to assign a target later. Rebinding is not allowed; if reseating is needed, use pointers or std::reference_wrapper for certain patterns.

Final Answer:A reference must always be initialized.

Discussion & Comments

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