This section on C++ interview questions and answers on “Vector”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
-
#include
-
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
Answer: d
Clarification: 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
-
#include
-
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
Answer: b
Clarification: 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
-
#include
-
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 9
View Answer
Answer: a
Clarification: 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
-
#include
-
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
Answer: d
Clarification: 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
-
#include
-
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
Answer: a
Clarification: 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 values (5)
b) vector values (5)
c) vector (5)
d) vector<5>
Answer: a
Clarification: The syntax for declaring the vector element is vector variable_name (number_of_elements);
10. Which is optional in the declaration of vector?
a) Type
b) Name
c) Vector
d) Number_of_elements
Answer: d
Clarification: The number of elements is optional. An empty vector means, A vector that contains zero elements.