Here is a listing of C++ interview questions on “Sequences and Containers” along with answers, explanations and/or solutions:
1. What kind of iteration does forward_list provide in C++?
a) Uni-directional
b) Bi-directional
c) Multi-directional
d) Bi-directional & Multi-directional
View Answer
Explanation: The forward_list uses singly linked list hence it is uni-directional. In the forward_list, the container provides insertion and removal at anywhere in the program.
2. What does the size of the vector refers to in c++?
a) Size of vector
b) Type of vector
c) Number of elements
d) Name of vector
View Answer
Explanation: In vectors, by size we mean the number of elements in that vector array.
3. Subsequent elements are moved in terms of _____ when an element in inserted in vector?
a) Assignment Operator
b) Copy constructor
c) Both assignment operator and copy constructor
d) destructor
View Answer
Explanation: The vector maintains a certain order of its elements, so that when a new element is inserted at the beginning or in the middle of the vector, Subsequent elements are moved backwards in terms of their assignment operator or copy constructor.
4. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector (3);
for (unsigned i = 0; i < myvector.size(); i++)
myvector.at(i) = i;
for (unsigned i = 0; i < myvector.size(); i++)
cout << ' ' << myvector.at(i);
return 0;
}
a) 1 2 3
b) 0 1 2
c) 1 2 3 4
d) 1 3 5 9
View Answer
Explanation: In this program, We are pushing the values into the vector from 0 to 3 by using for loop.
Output:
$ g++ seqc.cpp $ a.out 0 1 2
5. What will be the output of the following C++ code?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
myvector.push_back(78);
myvector.push_back(16);
myvector.front() += myvector.back();
cout << myvector.front() << '\n';
return 0;
}
a) 78
b) 16
c) 94
d) 86
View Answer
Explanation: In this program, We added all the values in the vector by using front and back operation.
Output:
$ g++ seqc1.cpp $ a.out 94
6. What will be the output of the following C++ code?
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> mylist;
list<int> :: iterator it1, it2;
for (int i = 1; i < 10; ++i) mylist.push_back(i * 1);
it1 = it2 = mylist.begin();
advance (it2, 6);
++it1;
it1 = mylist.erase (it1);
it2 = mylist.erase (it2);
++it1;
--it2;
mylist.erase (it1, it2);
for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
cout << ' ' << *it1;
return 0;
}
a) 1 3 6
b) 8 9
c) 1 3 6 8 9
d) 4 6 8 9
View Answer
Explanation: In this program, We are comparing the values in both lists and erasing it according to certain condition.
Output:
$ g++ seqc2.cpp $ a.out 1 3 6 8 9
7. What will be the output of the following C++ code?
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
unsigned int i;
deque<int> mydeque;
deque<int> :: iterator it;
mydeque.push_back ( 100 );
mydeque.push_back ( 200 );
mydeque.push_back ( 300 );
for (it = mydeque.begin(); it != mydeque.end(); ++it)
mydeque.clear();
cout << ' ' << *it;
}
a) 100
b) 200
c) 300
d) error
View Answer
Explanation: Segmentation fault will occur as we are trying to access the freed memory cell i.e. we are trying to access the element which is already deleted.
8. What will be the output of the following C++ code?
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
deque<int> mydeque;
int sum (0);
mydeque.push_back ( 10 );
mydeque.push_back ( 20 );
mydeque.push_back ( 30 );
while (!mydeque.empty())
{
sum += mydeque.back();
mydeque.pop_back();
}
cout << sum << '\n';
return 0;
}
a) 10
b) 20
c) 30
d) 60
View Answer
Explanation: In this program, We are adding all the values in the queue.
Output:
$ g++ seqc3.cpp $ a.out 60
9. What is the use of adapter in STL in c++?
a) To provide interface
b) To manipulate the data
c) To extract the data
d) To delete the data
View Answer
Explanation: Adapters are data types from STL that adapt a container to provide specific interface.
10. Which is used to iterate over container?
a) Associated iterator type
b) Data type of objects
c) Return type of variables
d) Name of the variables
View Answer
Explanation: Associated iterator type is used to iterate over container.
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.
- Practice Programming MCQs
- Check C++ Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Check Programming Books