Here is a listing of C++ aptitude questions on “Associative Containers” along with answers, explanations and/or solutions:
1. What do associate containers implement?
a) Arrays
b) Associative arrays
c) Functional Arrays
d) Static arrays
View Answer
Explanation: Associative containers refer to a group of class templates in the standard library of the C++ programming language that implements ordered associative arrays.
2. By using which of the following the elements in the associate container can be efficiently accessed?
a) Key
b) Position
c) Both Key & Position
d) Value
View Answer
Explanation: Associative containers are designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position.
3. How many items are presented in the associate container?
a) 2
b) 3
c) 4
d) 5
View Answer
Explanation: There are 4 items presented in the associate container. They are set, multiset, map and multimap.
4. What will be the output of the following C++ code?
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main ()
{
string mystring;
bitset<4> mybits;
mybits.set();
mystring = mybits.to_string<char, char_traits<char>,
allocator<char> >();
cout << mystring << endl;
return 0;
}
a) 0000
b) 0001
c) 0011
d) 1111
View Answer
Explanation: In this program, We converted the bitset values to string and printing it.
Output:
$ g++ asc.cpp $ a.out 1111
5. What will be the output of the following C++ code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
vector<int> first (5, 10);
vector<int> second (5, 33);
vector<int>::iterator it;
swap_ranges(first.begin() + 1, first.end() - 1, second.begin());
cout << " first contains:";
for (it = first.begin(); it != first.end(); ++it)
cout << " " << *it;
cout << "\nsecond contains:";
for (it = second.begin(); it != second.end(); ++it)
cout << " " << *it;
return 0;
}
a)
first contains: 10 33 33 33 10 second contains: 10 10 10 33 33
b)
first contains: 10 33 33 33 10 second contains: 10 10 10 33 10
c)
first contains: 10 33 33 33 30 second contains: 10 10 10 33 10
d)
first contains: 10 10 10 33 30 second contains: 10 10 10 10 10View Answer
Explanation: In this program, We swapped the values according to their position.
Output:
$ g++ asc1.cpp $ a.out first contains: 10 33 33 33 10 second contains: 10 10 10 33 33
6. What will be the output of the following C++ code?
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char, int> mymap;
map<char, int> :: iterator it;
mymap['b'] = 100;
mymap['a'] = 200;
mymap['c'] = 300;
for (map<char, int> :: iterator it = mymap.begin(); it != mymap.end(); ++it)
cout << it -> first << " => " << it -> second << '\n';
return 0;
}
a)
a => 200 c => 300
b)
a => 200 b => 100
c)
a => 200 b => 100 c => 300
d) a => 200
View Answer
Explanation: In this program, We used the map template and then we used the begin operation and then we are printing the elements.
Output:
$ g++ asc2.cpp $ a.out a => 200 b => 100 c => 300
7. What will be the output of the following C++ code?
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset;
myset.insert(20);
myset.insert(30);
myset.insert(10);
while (!myset.empty())
{
cout << ' ' << *myset.begin();
myset.erase(myset.begin());
}
cout << '\n';
return 0;
}
a) 10
b) 20
c) 30
d) 10 20 30
View Answer
Explanation: In this program, We used the set template and then we are initializing the values and then we are erasing it.
Output:
$ g++ asc3.cpp $ a.out 10 20 30
8. What will be the output of the following C++ code?
#include <iostream>
#include <set>
using namespace std;
int main ()
{
multiset<int> mymultiset;
for (int i = 0; i < 5; i++) mymultiset.insert(i);
multiset<int> :: key_compare mycomp = mymultiset.key_comp();
int highest = *mymultiset.rbegin();
multiset<int> :: iterator it = mymultiset.begin();
do
{
cout << ' ' << *it;
} while (mycomp(*it++, highest));
return 0;
}
a) 12345
b) 01234
c) 1234
d) 0123
View Answer
Explanation: In this program, We used the set template and then we compared the keys and printing the result.
Output:
$ g++ asc4.cpp $ a.out 0 1 2 3 4
9. How many instances are allowed by map and set while inserting an element into container?
a) 1
b) 2
c) 3
d) Multiple
View Answer
Explanation: Both map and set only allow one instance of a key or element to be inserted into the container.
10. What do maps and sets support?
a) Single directional iterators
b) Bi-directional iterators
c) Single & Bi-directional directional iterators
d) functional iterators
View Answer
Explanation: Bi-directional iterator are used to move in both direction from any element i.e. both forward and backward movements are allowed.
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 Computer Science MCQs
- Apply for C++ Internship
- Check Computer Science Books
- Practice Programming MCQs
- Check C++ Books