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
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
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
Explanation: It is node-based because it allows for efficient insertion anywhere in the program.
4. What type of access does deque and vector provide?
a) Linear access
b) Parallel access
c) Random access
d) Memory access
View Answer
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
Explanation: Vector allows insertion of element at the end.
6. Which are not full container classes in c++?
a) Sequence container
b) Associative container
c) Container adaptor
d) iterative container
View Answer
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
Explanation: A Container “owns” its elements: the lifetime of an element stored in a container cannot exceed that of the Container itself.
8. What will be the output of the following C++ code?
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char, int> mymultimap;
mymultimap.insert(make_pair('x', 100));
mymultimap.insert(make_pair('y', 200));
mymultimap.insert(make_pair('y', 350));
mymultimap.insert(make_pair('z', 500));
cout << mymultimap.size() << '\n';
return 0;
}
a) 1
b) 2
c) 4
d) 3
View Answer
Explanation: In this program, We are counting the number of elements in the map.
Output:
$ g++ alc.cpp $ a.out 4
9. What will be the output of the following C++ code?
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
priority_queue<int> mypq;
mypq.push(10);
mypq.push(20);
mypq.push(15);
cout << mypq.top() << endl;
return 0;
}
a) 15
b) 20
c) 10
d) Error
View Answer
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?
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char, int> mymultimap;
mymultimap.insert(make_pair('y', 202));
mymultimap.insert(make_pair('y', 252));
pair<char, int> highest = *mymultimap.rbegin();
multimap<char, int> :: iterator it = mymultimap.begin();
do
{
cout << (*it).first << " => " << (*it).second << '\n';
} while ( mymultimap.value_comp()(*it++, highest) );
return 0;
}
a) y => 202
b) y => 252
c) y => 202 & y => 252
d) y => 205
View Answer
Explanation: In this program, the method rbegin is used to return the first element in the map.
Output:
$ g++ alc2.cpp $ a.out y = > 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.
- Check Programming Books
- Check C++ Books
- Practice Programming MCQs
- Practice Computer Science MCQs
- Apply for C++ Internship