Here is a listing of C++ language interview questions on “Defining a New Container” along with answers, explanations and/or solutions:
1. What do all STL containers define?
a) Iterator types
b) Begin methods
c) End methods
d) All of the mentioned
Answer: d
Clarification: All the STL containers define the iterator types for that container, e.g., iterator and const_iterator, e.g., vector
2. What do we return if we use simple array on a internal container?
a) Methods
b) Pointers
c) Objects
d) Values
Answer: b
Clarification: Pointers are legal iterators, so if your internal container is a simple C array, then all you need to do is return the pointers.
3. What is mandatory for designing a new container?
a) Classes
b) Iterators
c) Container
d) Variables
Answer: b
Clarification: Iterators are used to increase the generality of an algorithm. Otherwise, we need to define the algorithm for each types.
4. What are the design requirements for building a container from the sratch?
a) Container interface requirements
b) Allocator interface requirements
c) Iterator requirements
d) All of the mentioned
Answer: d
Clarification: These are the design specific requirements for building a container from the scratch.
5. How many iterators are needed for the defining a new container?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: There are three main iterators needed for designing a container. They are const iterator, Reverse iterator and Iterator traits.
6. What is the use of the allocator interface in the user-defined container?
a) Storage management
b) Memory management
c) Storage & Memory management
d) Iterator management
Answer: a
Clarification: Storage management is the use of the allocator interface in the user-defined container.
7. How many types of container classes are there in c++?
a) 1
b) 2
c) 3
d) As many as possible
Answer: b
Clarification: There are two type of container classes in c++. They are value containers and reference containers.
8. What is the name of the container which contains group of multiple objects?
a) Heterogeneous container
b) Homogeneous container
c) Both Homogeneous & Heterogeneous container
d) Sequence container
Answer: a
Clarification: Heterogeneous container is the name of the container which contains group of multiple objects.
9. What will be the output of the following C++ code?
-
#include -
#include -
#include -
using namespace std;
-
int main()
-
{ -
string s = "spaces in text";
-
s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
-
cout << s << endl;
-
}
a) spaces
b) spaces in
c) spaces in text
d) spacesintext
Answer: d
Clarification: In this program, We formed a algorithm to remove spaces in the string.
Output:
$ g++ dan.cpp $ a.out spacesintext
