C++ Programming Questions and Answers – Associative Containers

«
»
This section on C++ aptitude questions and answers focuses on “Associative Containers”. One shall practice these aptitude questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams, aptitude tests 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++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: b
Explanation: Associative containers refer to a group of class templates in the standard library of the C++ programming language that implements ordered associative arrays.
advertisement

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

Answer: a
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

Answer: c
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?

  1.     #include <iostream>
  2.     #include <string>
  3.     #include <bitset>
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         string mystring;
  8.         bitset<4> mybits; 
  9.         mybits.set();
  10.         mystring = mybits.to_string<char, char_traits<char>, 
  11.         allocator<char> >();
  12.         cout << mystring << endl;
  13.         return 0;
  14.     }

a) 0000
b) 0001
c) 0011
d) 1111
View Answer

Answer: d
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?

  1.     #include <iostream>
  2.     #include <algorithm>
  3.     #include <vector>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         vector<int> first (5, 10);
  8.         vector<int> second (5, 33);
  9.         vector<int>::iterator it;
  10.         swap_ranges(first.begin() + 1, first.end() - 1, second.begin());
  11.         cout << " first contains:";
  12.         for (it = first.begin(); it != first.end(); ++it)
  13.             cout << " " << *it;
  14.         cout << "\nsecond contains:";
  15.         for (it = second.begin(); it != second.end(); ++it)
  16.             cout << " " << *it;
  17.         return 0;
  18.     }

a)

   first contains: 10 33 33 33 10
   second contains: 10 10 10 33 33
advertisement

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
Answer: a
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?

  1.     #include <iostream>
  2.     #include <map>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         map<char, int> mymap;
  7.         map<char, int> :: iterator it;
  8.         mymap['b'] = 100;
  9.         mymap['a'] = 200;
  10.         mymap['c'] = 300;
  11.         for (map<char, int> :: iterator it = mymap.begin(); it != mymap.end(); ++it)
  12.             cout << it -> first << " => " << it -> second << '\n';
  13.         return 0;
  14.     }

a)

   a => 200 
   c => 300
advertisement

b)

   a => 200
   b => 100

c)

   a => 200  
   b => 100
   c => 300

d) a => 200
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <set>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         set<int> myset;
  7.         myset.insert(20);
  8.         myset.insert(30);
  9.         myset.insert(10);
  10.         while (!myset.empty())
  11.         {
  12.             cout << ' ' << *myset.begin();
  13.             myset.erase(myset.begin());
  14.         }
  15.         cout << '\n';
  16.         return 0;
  17.     }

a) 10
b) 20
c) 30
d) 10 20 30
View Answer

Answer: d
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?

  1.     #include <iostream>
  2.     #include <set>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         multiset<int> mymultiset;
  7.         for (int i = 0; i < 5; i++) mymultiset.insert(i);
  8.         multiset<int> :: key_compare mycomp = mymultiset.key_comp();
  9.         int highest = *mymultiset.rbegin();
  10.         multiset<int> :: iterator it = mymultiset.begin();
  11.         do 
  12.         {
  13.             cout << ' ' << *it;
  14.         } while (mycomp(*it++, highest));
  15.         return 0;
  16.     }

a) 12345
b) 01234
c) 1234
d) 0123
View Answer

Answer: b
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

Answer: a
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

Answer: b
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.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn