C++ Programming Questions and Answers – seq_con Vector Class – 2

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

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

a)

5
5

b)

advertisement
advertisement
5
8

c)

5
10

d)

Note: Join free Sanfoundry classes at Telegram or Youtube
8
8
View Answer
Answer: b
Explanation: 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
 
 

advertisement

2. Which function is used to check whether the vector is empty or not?
a) empty()
b) isempty()
c) haveElements()
d) none()
View Answer

Answer: a
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: b
Explanation: 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:

advertisement
$ ./a.out 
1 2 3 4

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

a)

5
4

b)

5
5

c)

4
4

d) Error
View Answer

Answer: a
Explanation: 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 <iostream> 
#include <vector> 
 
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
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: b
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: b
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: c
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: c
Explanation: 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()
View Answer

Answer:a
Explanation: Vector provides push_back() function to insert an element at its end.

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);
    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
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: a
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: c
Explanation: 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 <iostream> 
#include <vector> 
 
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
View Answer

Answer: b
Explanation: 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

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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.