Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Vector Container Aptitude Questions and Answers
C++ Vector Container Aptitude: This section contains C++ Vector Container Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 09, 2021
1) There are the following statements that are given below, which of them are correct about vector container in C++?
- It is a sequence container class.
- It is a derived container class.
- It is used to implement a dynamic array; it means size will automatically increase when we add new items.
- The vector objects store items in a contiguous manner.
Options:
- A and C
- B and C
- A, C, and D
- B, C, and D
Correct Answer - 3
A, C, and D
Explanation:
Statements A, C, and D are correct about vector containers in C++.
2) Which of the following is correct syntax to create an object "ob" of vector container class in C++?
- vector ob;
- vector <data_type> ob;
- Vector ob;
- Vector <data_type> ob;
Correct Answer - 2
vector <data_type> ob;
Explanation:
The 2nd option is correct syntax to create an object ob of vector container class in C++.
3) Which of the following header file is required to use vector container class in C++?
- <vector>
- <vcontainer>
- <scontainer>
- <seqcont>
Correct Answer - 1
<vector>
Explanation:
The <vector> header is required to use vector container class in C++.
4) Which of the following function is used to add an element to the end in the vector?
- add()
- push()
- push_back()
- push_end()
Correct Answer - 3
push_back()
Explanation:
The push_back() function is used to add an element to the end in the vector.
5) Which of the following function is used to add an element at the specified position in the vector?
- add()
- insert()
- additem()
- insertitem()
Correct Answer - 2
insert()
Explanation:
The insert() function is used to add an element at the specified position in the vector.
6) Which of the following is correct syntax to create an iterator object "itr" to access vector container elements in C++?
- vector<data_type>::iterator itr;
- vector<data_type>.iterator itr;
- vector::iterator<data_type> itr;
- iterator<data_type> itr;
Correct Answer - 1
vector<data_type>::iterator itr;
Explanation:
The 1st option is correct syntax to create an iterator object.
7) What is the correct output of given code snippets in C++?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> vec;
vector<string>::iterator iter;
vec.push_back("India ");
vec.push_back("UP ");
vec.push_back("Noida ");
iter = vec.begin();
while (iter != vec.end()) {
++iter;
cout << *iter;
}
return 0;
}
Options:
- India UP Noida
- UP Noida
- India UP
- Syntax error
Correct Answer - 2
UP Noida
Explanation:
The above code will print "UP Noida" on the console screen.
8) What is the correct output of given code snippets in C++?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> vec;
vector<string>::iterator iter;
vec.push_back("India ");
vec.push_back("UP ");
vec.push_back("Noida ");
iter = vec.begin();
cout << *iter;
while (iter != vec.end()) {
++iter;
cout << *iter;
}
return 0;
}
Options:
- India UP Noida
- UP Noida
- India UP
- Syntax error
Correct Answer - 1
India UP Noida
Explanation:
The above code will print "India UP Noida" on the console screen.
9) Which of the following function is used to check a vector object is empty or not?
- isempty()
- empty()
- noelement()
- noitem()
Correct Answer - 2
empty()
Explanation:
The empty() function is used to check a vector object is empty or not.
10) Which of the following function is used to remove all elements of vector in C++?
- remove()
- removeall()
- clear()
- erase()
Correct Answer - 3
clear()
Explanation:
The clear() function is used to remove all elements of vector in C++.
11) Which of the following function is used to get total number of elements in vector?
- len()
- length()
- count()
- size()
Correct Answer - 4
size()
Explanation:
The size() function is used to get total number of elements in vector.
12) What is the correct output of given code snippets?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
for (int i = 0; i < v1.size(); i++)
cout << v1[i] << " ";
return 0;
}
Options:
- 1 2 3 4
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 1
1 2 3 4
Explanation:
The above code will print "1 2 3 4" on the console screen.
13) What is the correct output of given code snippets?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
for (int i = 0; i < v1.size(); i++)
cout << *(v1 + i) << " ";
return 0;
}
Options:
- 1 2 3 4
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
14) What is the correct output of the given code snippets?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v2.push_back(5);
v2.push_back(6);
v2.push_back(7);
v2.push_back(8);
v1.swap(v2);
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
return 0;
}
Options:
- 1 2 3 4
- 5 6 7 8
- Garbage value
- Syntax error
Correct Answer - 1
1 2 3 4
Explanation:
The above code will print "1 2 3 4" on the console screen.
15) What is the correct output of the given code snippets?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
v2.push_back(4);
if (v1 == v2)
cout << "both are equal";
else
cout << "both are not equal";
return 0;
}
Options:
- both are equal
- both are not equal
- Syntax error
- Runtime error
Correct Answer - 1
both are equal
Explanation:
The above code will print "both are equal" on the console screen.
16) Which of the following operators are overloaded in the vector container?
- <
- >
- +
- -
Options:
- A and B
- C and D
- A, B, C, and D
- None of the above
Correct Answer - 1
A and B
Explanation:
The '<' and '>' operators are overloaded in the vector container.
17) Which of the following function is used to get how many elements a vector can hold?
- maxlen()
- max_len()
- maxsize()
- max_size()
Correct Answer - 4
max_size()
Explanation:
The max_size() function is used to get how many elements a vector can hold.
18) Which of the following are not correct functions of vector class in C++?
- begin()
- rend()
- rand()
- cend()
Options:
- A and B
- A and D
- C
- D
Correct Answer - 3
C
Explanation:
The rand() function is not present in the vector class.
19) Which of the following function is used to insert an item just before a given position in the vector?
- insertbefore()
- emplace()
- emplace_back()
- insert_back()
Correct Answer - 2
emplace()
Explanation:
The emplace() function is used to insert an item just before a given position in the vector.
20) Which of the following function is used to write data from vector to an array?
- data()
- writedata()
- convertvector()
- coverttoarray()
Correct Answer - 1
data()
Explanation:
The data() function is used to write data from vector to an array.
21) What is the correct output of the given code snippets?
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("India ");
v.push_back("UP ");
v.push_back("Noida ");
string str[3] = v.data();
for (int i = 0; i < v.size(); i++)
cout << *str++ << " ";
return 0;
}
Options:
- India UP Noida
- UP Noida
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
22) What is the correct output of given code snippets in C++?
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("India ");
v.push_back("UP ");
v.push_back("Noida ");
string str[3] = v.data();
for (int i = 0; i < v.size(); i++)
cout << str[i] << " ";
return 0;
}
Options:
- India UP Noida
- UP Noida
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
23) What is the correct output of given code snippets in C++?
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("India ");
v.push_back("UP ");
v.push_back("Noida ");
string* str = v.data();
for (int i = 0; i < v.size(); i++)
cout << *str++ << " ";
return 0;
}
Options:
- India UP Noida
- UP Noida
- Syntax error
- Runtime error
Correct Answer - 1
India UP Noida
Explanation:
The above code will print "India UP Noida " on the console screen.
24) What is the correct output of given code snippets in C++?
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("India ");
v.push_back("UP ");
v.push_back("Noida ");
string* str = v.data();
for (int i = 0; i < v.size(); i++)
cout << str[i] << " ";
return 0;
}
Options:
- India UP Noida
- UP Noida
- Syntax error
- Runtime error
Correct Answer - 1
India UP Noida
Explanation:
The above code will print "India UP Noida " on the console screen.
25) What is the correct output of given code snippets in C++?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
int* num = v.data();
for (int i = 0; i < v.size(); i++)
cout << num[i] << " ";
return 0;
}
Options:
- 10 20 30
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.