LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 4 of 27

Arrays and Vectors

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

Arrays and Vectors · Study material

C++ Arrays and Vectors – Interview Questions

1. What is the difference between an array and a vector?

A C array has a compile-time fixed size, lives on the stack (as a local), decays to a pointer and cannot be copied or returned. A vector has a run-time size, stores its elements in a heap buffer, manages its memory and can be copied, moved and returned.

2. What does "array decay" mean?

The automatic conversion of an array to a pointer to its first element in most expressions, including when passed to a function. The size information is lost.

3. How are 2D arrays stored in memory?

In row-major order: all elements of row 0, then row 1, and so on. Element [r][c] of an R × C array is at offset r × C + c.

4. What is the difference between size() and capacity()?

size is the number of elements; capacity is how many elements the allocated buffer can hold before the vector must reallocate.

5. How does a vector grow?

When full, it allocates a larger buffer (typically 1.5× or 2× the old capacity), moves or copies the elements and frees the old buffer. This gives amortized O(1) push_back.

6. What is the difference between reserve and resize?

reserve(n) only guarantees capacity ≥ n; the size is unchanged. resize(n) changes the number of elements, value-initializing new ones or destroying extras.

7. push_back vs emplace_back?

push_back takes an existing object and copies or moves it in; emplace_back takes constructor arguments and constructs the element directly in the vector.

8. What is the difference between v[i] and v.at(i)?

at checks the index and throws std::out_of_range if invalid; operator[] does not check, and an invalid index is undefined behaviour.

9. What is iterator invalidation?

After operations that reallocate (push_back beyond capacity, reserve, insert) all iterators, pointers and references to elements become invalid; erase invalidates those at and after the erased position.

10. Explain the erase-remove idiom.

std::remove moves the elements to keep to the front and returns the new logical end; vector::erase then deletes the leftover tail. C++20 replaces it with std::erase and std::erase_if.

11. Does clear() free a vector's memory?

No – it destroys the elements and sets size to 0 but usually keeps the capacity. shrink_to_fit requests a release, or swap with an empty vector.

12. Why is std::vector<bool> special?

It is specialised to store bits, so its elements are not real bool objects: operator[] returns a proxy and you cannot take a bool* to an element.

13. What advantages does std::array have over a C array?

It knows its size, does not decay, can be copied, compared and returned, works with algorithms and offers at() – with no runtime overhead.

14. How do you pass a 2D array to a function?

The column count must be part of the type: void f(int m[][4], int rows). Easier alternatives are std::array of std::array, std::vector of vectors or a flat vector with dimensions.

15. Are vector elements stored contiguously?

Yes. data() returns a pointer to the first element, so a vector can be passed to C APIs expecting an array.

16. What is std::span?

A C++20 non-owning view of a contiguous sequence (pointer + length). It lets one function accept arrays, std::array and vectors without copying.

17. What is the time complexity of inserting in the middle of a vector?

O(n) – all following elements must be shifted. Inserting or removing at the end is amortized O(1).

18. Can a C array's size be a variable?

Not in standard C++. Variable length arrays are a C feature some compilers accept as an extension; use std::vector instead.

19. What does vector<int> v(5); contain?

Five value-initialized ints, i.e. five zeros. vector<int> v{5}; contains a single element, 5.

20. How do you find the maximum element and its index?

auto it = std::max_element(v.begin(), v.end()); the value is *it and the index is it − v.begin().

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links