C++ Programming Questions and Answers – Sequences and Containers

This section on C++ interview questions and answers focuses on “Sequences and Containers”. 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 “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

Answer: a
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

Answer: c
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

Answer: c
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.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector (3);
  7.         for (unsigned i = 0; i < myvector.size(); i++)
  8.         myvector.at(i) = i;
  9.         for (unsigned i = 0; i < myvector.size(); i++)
  10.         cout << ' ' << myvector.at(i);
  11.         return 0;
  12.     }

a) 1 2 3
b) 0 1 2
c) 1 2 3 4
d) 1 3 5 9
View Answer

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

advertisement
  1.     #include <iostream>
  2.     #include <vector>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         vector<int> myvector;
  7.         myvector.push_back(78);
  8.         myvector.push_back(16);
  9.         myvector.front() += myvector.back();
  10.         cout << myvector.front() << '\n';
  11.         return 0;
  12.     }

a) 78
b) 16
c) 94
d) 86
View Answer

Answer: c
Explanation: In this program, We added all the values in the vector by using front and back operation.
Output:

advertisement
$ g++ seqc1.cpp
$ a.out
94

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <list>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         list<int> mylist;
  7.         list<int> :: iterator it1, it2;
  8.         for (int i = 1; i < 10; ++i) mylist.push_back(i * 1);
  9.             it1 = it2 = mylist.begin();
  10.         advance (it2, 6);
  11.         ++it1;
  12.         it1 = mylist.erase (it1);
  13.         it2 = mylist.erase (it2);
  14.         ++it1;
  15.         --it2;
  16.         mylist.erase (it1, it2);
  17.         for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
  18.             cout << ' ' << *it1;
  19.         return 0;
  20.     }

a) 1 3 6
b) 8 9
c) 1 3 6 8 9
d) 4 6 8 9
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <deque> 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         deque<int> mydeque;
  8.         deque<int> :: iterator it;
  9.         mydeque.push_back ( 100 );
  10.         mydeque.push_back ( 200 );
  11.         mydeque.push_back ( 300 );
  12.         for (it = mydeque.begin(); it != mydeque.end(); ++it)
  13.             mydeque.clear();
  14.         cout << ' ' << *it;
  15.     }

a) 100
b) 200
c) 300
d) error
View Answer

Answer: d
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?

  1.     #include <iostream>
  2.     #include <deque>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         deque<int> mydeque;
  7.         int sum (0);
  8.         mydeque.push_back ( 10 );
  9.         mydeque.push_back ( 20 );
  10.         mydeque.push_back ( 30 );
  11.         while (!mydeque.empty())
  12.         {
  13.             sum += mydeque.back();
  14.             mydeque.pop_back();
  15.         }
  16.         cout << sum << '\n';
  17.         return 0;
  18.     }

a) 10
b) 20
c) 30
d) 60
View Answer

Answer: d
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

Answer: a
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

Answer: a
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.