250+ TOP MCQs on Defining a New Container and Answers

This section on C++ language interview questions and answers on “Defining a New Container”. 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++ language interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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::iterator and the begin/end methods for that container, e.g., begin() and end().

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?

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main() 
  6.     {
  7.         string s = "spaces in text";
  8.         s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
  9.         cout << s << endl;
  10.     }

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

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

  1.     #include  
  2.     #include 
  3.     #include 
  4.     #include 
  5.     using namespace std;
  6.     int square(int i) { return i * i; }
  7.     int main()
  8.     {
  9.         vector<int> V, V2;
  10.         V.push_back(0);
  11.         V.push_back(1);
  12.         V.push_back(2);
  13.         transform(V.begin(), V.end(), back_inserter(V2), square);
  14.         copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
  15.         cout << endl;
  16.     }

a) 0
b) 1
c) 2
d) 0 1 4
Answer: d
Clarification: In this program, We formed an algorithm to find the square of the given number.
Output:

$ g++ dan1.cpp
$ a.out
0 1 4

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top