This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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) <array>
b) <vector>
c) <containers>
d) <stdio>
View Answer
Explanation: Header file <vector> contains all the implementation of vector methods, hence we need to include this 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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<int> v?
a) vector <int> :: const_iterator itr = v.rbegin();
b) vector <int> :: reverse_iterator itr = v.begin();
c) vector <int> :: iterator itr = v.begin();
d) vector <int> :: iterator itr = v.cbegin();
View Answer
Explanation: 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 <iostream> #include <vector> 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
View Answer
Explanation: 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 <iostream> #include <vector> 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
View Answer
Explanation: 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
12. What will be the output of the following C++ code?
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for (int i = 1; i <= 5; i++) v.push_back(i); vector<int> :: const_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) Error
d) Segmentation fault
View Answer
Explanation: As i is a constant iterator therefore value stored in it is read-only therefore cannot be updated. Therefore the program gives an error.
13. Which of the following function is used to get the actual number of elements stored in vector?
a) v.size()
b) v.capacity()
c) v.max_size()
d) v.no_of_elements()
View Answer
Explanation: To get the number of elements stored in the vector v we use the function v.size(). It returns how many elements are currently in the vector excluding the void places.
14. Which function is used to get the total capacity of a vector?
a) v.size()
b) v.capacity()
c) v.max_size()
d) v.no_of_elements()
View Answer
Explanation: capacity() function is used to get the total number of elements that can be stored at present in the vector.
15. How the size of a vector increases once it is full?
a) Vector increases its capacity one by one
b) Vector doubles its capacity after it is full
c) Vector increases its capacity by half of its previous size
d) Vector increases its capacity by a constant factor
View Answer
Explanation: Once the vector is full i.e. number of elements in the vector becomes equal to the capacity of the vector then vector doubles its capacity i.e. if previous capacity was 2 then new capacity becomes 2 * 2 = 4 or 2 + 2 = 4.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Computer Science Books
- Apply for Computer Science Internship
- Check C++ Books
- Apply for Information Technology Internship
- Check Programming Books