C++ Programming Questions and Answers – Almost Containers

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

Here is a listing of C++ interview questions on “Almost Containers” along with answers, explanations and/or solutions:

1. What kind of library is Standard Template Library?
a) Polymorphic
b) Generic
c) Both Polymorphic & Generic
d) Virtual
View Answer

Answer: b
Explanation: The STL is a generic library, meaning that its components are heavily parameterized.

2. To what type of object does the container can be instantiated?
a) int
b) float
c) double
d) any type of object
View Answer

Answer: d
Explanation: All type of object does the container can be instantiated.

3. What type of class template is list?
a) Class-based
b) Node-based
c) Method-based
d) size-based
View Answer

Answer: b
Explanation: It is node-based because it allows for efficient insertion anywhere in the program.
advertisement
advertisement

4. What type of access does deque and vector provide?
a) Linear access
b) Parallel access
c) Random access
d) Memory access
View Answer

Answer: c
Explanation: Because they can manipulate the values on anywhere in the program, So it is providing random access.

5. Where does the vector add the item?
a) End
b) Insert
c) Middle
d) Start
View Answer

Answer: a
Explanation: Vector allows insertion of element at the end.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. Which are not full container classes in c++?
a) Sequence container
b) Associative container
c) Container adaptor
d) iterative container
View Answer

Answer: c
Explanation: Container adaptors are not full container classes, but classes that provide a specific interface relying on an object of one of the container classes such as deque or list to handle the elements.

7. What is the lifetime of the element in container?
a) Whole program
b) Outside the block
c) Everywhere
d) Only on that container
View Answer

Answer: d
Explanation: A Container “owns” its elements: the lifetime of an element stored in a container cannot exceed that of the Container itself.
advertisement

8. 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.         multimap<char, int> mymultimap;
  7.         mymultimap.insert(make_pair('x', 100));
  8.         mymultimap.insert(make_pair('y', 200));
  9.         mymultimap.insert(make_pair('y', 350));
  10.         mymultimap.insert(make_pair('z', 500));
  11.         cout << mymultimap.size() << '\n';
  12.         return 0;
  13.     }

a) 1
b) 2
c) 4
d) 3
View Answer

Answer: c
Explanation: In this program, We are counting the number of elements in the map.
Output:

advertisement
$ g++ alc.cpp
$ a.out
4

9. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <queue>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         priority_queue<int> mypq;
  7.         mypq.push(10);
  8.         mypq.push(20);
  9.         mypq.push(15);
  10.         cout  << mypq.top() << endl;
  11.         return 0;
  12.     }

a) 15
b) 20
c) 10
d) Error
View Answer

Answer: b
Explanation: In this program, We used the queue template and the top method is used to retain the last but before element.
Output:

$ g++ alc1.cpp
$ a.out
20

10. 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.         multimap<char, int> mymultimap;
  7.         mymultimap.insert(make_pair('y', 202));
  8.         mymultimap.insert(make_pair('y', 252));
  9.         pair<char, int> highest = *mymultimap.rbegin();
  10.         multimap<char, int> :: iterator it = mymultimap.begin();
  11.         do 
  12.         {
  13.             cout << (*it).first << " => " << (*it).second << '\n';
  14.         } while ( mymultimap.value_comp()(*it++, highest) );
  15.         return 0;
  16.     }

a) y => 202
b) y => 252
c) y => 202 & y => 252
d) y => 205
View Answer

Answer: a
Explanation: In this program, the method rbegin is used to return the first element in the map.
Output:

$ g++ alc2.cpp
$ a.out
y = &gt; 202

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.