C++ Programming Multiple Choice Questions & Answers (MCQs) on “seq_con Vector Class – 1”.
1. What are the vectors?
a) Arrays with dynamic size
b) Arrays with different types of elements
c) Same as array classes
d) Arrays with static size but use template classes
Answer: a
Clarification: Vectors are just like arrays with the ability to resize itself whenever an element is added to or deleted from it.
2. Pick the correct statement.
a) Vectors have dynamic size whereas Array classes have a static size
b) Both vectors and Array classes have a dynamic size
c) Both vectors and Array classes have a static size
d) Vectors have static size whereas Array classes have a dynamic size
Answer: a
Clarification: Vectors are implemented in a way so that it can handle any number of elements at a time means the size of a vector can vary, whereas Array classes have fixed size.
3. Pick the incorrect statement.
a) Vectors have a dynamic size
b) Vectors are placed in contiguous storage
c) Insertion in vectors always takes constant time
d) Vectors insert the element at the end
Answer: c
Clarification: Insertion in vectors are not always constant. When we are inserting an element at the end of the vector then if a vector is full then it needs to size itself which takes time to resize and time to insert element else just time for inserting that element at the end. Hence the insertion time is not constant always. Vectors have a dynamic size. They are placed in contiguous memory for easy access.
4. Which of the following header file is needed to use vectors in your program?
a)
b)
c)
d)
Answer: b
Clarification: Header file
5. Which of the following(s) can be used to access the first element of a vector v?
a) v.begin()
b) v.cbegin()
c) v[0]
d) all of the mentioned
Answer: d
Clarification: To access the first element of a vector we can use the following things:
i) v.begin()
ii) v.cbegin()
iii) v[0]
iv) v.at(0)
6. Which of the following(s) can be used to access the last element of a vector v?
a) v.end()
b) v.cend()
c) both v.end() and v.cend()
d) vectors do not have a function to access the last element
Answer: d
Clarification: There are no function to access the last element of the vector. The end() and cend() returns the iterator to an element which is kept at the last of the vector to keep the knowledge about the end of a vector. In order to access the last element, you can first find the size and then can use v[size-1] or v.at(size – 1) to access the last element.
7. What is the difference between begin() and cbegin() in vectors?
a) both are same
b) begin() returns iterator to first element and cbegin() returns iterator to last element
c) begin() returns an iterator to first element whereas cbegin() returns constant iterator to first element
d) begin() returns returns first element cbegin() returns void
Answer: c
Clarification: Both begin() and cbegin() are used to access the first element of the vector. The function begin() returns an iterator to first element whereas cbegin() returns a constant iterator to first element.
8. What is the difference between begin() and rbegin()?
a) both are the same
b) begin() returns an iterator to the first element and rbegin() returns an iterator to an element kept at the end of the vector
c) begin() returns an iterator to first element whereas rbegin() returns constant iterator to first element
d) begin() returns returns first element rbegin() returns void
Answer: b
Clarification: begin() is used to return the iterator to the first element of the vector whereas rbegin() is used to return the an element stored at in the last of a vector.
9. Which is the following is syntactically correct for vector
a) vector
b) vector
c) vector
d) vector
Answer: c
Clarification: v.rbegin() returns itertor of reverse iterator therefore cannot be stored in const_iterator(type mismatch). Similarly v.begin() returns normal iterator therefore cannot be stored in reverse_iterator and v.cbegin() returns the const_iterator therefore cannot be stored in normal iterator.
10. What will be the output of the following C++ code?
#include#include using namespace std; int main() { vector<int> v; for (int i = 1; i <= 5; i++) v.push_back(i); vector<int> :: const_iterator i; for (i = v.begin(); i != v.end(); ++i) cout << *i << " "; cout<<endl; return 0; }
a) 1 2 3 4 5
b) 1 3 5
c) 1 4 5
d) Error
Answer: a
Clarification: A normal iterator can be stored in const_iterator therefore program does not gives any error hence will be executed perfectly.
Output:
$ ./a.out 1 2 3 4 5
11. What will be the output of the following C++ code?
#include#include using namespace std; int main() { vector<int> v; for (int i = 1; i <= 5; i++) v.push_back(i); vector<int> :: iterator i; i = v.begin(); *i = 3; for (i = v.begin(); i != v.end(); ++i) cout << *i << " "; cout<<endl; return 0; }
a) 1 2 3 4 5
b) 3 2 3 4 5
c) 5 4 3 2 1
d) 3 3 3 3 3
Answer: b
Clarification: We have changed the value of 0th element of vector from 1 to 3 therefore the output is as follows.
Output:
$ ./a.out 3 2 3 4 5