C++ Programming Questions and Answers – Sequence Adapters

This section on C++ programming interview questions and answers focuses on “Sequence Adapters”. 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++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language programming interview questions on “Sequence Adapters” along with answers, explanations and/or solutions:

1. What do container adapter provide to interface?
a) Restricted interface
b) More interface
c) No interface
d) Memory interface
View Answer

Answer: a
Explanation: A container adapter provides a restricted interface to a container.In particular, adapters do not provide iterators; they are intended to be used only through their specialized interfaces.

2. What does the sequence adaptor provide?
a) Insertion
b) Deletion
c) Interface to sequence container
d) Insertion & Deletion
View Answer

Answer: c
Explanation: Sequence adaptor provides interface to sequence container.

3. Which are presented in the container adaptors?
a) stack
b) queue
c) priority_queue
d) all of the mentioned
View Answer

Answer: d
Explanation: These mentioned things are presented in container adapters.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <queue>  
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         queue<int> myqueue;
  7.         myqueue.push(12);
  8.         myqueue.push(75);  
  9.         myqueue.back() -= myqueue.front();
  10.         cout << myqueue.back() << endl;
  11.         return 0;
  12.     }

a) 12
b) 75
c) 63
d) 74
View Answer

Answer: c
Explanation: In this program, We used the queue operation and performed the back operation. Because of that operation, We got the output as 63.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ sca.cpp
$ a.out
63

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

advertisement
  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         queue<int> myqueue;
  7.         int sum (0);
  8.         for (int i = 1; i <= 10; i++) 
  9.             myqueue.push(i);
  10.         while (!myqueue.empty())
  11.         {
  12.             sum += myqueue.front();
  13.             myqueue.pop();
  14.         }
  15.         cout << sum << endl;
  16.         return 0;
  17.     }

a) 51
b) 52
c) 54
d) 55
View Answer

Answer: d
Explanation: In this program, We used the push and pop operation of quueue to find out the total of all the number from 1 to 10.
Output:

advertisement
$ g++ sca1.cpp
$ a.out
55

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

  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         priority_queue<int> mypq;
  7.         mypq.push(30);
  8.         mypq.push(100);
  9.         mypq.push(25);
  10.         mypq.push(40);
  11.         while (!mypq.empty())
  12.         {
  13.             cout << " " << mypq.top();
  14.             mypq.pop();
  15.         }
  16.         cout << endl;
  17.         return 0;
  18.     }

a) 100 40 30 25
b) 100 40 30
c) 100 40
d) 100 30 25
View Answer

Answer: a
Explanation: In this program, We used priority_queue and with that we are pushing and popping out the elements.
Output:

$ g++ sca2.cpp
$ a.out
100 40 30 25

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

  1.     #include <iostream>
  2.     #include <stack>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         stack<int> myints;
  7.         cout  << (int) myints.size();
  8.         for (int i = 0; i < 5; i++) myints.push(i);
  9.         cout  << (int) myints.size() << endl;
  10.         return 0;
  11.     }

a) 05
b) 15
c) 24
d) 102
View Answer

Answer: a
Explanation: In this program, We declared myints and not initialized in first option, So it’s value is 0 and on another, We are pushing 5 values, So it’s size is 5.
Output:

$ g++ sca3.cpp
$ a.out
05

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

  1.     #include <iostream>
  2.     #include <stack>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         stack<int> mystack;
  7.         mystack.push(10);
  8.         mystack.push(20);
  9.         mystack.top() -= 5;
  10.         cout << mystack.top() << endl;
  11.         return 0;
  12.     }

a) 10
b) 20
c) 13
d) 15
View Answer

Answer: d
Explanation: In this program, We used top option and this will return the reference to the next element.
Output:

$ g++ sca4.cpp
$ a.out
15

9. In which context does the stack operates?
a) FIFO
b) LIFO
c) Both FIFO & LIFO
d) LIFI
View Answer

Answer: b
Explanation: A stack is a container where elements operate in a LIFO context, where elements are inserted (pushed) and removed (popped) from the end of the container.

10. Which operator is used in priority queue?
a) operator<
b) operator>
c) operator)
d) operator!
View Answer

Answer: a
Explanation: It is used to decide the priority of two elements to be inserted in the queue.

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.