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
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
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
Explanation: 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 <iostream>
#include <deque>
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
View Answer
Explanation: 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 <iostream>
#include <deque>
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
View Answer
Explanation: 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
6. What will be the output of the following C++ code?
#include <iostream>
#include <deque>
using namespace std;
int main ()
{
unsigned int i;
deque<int> mydeque;
mydeque.push_back (100);
mydeque.push_back (200);
mydeque.push_back (300);
for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
{
}
mydeque.clear();
mydeque.push_back (110);
mydeque.push_back (220);
for(deque<int> :: iterator it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
a) 110
b) 220
c) Both 110 & 220
d) 330
View Answer
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?
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
int * p;
unsigned int i;
p = myvector.get_allocator().allocate(5);
for (i = 0; i < 5; i++)
myvector.get_allocator().construct(&p[i], i);
for (i = 0; i < 5; i++)
cout << ' ' << p[i];
for (i = 0; i < 5; i++)
myvector.get_allocator().destroy(&p[i]);
myvector.get_allocator().deallocate(p, 5);
return 0;
}
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
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?
#include <iostream>
#include <cmath>
#include <list>
using namespace std;
bool same_integral_part (double first, double second)
{
return ( int(first) == int(second) );
}
struct is_near
{
bool operator() (double first, double second)
{
return (fabs(first - second) < 5.0);
}
};
int main ()
{
double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14, 12.77, 73.35, 72.25, 15.3, 72.25 };
list<double> mylist (mydoubles, mydoubles + 10);
mylist.sort();
mylist.unique();
mylist.unique (same_integral_part);
mylist.unique (is_near());
for (list<double> :: iterator it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
a) 2.72 12.15 72.25
b) 12.15 73.0 12.77
c) 73.35
d) 74.45
View Answer
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
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
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]
- Practice Programming MCQs
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check Programming Books
- Check C++ Books