C++ Containers Interview Preparation Guide
Download PDF

C++ Containers frequently Asked Questions by expert members with experience in C++ Containers. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts

19 C++ Containers Questions and Answers:

Table of Contents

C++ Containers Interview Questions and Answers
C++ Containers Interview Questions and Answers

1 :: What is Iterator class?

An iterator class is used to iterate through objects of the container class. An iterator is an entity that gives access to the contents of a container object.

2 :: What is Input Iterator?

These iterators can read values in the forward movement. They can be incremented, compared and dereferenced.

3 :: What is Ouptut Iterator?

They write values in the forward movement. They can be incremented and dereferenced.

4 :: What is Forward Iterator?

They are like input and output iterators that can read and write values in forward movement.

5 :: What is Bidirectional iterators?

These can Read and write values with forward and backward movement and can be incremented, decremented.

6 :: What is random_access_iterator?

Can read and write values randomly.

7 :: Tell us what you know about Iterator class?

A container class hold group of objects and iterator class is used to traverse through the objects maintained by a container class. The iterator class provides access to the classes inside a container. They are objects that point to other objects. Iterator points to one element in a range, and then it is possible to increment it so that it points to the next element.

There are several different types of iterators:

input_iterator
output_iterator
forward_iterator
bidirectional_iterator
random_iterator
reverse_iterator

8 :: What are storage classes?

Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program.

9 :: What is Automatic variable?

Automatic variable, also called as local variable and it has scope only within the function block where it is defined.

10 :: What are External variable?

External variable are defined outside any function and memory is set aside for this type of variable once it is declared and remained until the end of the program. These variables are also called global variables.

11 :: What are static automatic variables?

The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

12 :: What is a container class? What are the types of container classes?

A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. A generic class plays a role of generic holder. A container class is a good blend of predefined behavior and an interface that is well known. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory. A container class is known as heterogeneous container, when it contains a set of different objects. A container class is known as homogeneous container when it contains a set of similar objects.

13 :: Explain containers of pointer?

Container class is one of the classes that were put into class libraries. To handle objects that contain other objects, container classes are used. A GUI class library contains a group of container classes.

Containers of pointers provide containers to hold the objects that are heap-allocated in manner that is exception-safe and with minimum overhead. The central idea is to make OOP easier in C++. This is done by establishing a standard a set of classes, methods to deal with OO specific problems.

14 :: What are the C++ standardized container classes?

The following are the standardized container classes:

std::map: Used for handle sparse array or a sparse matrix.
std::vector: Like an array, this standard container class offers additional features such as bunds checking through the at () member function, inserting or removing elements, automatic memory management and throwing exceptions.
std::string: A better supplement for arrays of chars.

15 :: Explain different types of iterators, i.e. input_iterator, output_iterator etc?

Input Iterator: These iterators can read values in the forward movement. They can be incremented, compared and dereferenced.
Ouptut Iterator: They write values in the forward movement. They can be incremented and dereferenced.
Forward Iterator: They are like input and output iterators that can read and write values in forward movement.
Bidirectional iterators: These can Read and write values with forward and backward movement and can be incremented, decremented.
random_access_iterator: Can read and write values randomly.

16 :: Do you know what is a container class? What are the types of container classes?

A class is said to be a container class which is utilized for the purpose of holding objects in memory or persistent media. A generic class plays a role of generic holder. A container class is a good blend of predefined behavior and an interface that is well known. The purpose of container class is to hide the topology for the purpose of objects list maintenance in memory. A container class is known as heterogeneous container, when it contains a set of different objects. A container class is known as homogeneous container when it contains a set of similar objects.

17 :: Can you please explain the difference between homogeneous and a heterogeneous container?

A container is an object holding instances of another object. A container that contains or holds objects of a single type is said to be homogenous. On the other hand, A container that contains objects (derived from a common base class) of a variety of types is termed heterogeneous.

18 :: Tell me when should we use container classes instead of arrays?

It is advisable to use container classes of the STL so that you don’t have to go through the pain of writing the entire code for handling collisions, making sure its working well, testing it repeatedly with an overhead of time consumption.

Suppose you have some data that has values associated with strings and its fields consist of grades> in this situation you can directly use hash table instead of having it created by yourself.

19 :: What is associative container?

Associative containers provide lookup based on keys. They come in two variants:
• Ordered associative containers do lookup based on an ordering criterion, by default < (less
than). They are implemented as balanced binary trees, usually red-black trees.
• Unordered associative containers do lookup based on a hash function. They are implemented
as hash tables with linked overflow.
Both come as
• maps: sequences of {key,value} pairs
• sets: maps without values (or you could say that the key is also the value)
Finally, maps and sets, whether ordered or unordered, come in two variants:
• ‘‘Plain’’ sets or maps with a unique entry for each key
• ‘‘Multi’’ sets or maps for which multiple entries can exist for each key
The name of an associate container indicates its place in this 3-dimensional space: {set|map,
plain|unordered, plain|multi}. ‘‘Plain’’ is nev er spelled out, so the associative containers are:
Associative Containers (§iso.23.4.1, §iso.23.5.1)
set multiset unordered_set unordered_multiset
map multimap unordered_map unordered_multimap
Their template arguments are described in §31.4.
Internally, a map and an unordered_map are very different. See §31.2.1 for graphical representations.
In particular, map uses its comparison criterion (typically <) on a key to search through a balanced tree (an O(log(n)) operation), whereas unordered_map applies a hash function on a key to find a slot in a hash table (an O(1) operation for a good hash function).