Test Series - cpp

Test Number 68/102

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which is the following is syntactically correct for vector v?
A. vector :: const_iterator itr = v.rbegin();
B. vector :: reverse_iterator itr = v.begin();
C. vector :: iterator itr = v.begin();
D. vector :: iterator itr = v.cbegin();
Solution: 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.
Q: 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()
Solution: 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.
Q: 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()
Solution: capacity() function is used to get the total number of elements that can be stored at present in the vector.
Q: 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
Solution: 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.
Q: Which function is used to check whether the vector is empty or not?
A. empty()
B. isempty()
C. haveElements()
D. none()
Solution: empty() function is provided by the vector container to check whether it is empty or not.
Q: Which of the following function is used to insert an element at the end of a vector?
A. push_back()
B. pop_back()
C. front()
D. end()
Solution: Vector provides push_back() function to insert an element at its end.
Q: Which function is used to swap two vectors?
A. swap()
B. change()
C. merge()
D. exchange()
Solution: Vectors allows the use of swap function to swap to vectors with each other of same type and size.
Q: What will be the capacity of vector at the end in the following C++ code?

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector v; 
    for (int i = 1; i <= 5; i++) 
        v.push_back(i);
    v.reserve(50);
    cout<
A. 10
B. 8
C. 50
D. 60
Solution: In this program reserve(n) function is used which is used to reserve the space for n elements in vector. Hence when the reserve(50) function is called for vector v then the we are trying to reserve memory for 50 elements, hence the capacity of vector v becomes 50.

You Have Score    /10