This section on C++ programming interview questions and answers 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: d
Clarification: These mentioned things are presented in container adapters.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
queue<int> myqueue;
-
myqueue.push(12);
-
myqueue.push(75);
-
myqueue.back() -= myqueue.front();
-
cout << myqueue.back() << endl;
-
return 0;
-
}
a) 12
b) 75
c) 63
d) 74
Answer: c
Clarification: In this program, We used the queue operation and performed the back operation. Because of that operation, We got the output as 63.
Output:
5. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
queue<int> myqueue;
-
int sum (0);
-
for (int i = 1; i <= 10; i++)
-
myqueue.push(i);
-
while (!myqueue.empty())
-
{
-
sum += myqueue.front();
-
myqueue.pop();
-
}
-
cout << sum << endl;
-
return 0;
-
}
a) 51
b) 52
c) 54
d) 55
Answer: d
Clarification: 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:
$ g++ sca1.cpp
$ a.out
55
6. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
priority_queue<int> mypq;
-
mypq.push(30);
-
mypq.push(100);
-
mypq.push(25);
-
mypq.push(40);
-
while (!mypq.empty())
-
{
-
cout << " " << mypq.top();
-
mypq.pop();
-
}
-
cout << endl;
-
return 0;
-
}
a) 100 40 30 25
b) 100 40 30
c) 100 40
d) 100 30 25
Answer: a
Clarification: 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?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
stack<int> myints;
-
cout << (int) myints.size();
-
for (int i = 0; i < 5; i++) myints.push(i);
-
cout << (int) myints.size() << endl;
-
return 0;
-
}
a) 05
b) 15
c) 24
d) 102
Answer: a
Clarification: 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?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
stack<int> mystack;
-
mystack.push(10);
-
mystack.push(20);
-
mystack.top() -= 5;
-
cout << mystack.top() << endl;
-
return 0;
-
}
a) 10
b) 20
c) 13
d) 15
Answer: d
Clarification: 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
Answer: b
Clarification: 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!
Answer: a
Clarification: It is used to decide the priority of two elements to be inserted in the queue.