1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What does std::array provide that a plain int[4] does not?

Added 30 Nov -0001

What does std::array<int, 4> provide that a plain int[4] does not?

  1. Value semantics and a size() member, while the elements stay contiguous in memory Correct answer
  2. Heap allocation, so very large arrays do not consume space in the caller's stack frame
  3. Automatic bounds checking on every operator[] access in all build configurations
  4. Dynamic resizing, so extra elements can be appended after the object is constructed

Correct answer

A. Value semantics and a size() member, while the elements stay contiguous in memory

15 views

MCQSEXAM

Explanation

std::array is an aggregate wrapping a built-in array, so it keeps contiguous storage and adds value semantics, which lets it be copied and returned by value, plus size(), begin() and end(). It does not heap-allocate, and operator[] remains unchecked exactly as in C. Use .at() when a bounds check is wanted, at the cost of an exception path.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

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