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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: There are three main iterators needed for designing a container. They are const iterator, Reverse iterator and Iterator traits.
6. What is the combined role of the allocator interface in a user-defined container?
a) Storage management
b) Memory management
c) Storage & Memory management
d) Iterator management
View Answer
Explanation: The allocator interface in a user-defined container is responsible for both storage and memory management. It provides methods for allocating and deallocating memory dynamically while also ensuring efficient storage handling within the container. This dual role optimizes performance and resource utilization in C++ container implementations.
7. How many types of container classes are there in c++?
a) 1
b) 2
c) 3
d) As many as possible
View Answer
Explanation: 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
View Answer
Explanation: 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 <iostream>
#include <string>
#include <algorithm>
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
View Answer
Explanation: 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?
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int square(int i) { return i * i; }
int main()
{
vector<int> V, V2;
V.push_back(0);
V.push_back(1);
V.push_back(2);
transform(V.begin(), V.end(), back_inserter(V2), square);
copy(V2.begin(), V2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
a) 0
b) 1
c) 2
d) 0 1 4
View Answer
Explanation: In this program, We formed an algorithm to find the square of the given number.
Output:
$ g++ dan1.cpp $ a.out 0 1 4
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.
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs
- Apply for C++ Internship
- Check Programming Books