Skip to main content

Vectors and Dynamic Arrays

1).What is the main advantage of using a vector in C++ over an array?

1) Dynamic resizing
2) Fixed size
3) Faster access time
4) None of the above

2).Which of the following methods adds an element to the end of a vector?

1) push_back()
2) append()
3) insert()
4) add()

3).How can you initialize a vector with predefined values?

1) vector<int> v(5, 10);
2) vector<int> v = {10, 20, 30};
3) vector<int> v(5);
4) All of the above

4).What does the 'resize' method of a vector do?

1) Changes the size of the vector
2) Sorts the vector
3) Removes all elements
4) None of the above

5).Which of the following is a drawback of using vectors?

1) Increased memory consumption
2) Slow resizing performance
3) No random access
4) None of the above

6).Can a vector hold elements of different data types?

1) Yes, by using void pointers
2) No
3) Yes, by using a template
4) None of the above

7).What is the time complexity of accessing an element in a vector?

1) O(1)
2) O(n)
3) O(log n)
4) O(n^2)

8).How do you remove the last element from a vector?

1) pop_back()
2) erase()
3) remove()
4) delete()

9).Which operation is NOT supported by a vector?

1) Accessing elements by index
2) Inserting elements in the middle
3) Random access to elements
4) None of the above

10).What is the capacity of a vector?

1) The number of elements it can currently hold
2) The maximum number of elements it can ever hold
3) The amount of memory allocated for the vector
4) None of the above