What does std::array<int, 4> provide that a plain int[4] does not?
A
Value semantics and a size() member, while the elements stay contiguous in memory
Correct answer
B
Heap allocation, so very large arrays do not consume space in the caller's stack frame
C
Automatic bounds checking on every operator[] access in all build configurations
D
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
⚔️
Challenge a friend on C++ and Object-Oriented Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
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
Doubts & discussionNot sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ std::sort(v.begin(), v.end(), [](int a, int b){ return a >= b; }); is called. What is the defect? Next MCQ › Two objects hold std::shared_ptr to each other. Why is neither destructor called when the last external owner releases them?