Here is a listing of online C++ test 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
Answer: d
Clarification: 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
Answer: d
Clarification: 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
Answer: b
Clarification: Q3: back() in containers are used to access the last element of the sequence.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
deque<int> mydeque (5);
-
deque<int>::reverse_iterator rit = mydeque.rbegin();
-
int i = 0;
-
for (rit = mydeque.rbegin(); rit!= mydeque.rend(); ++rit)
-
*rit = ++i;
-
for (deque<int> :: iterator it = mydeque.begin();
-
it != mydeque.end(); ++it)
-
cout << ' ' << *it;
-
return 0;
-
}
a) 12345
b) 1234
c) 54321
d) 43210
Answer: c
Clarification: In this program, We used the operation of rbegin and rend on dequeue and produced the result.
Output:
$ g++ seq.cpp $ a.out 5 4 3 2 1
5. What will be the output of the following C++ code?
-
#include
-
#include
-
using namespace std;
-
int main ()
-
{
-
unsigned int i;
-
deque<int> a (3,100);
-
deque<int> b (5,200);
-
a.swap(b);
-
cout << "a contains:";
-
for (deque<int>::iterator it = a.begin(); it != a.end(); ++it)
-
cout << ' ' << *it;
-
cout << "b contains:";
-
for (deque<int>::iterator it = b.begin(); it != b.end(); ++it)
-
cout << ' ' << *it;
-
return 0;
-
}
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
Answer: a
Clarification: In this program, We swapped the values of both dequeues and printing the dequeues.
Output:
$ g++ seq1.cpp $ a.out a contains: 200 200 200 200 200b contains: 100 100 100