C++ Programming MCQ – Sequences

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Sequences”. One shall practice these test 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 online C++ test questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ questions on “Sequences” along with answers, explanations and/or solutions:

1. How many items are there in sequence container?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: d
Explanation: There are five items in sequence container. They are array, vector, list, forward_list and dequeue.

2. Which of the following class template are based on arrays?
a) vector
b) list
c) dequeue
d) both vector & dequeue
View Answer

Answer: d
Explanation: Class template vector and class template dequeue both are based on arrays.

3. Which of the following will return the new element at the end of container?
a) front
b) back
c) push_back
d) pop_back
View Answer

Answer: b
Explanation: Q3: back() in containers are used to access the last element of the sequence.
advertisement
advertisement

4. 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 (5);  
  7.         deque<int>::reverse_iterator rit = mydeque.rbegin();
  8.         int i = 0;
  9.         for (rit = mydeque.rbegin(); rit!= mydeque.rend(); ++rit)
  10.             *rit = ++i;
  11.         for (deque<int> :: iterator it = mydeque.begin();
  12.         it != mydeque.end(); ++it)
  13.         cout << ' ' << *it;
  14.         return 0;
  15.     }

a) 12345
b) 1234
c) 54321
d) 43210
View Answer

Answer: c
Explanation: In this program, We used the operation of rbegin and rend on dequeue and produced the result.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ seq.cpp
$ a.out
5 4 3 2 1

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

advertisement
  1.     #include <iostream>
  2.     #include <deque>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         unsigned int i;
  7.         deque<int> a (3,100);
  8.         deque<int> b (5,200);
  9.         a.swap(b);
  10.         cout << "a contains:";
  11.         for (deque<int>::iterator it = a.begin(); it != a.end(); ++it)
  12.             cout << ' ' << *it;
  13.         cout << "b contains:";
  14.         for (deque<int>::iterator it = b.begin(); it != b.end(); ++it)
  15.             cout << ' ' << *it;
  16.         return 0;
  17.     }

a) a contains: 200 200 200 200 200b contains: 100 100 100
b) a contains: 100 100 100 100 100b contains: 200 200 200
c) a contains: 200 200 200 200 200b contains: 200 200 200
d) a contains: 200 200 200 200 200b contains: 100 200 150
View Answer

Answer: a
Explanation: In this program, We swapped the values of both dequeues and printing the dequeues.
Output:

advertisement
$ g++ seq1.cpp
$ a.out
a contains: 200 200 200 200 200b contains: 100 100 100

6. 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.         mydeque.push_back (100);
  9.         mydeque.push_back (200);
  10.         mydeque.push_back (300);
  11.         for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  12.         {
  13.         }
  14.         mydeque.clear();
  15.         mydeque.push_back (110);
  16.         mydeque.push_back (220);
  17.         for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
  18.             cout << ' ' << *it;
  19.         cout << '\n';
  20.         return 0;
  21.     }

a) 110
b) 220
c) Both 110 & 220
d) 330
View Answer

Answer: c
Explanation: In this program, We cleared the old values presented in the dequeue with the new values.
Output:

$ g++ seq2.cpp
$ a.out
110 220

7. 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;
  7.         int * p;
  8.         unsigned int i;
  9.         p = myvector.get_allocator().allocate(5);
  10.         for (i = 0; i < 5; i++) 
  11.             myvector.get_allocator().construct(&p[i], i);
  12.         for (i = 0; i < 5; i++)
  13.             cout << ' ' << p[i];
  14.         for (i = 0; i < 5; i++)
  15.             myvector.get_allocator().destroy(&p[i]);
  16.         myvector.get_allocator().deallocate(p, 5);
  17.         return 0;
  18.     }

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

Answer: b
Explanation: In this program, We allocated the values to the vector by using get allocater and then we are destroying it.
Output:

$ g++ seq3.cpp
$ a.out
0 1 2 3 4

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

  1.     #include <iostream>
  2.     #include <cmath>
  3.     #include <list>
  4.     using namespace std;
  5.     bool same_integral_part (double first, double second)
  6.     {  
  7.         return ( int(first) == int(second) ); 
  8.     }
  9.     struct is_near 
  10.     {
  11.         bool operator() (double first, double second)
  12.         { 
  13.             return (fabs(first - second) < 5.0); 
  14.         }
  15.     };
  16.     int main ()
  17.     {
  18.         double mydoubles[] = { 12.15,  2.72, 73.0,  12.77,  3.14, 12.77, 73.35, 72.25, 15.3,  72.25 };
  19.         list<double> mylist (mydoubles, mydoubles + 10);
  20.         mylist.sort();
  21.         mylist.unique();
  22.         mylist.unique (same_integral_part);
  23.         mylist.unique (is_near());
  24.         for (list<double> :: iterator it = mylist.begin(); it != mylist.end(); ++it)
  25.             cout << ' ' << *it;
  26.         cout << '\n';
  27.         return 0;
  28.     }

a) 2.72 12.15 72.25
b) 12.15 73.0 12.77
c) 73.35
d) 74.45
View Answer

Answer: a
Explanation: In this program, We are eliminating the values by using the unique operation in the list.
Output:

$ g++ seq4.cpp
$ a.out
2.72 12.15 72.25

9. How the list containers are implemented?
a) Using Double linked list
b) Using Single linked list
c) Using Single & Double linked list
d) Using linear linked list
View Answer

Answer: a
Explanation: List containers are implemented as doubly-linked lists. Doubly linked lists can store each of the elements they contain in different and unrelated storage locations.

10. Which of the following does not support any insertion or deletion?
a) Array
b) Vector
c) Dequeue
d) List
View Answer

Answer: a
Explanation: Because array is not dynamic in nature, So they can’t be manipulated.

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.