Here is a listing of C++ interview questions on “Vector” along with answers, explanations and/or solutions:
1. What do vectors represent?
a) Static arrays
b) Dynamic arrays
c) Stack
d) Queue
View Answer
Explanation: Vectors are sequence containers representing arrays that can change in size.
2. In which type of storage location are the vector members stored?
a) Contiguous storage locations
b) Non-contiguous storage locations
c) Contiguous & Non-contiguous storage locations
d) Memory storage locations
View Answer
Explanation: Vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements
3. How many vector container properties are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are three container properties in c++. They are sequence, Dynamic array and allocator-aware.
4. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
vector<int> first;
vector<int> second (4, 100);
vector<int> third (second.begin(), second.end());
vector<int> fourth (third);
int myints[] = {16, 2, 77, 29};
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
for (vector<int> :: iterator it = fifth.begin(); it != fifth.end(); ++it)
cout << ' ' << *it;
return 0;
}
a) 16
b) 16 2
c) 16 2 77
d) 16 2 77 29
View Answer
Explanation: In this program, We got the values and printing it by using the vector and we are contructing vectors.
Output:
$ g++ vect.cpp $ a.out 16 2 77 29
5. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int sum (0);
myvector.push_back (100);
myvector.push_back (200);
myvector.push_back (300);
while (!myvector.empty())
{
sum += myvector.back();
myvector.pop_back();
}
cout << sum << '\n';
return 0;
}
a) 500
b) 600
c) 700
d) Error
View Answer
Explanation: In this program, We are forming a stack and adding the elements and We are finding the total number of elements that are in stack.
Output:
$ g++ vect1.cpp $ a.out 600
6. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> a (3, 0);
vector<int> b (5, 0);
b = a;
a = vector<int>();
cout << "Size of a " << int(a.size()) << '\n';
cout << "Size of b " << int(b.size()) << '\n';
return 0;
}
a)
Size of a 0 Size of b 3
b)
Size of a 3 Size of b 5
c) Error
d)
Size of a 3 Size of b 9View Answer
Explanation: In this program, We are finding the size of the vector elements.
Output:
$ g++ vect2.cpp $ a.out Size of a 0 Size of b 3
7. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> first;
first.assign (7,100);
vector<int>::iterator it;
it=first.begin()+1;
int myints[] = {1776,7,4};
cout << int (first.size()) << '\n';
return 0;
}
a) 10
b) 9
c) 8
d) 7
View Answer
Explanation: In this program, We are finding the size of the vector elements and resizing it.
Output:
$ g++ vect3.cpp $ a.out 7
8. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100;
for (unsigned i = 0; i < myvector.size(); ++i)
cout << ' ' << myvector[i];
return 0;
}
a) 10 20 0 100 0
b) 10 20 0 100
c) 10 20 0
d) 10 20
View Answer
Explanation: In this program, We are allocating the values to the vector and unallocated values are left as zero.
Output:
$ g++ vect4.cpp $ a.out 10 20 0 100 0
9. Pick out the correct statement about vector.
a) vector<int> values (5)
b) vector values (5)
c) vector<int> (5)
d) vector<5>
View Answer
Explanation: The syntax for declaring the vector element is vector<type> variable_name (number_of_elements);
10. Which is optional in the declaration of vector?
a) Type
b) Name
c) Vector
d) Number_of_elements
View Answer
Explanation: The number of elements is optional. An empty vector means, A vector that contains zero elements.
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.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Check Computer Science Books
- Check C++ Books
- Apply for Computer Science Internship
- Apply for C++ Internship