Which header file is required to use vectors in C++?
A
<vector.h> B
<array> C
<vector> D
<list>
Analysis & Theory
The correct header file for vectors in C++ STL is <vector>.
What is the default initial size of a vector after declaring `vector<int> v;`?
A
0 B
1 C
Undefined D
Depends on compiler
Analysis & Theory
By default, the vector is empty, so its initial size is 0.
Which function is used to add an element to the end of a vector?
A
push() B
insert() C
append() D
push_back()
Analysis & Theory
`push_back()` is used to add an element to the end of a vector.
How do you access the first element of a vector `v`?
A
v.first() B
v.get(0) C
v[0] D
v.front()
Analysis & Theory
You can use `v[0]` or `v.at(0)` to access the first element.
What will `v.size()` return for an empty vector?
A
0 B
-1 C
undefined D
error
Analysis & Theory
For an empty vector, `size()` returns 0.
Which of the following erases all elements from the vector `v`?
A
v.erase_all() B
v.remove() C
v.clear() D
v.empty()
Analysis & Theory
`v.clear()` removes all elements from the vector.
Which function checks whether a vector is empty?
A
isEmpty() B
v.size() == 0 C
v.empty() D
v.isNull()
Analysis & Theory
`v.empty()` returns true if the vector is empty.
What does `v.capacity()` represent in a vector?
A
Number of elements in the vector B
Maximum possible elements C
Current allocated memory size D
Total size used by elements
Analysis & Theory
`capacity()` gives the size of the storage space currently allocated to the vector.
Which of the following creates a vector with 5 elements, each initialized to 10?
A
vector<int> v(5, 10); B
vector v = {5, 10}; C
vector<int> v{5}; D
vector<int> v{10};
Analysis & Theory
`vector<int> v(5, 10);` creates a vector with 5 elements, each having the value 10.
What will happen if you access an out-of-bounds index using `v.at(i)`?
A
Returns garbage value B
Compiles successfully with wrong output C
Throws out_of_range exception D
Returns NULL
Analysis & Theory
`at(i)` performs bounds checking and throws an `out_of_range` exception if `i` is invalid.