1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What does std::vector::push_back do in C++?

Added 30 Nov -0001

What does std::vector<int>::push_back do in C++?

  1. Inserts an element at the front of the vector
  2. Replaces the element at the current index position
  3. Reserves capacity without adding an element
  4. Appends an element at the end, growing if needed Correct answer

Correct answer

D. Appends an element at the end, growing if needed

14 views

MCQSEXAM

Explanation

push_back copies or moves a value into a new slot at the end and increases the size by one, reallocating when size reaches capacity. Because reallocation can move the buffer, iterators and references obtained earlier may be invalidated. reserve only changes capacity without changing size, and insert(begin(), x) is the operation that shifts elements to make room at the front.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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