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
Answer: b
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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 -
#include -
#include -
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
Answer: d
Clarification: 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 -
#include -
#include -
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 10
View Answer
Clarification: 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 -
#include -
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
Answer: c
Clarification: 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
