C++ Programming Questions and Answers – Defining a New Container

This section on C++ language interview questions and answers focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
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

Answer: b
Explanation: Iterators are used to increase the generality of an algorithm. Otherwise, we need to define the algorithm for each types.
advertisement

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

Answer: d
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

Answer: c
Explanation: There are three main iterators needed for designing a container. They are const iterator, Reverse iterator and Iterator traits.
Free 30-Day C Certification Bootcamp is Live. Join Now!

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

Answer: c
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

Answer: b
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

Answer: a
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?

advertisement
  1.     #include <iostream>
  2.     #include <string>
  3.     #include <algorithm>
  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
View Answer

Answer: d
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?

  1.     #include <vector> 
  2.     #include <algorithm>
  3.     #include <iostream>
  4.     #include <iterator>
  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
View Answer

Answer: d
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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.