1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A reference int& r = v[0]; is taken, then v.push_back(99)…

Added 30 Nov -0001

A reference int& r = v[0]; is taken, then v.push_back(99) triggers a reallocation. What is the state of r?

  1. It becomes a reference to a default-constructed int placed in the new buffer
  2. It dangles, because growth allocates a new buffer and destroys the old elements Correct answer
  3. It remains valid, since vector guarantees reference stability across insertions
  4. It is rebound automatically to the new buffer at the same logical index position

Correct answer

B. It dangles, because growth allocates a new buffer and destroys the old elements

16 views

MCQSEXAM

Explanation

Vector reallocation allocates a larger block, moves or copies the elements and destroys the originals, invalidating every pointer, reference and iterator into the old storage. Calling reserve with an expected size prevents that churn. std::deque gives different guarantees: references survive push_back but not push_front. std::list keeps all references valid except those to erased elements.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs