250+ TOP MCQs on seq_con Vector Class – 2 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “seq_con Vector Class – 2”.

1. 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); 
 
    cout<<v.size();
    cout<<endl<<v.capacity();  
    return 0; 
}

a)

5
5

b)

5
8

c)

5
10

d)

8
8

View Answer

Answer: b
Clarification: The size() returns the number of elements in the vector and capacity() returns the total number of elements that this vector can hold. Hence as the number of elements in vector is 5 and size is increased by 2 times. Therefore output is 5 and 8
Output:

$ ./a.out 
5
8

 
 

2. Which function is used to check whether the vector is empty or not?
a) empty()
b) isempty()
c) haveElements()
d) none()
Answer: a
Clarification: empty() function is provided by the vector container to check whether it is empty or not.

3. 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); 
    v.resize(4);
	for (auto it = v.begin(); it != v.end(); it++) 
        cout << *it << " "; 
    return 0; 
}

a) 1 2 3 4 5
b) 1 2 3 4
c) 2 3 4 5
d) error
Answer: b
Clarification: resize() function is used to resize a vector container. It updates the size of vector and removes all the elements after n if new size(n) is less than previous size. Hence in the program initially the vector has 5 elements but after resizing the vector to 4 it has only 4 elements as 5 is removed.
Output:

$ ./a.out 
1 2 3 4

4. 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); 
    cout<<v.size()<<endl;
    v.resize(4);
    cout<<v.size()<<endl;
    return 0; 
}

a)

5
4

b)

5
5

c)

4
4

d) Error
Answer: a
Clarification: Intitally the size of the vector is 5 as it contains only 5 elements. After resizing the elements 5 is terminated so only 4 remains therfore the size becomes 4. Hence out is as follow.
Output:

$ ./a.out 
5
4

5. 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); 
    cout<<v.capacity()<<endl;
    v.resize(4);
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

5
4

b)

8
8

c)

5
8

d)

4
8

View Answer

Answer: b
Clarification: The capacity denotes how many elements a avector can hold. On resizing a vector the capacity of a vector is not changed hence the capacity before and after is same. Therefore the output is as follows.
Output:

$ ./a.out 
8
8

 
 

6. 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); 
    cout<<v.capacity()<<endl;
    v.shrink_to_fit();
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

5
8

b)

8
5

c) Error
d) Segmentation fault
Answer: b
Clarification: Initially we have 5 elements in the vector therefore the capacity of the vector is 8(one can observe that as capacity doubles after vector is full). Now the function shrink_to_fit() makes the capacity of vector equal to its size hence removing the extra space occupied by the vector. Therefore as only 5 elements were there in the vectore therefore the capacity becomes 8.
Output:

$ ./a.out 
8
5

7. What will be the capacity of the vector after 10 is pushed into the vector in 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); 
    cout<<v.capacity()<<endl;
    v.shrink_to_fit();
    cout<<v.capacity()<<endl;
    v.push_back(10);
    return 0; 
}

a) 8
b) 10
c) 5
d) 6
Answer: b
Clarification: After shrinking the capacity of vector the capacity of vector becomes 5. Now when a new element i.e. 10 is inserted into the vector then the capacity of the vector will double i.e. it will become 10. hence the final capacity will be 10.

8. What will be the capacity of vector at the end in 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);
    v.reserve(50);
    cout<<v.capacity();
    return 0; 
}

a) 10
b) 8
c) 50
d) 60
Answer: c
Clarification: 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.

9. 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);
    int *pos = v.data();
    cout<<*(pos + 3);
    return 0; 
}

a) 5
b) 1
c) 4
d) 3
Answer: c
Clarification: data() function in vector returns the direct pointer to the memory array which the vector has used to store its elements. Hence a pointer to vector is returned. So when we are accessing *(pos + 3) we are trying to do v[3] which is 4. Hence the output is as follows.
Output:

$ ./a.out 
4

10. 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()
Answer:a
Clarification: Vector provides push_back() function to insert an element at its end.

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);
    cout<<v.capacity()<<endl;
    v.pop_back();
    v.pop_back();
    cout<<v.capacity()<<endl;
    return 0; 
}

a)

8
4

b)

4
4

c)

8
8

d)

4
8

View Answer

Answer: c
Clarification: Vector never reduces its capacity on deleting any element which one may get confuse by thinking about the fact that vector doubles its memory on insertion. Hence both returns the same capacity.
Output:

$ ./a.out 
8
8

 
 

12. 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);
    for(int i=0;i<v.size();i++)
    	cout<<v[i]<<" ";
    cout<<endl;
    v.assign(3, 8);
    for(int i=0;i<v.size();i++)
    	cout<<v[i]<<" ";
    cout<<endl;
    return 0; 
}

a)

1 2 3 4 5 
8 8 8

b)

1 2 3 4 5 
8 8 8 8 8

c)

1 2 3 4 5 
8 8 8 4 5

d) 1 2 3 4 5
Answer: a
Clarification: assign(m,n) function changes the vector values by assigning new values to vector. It copies m times the value n to the vector by first removing all the initial values. hence the vector has 3 8’s after updation i.e. using assign(3,8) function. Hence the output is as follows.
Output:

$ ./a.out 
1 2 3 4 5 
8 8 8

13. Which function is used to swap two vectors?
a) swap()
b) change()
c) merge()
d) exchange()
Answer: a
Clarification: Vectors allows the use of swap function to swap to vectors with each other of same type and size.

14. What will be the output of the following C++ code?

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v1;
    vector<char> v2; 
    for (int i = 1; i <= 5; i++) 
        v1.push_back(i);
    for (int i = 6; i <= 10; i++) 
        v2.push_back(i);
    v1.swap(v2);
    for(int i=0;i<v1.size();i++)
    	cout<<v1[i]<<" ";
    for(int i=0;i<v2.size();i++)
    	cout<<v2[i]<<" ";
    cout<<endl;
    return 0; 
}

a) 1 2 3 4 5 6 7 8 9 10
b) 6 7 8 9 10 1 2 3 4 5
c) Error
d) Segmentation fault
Answer: c
Clarification: swap() function in vector allows to swap two vectors of same size and type but here the vectors v1 and v2 have different types therefore the program gives the error.

15. What will be the output of the following C++ code?

#include  
#include  
 
using namespace std; 
 
int main() 
{ 
    vector<int> v1;
    vector<int> v2; 
    for (int i = 1; i <= 5; i++) 
        v1.push_back(i);
    for (int i = 6; i <= 10; i++) 
        v2.push_back(i);
    v1.swap(v2);
    for(int i=0;i<v1.size();i++)
    	cout<<v1[i]<<" ";
    for(int i=0;i<v2.size();i++)
    	cout<<v2[i]<<" ";
    cout<<endl;
    return 0; 
}

a) 1 2 3 4 5 6 7 8 9 10
b) 6 7 8 9 10 1 2 3 4 5
c) Error
d) Segmentation fault
Answer: b
Clarification: Here swap() function is used and the type and size of both vectors v1 and v2 are same therefore they can be swapped and hence the program allows such swap. Therefore no error and program runs perfectly.
Output:

$ ./a.out 
6 7 8 9 10 1 2 3 4 5

contest

  250+ TOP MCQs on Void and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top