Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Which container is used to store elements as key-value pair?(a) map(b) multimap(c) unordered map(d) all of the mentionedThis question was addressed to me in final exam.The doubt is from More Containers in chapter Class Hierarchies, Library & Containers of C++

Answer» CORRECT choice is (d) all of the mentioned

Easy EXPLANATION - C++ provides these three CONTAINERS(map, MULTIMAP and unordered map) to store ELEMENTS as key-value pair.
2.

In which of the following relationship objects of related classes are strongly dependent?(a) Aggregation(b) Association(c) Composition(d) Both Composition an AssociationI got this question in an online quiz.This interesting question is from Class Relationships in division Class Hierarchies, Library & Containers of C++

Answer»

Correct choice is (d) Both COMPOSITION an ASSOCIATION

To explain I would say: In both Composition and Association, objects of related classes are STRONGLY dependent. For example, BOOKS will be DESTROYED if the library does not exist (talking about objects of classes).

3.

Which of the following is corect way of constructing bitset using binary string?(a) bitset b;(b) bitset b(12);(c) bitset b(string(“1100”));(d) bitset b(float(12));This question was posed to me in an online quiz.This intriguing question comes from Bitset in chapter Class Hierarchies, Library & Containers of C++

Answer» CORRECT OPTION is (C) bitset b(string(“1100”));

Explanation: The correct way of constructing bitset using BINARY string is as follows:

bitset b(string(“1100”));
4.

What is the use of proj() function?(a) Used to calculate the argument of a complex number(b) Used to calculate the conjugate of a complex number(c) Used to calculate the negative of a complex number(d) Used to calculate the projection of a complex numberI have been asked this question by my school teacher while I was bunking the class.My doubt is from Complex Library in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct CHOICE is (d) Used to calculate the projection of a COMPLEX number

The BEST explanation: HEADER PROVIDES proj() function to calculate the projection of a complex number a + ib.

5.

Which classes can have vtable?(a) Classes having friend functions(b) Classes having virtual functions(c) Classes having static functions(d) All classes have a vtableThe question was asked in a job interview.Query is from vtable and vptr in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct CHOICE is (b) Classes having virtual FUNCTIONS

Best explanation: Classes having virtual functions only NEED VTABLE because in those cases only we need to RESOLVE function calls in a dynamic manner.

6.

Which function is used to insert an element into heap?(a) push_back()(b) push_heap()(c) pop_back()(d) pop_heap()This question was addressed to me in examination.This intriguing question originated from STL topic in division Class Hierarchies, Library & Containers of C++

Answer»

Correct choice is (b) push_heap()

The best explanation: C++ STL-heap container provides push_heap() FUNCTION that inserts a new ELEMENT to the CONSTRUCTED heap.

7.

Pick the correct statement.(a) Vectors have dynamic size whereas Array classes have a static size(b) Both vectors and Array classes have a dynamic size(c) Both vectors and Array classes have a static size(d) Vectors have static size whereas Array classes have a dynamic sizeThis question was posed to me in unit test.I need to ask this question from seq_con Vector Class in portion Class Hierarchies, Library & Containers of C++

Answer»

The correct choice is (a) Vectors have DYNAMIC SIZE whereas Array CLASSES have a static size

Explanation: Vectors are IMPLEMENTED in a way so that it can handle any number of elements at a TIME means the size of a vector can vary, whereas Array classes have fixed size.

8.

Which of the following is/are advantage(s) of Sequence Container arrays over C-like arrays?(a) Sequence Container arrays store its size within itself whereas C-like arrays do not(b) Sequence Container arrays are more efficient(c) Sequence Container arrays have no array decay problem whereas C-like arrays do have(d) All of the mentionedThis question was posed to me by my school teacher while I was bunking the class.My question comes from seq_con Array Class in portion Class Hierarchies, Library & Containers of C++

Answer» CORRECT choice is (d) All of the mentioned

Easiest EXPLANATION - Sequence Container arrays(a.k.a ARRAY CLASSES) somehow stores its SIZE and it can be implemented efficiently. Also, Array classes do not have Array decay problem.
9.

What is mandatory for designing a new container?(a) Classes(b) Iterators(c) Container(d) VariablesThis question was addressed to me in homework.My question is taken from Defining a New Container in section Class Hierarchies, Library & Containers of C++

Answer»

Right ANSWER is (b) ITERATORS

Easiest explanation - Iterators are used to INCREASE the generality of an algorithm. OTHERWISE, we need to DEFINE the algorithm for each types.

10.

Which are not full container classes in c++?(a) Sequence container(b) Associative container(c) Container adaptor(d) iterative containerThis question was posed to me in an interview.I'd like to ask this question from Almost Containers in division Class Hierarchies, Library & Containers of C++

Answer» CORRECT ANSWER is (c) Container adaptor

Easy explanation - Container ADAPTORS are not full container classes, but classes that provide a specific INTERFACE relying on an object of one of the container classes such as deque or list to HANDLE the elements.
11.

What type of access does deque and vector provide?(a) Linear access(b) Parallel access(c) Random access(d) Memory accessThe question was asked during an interview.I need to ask this question from Almost Containers topic in chapter Class Hierarchies, Library & Containers of C++

Answer» RIGHT ANSWER is (C) Random access

Explanation: Because they can manipulate the VALUES on anywhere in the program, So it is PROVIDING random access.
12.

Which symbol is used to create multiple inheritances?(a) Dot(b) Comma(c) Dollar(d) starThis question was posed to me in an international level competition.This interesting question is from Multiple Inheritance in division Class Hierarchies, Library & Containers of C++

Answer» CORRECT option is (b) COMMA

The best I can explain: For USING MULTIPLE inheritance, simply specify each base class (just like in single inheritance), SEPARATED by a comma.
13.

Which of the following is correct about the map and unordered map?(a) Ordering of keys in maps whereas no such order in the unordered map(b) Maps are implemented red-black trees whereas unordered map are implemented using hash tables(c) Average search time in the unordered map is O(1) whereas it is O(logn) in case of maps(d) All of the mentionedThis question was posed to me in a national level competition.The query is from More Containers in portion Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT choice is (d) All of the mentioned

Explanation: All the above mentioned POINTS are correct about maps and unordered maps. Maps USES red-black tree WHEREAS unordered map uses hash tables THEREFORE the average search time for the unordered map is O(1) whereas it is O(logn) in case of maps.

14.

Map is implemented using ____________________(a) binary search tree(b) red black tree(c) heap(d) hash tableThe question was asked during an interview for a job.My doubt stems from More Containers in section Class Hierarchies, Library & Containers of C++

Answer»

Correct answer is (B) RED black tree

Best explanation: The map has some order of stored keys THEREFORE red black tree is used to maintain that order and ACCESS the elements as SOON as possible.

15.

Composition is a __________ type of Association relationship.(a) strong(b) weak(c) unnecessary(d) necessaryThis question was posed to me in an internship interview.The origin of the question is Class Relationships topic in chapter Class Hierarchies, Library & Containers of C++

Answer»

Right option is (a) strong

Explanation: Composition is a strong TYPE of ASSOCIATION relationship because in this CASE, OBJECTS are strongly dependent on each other. For EXAMPLE, human and heart cannot exist without each other.

16.

How the relationship is made in Association?(a) Through the objects of classes(b) Through constructor(c) Through destructor(d) Through class NamesThis question was addressed to me during an interview.My query is from Class Relationships topic in portion Class Hierarchies, Library & Containers of C++

Answer»

The correct CHOICE is (a) Through the objects of classes

The best explanation: Association RELATIONSHIP between classes is made using the objects of classes like we have a bank object DENOTING ABC bank and some objects of Employee class XYZ1, XYZ2 and so on. So, in this case, XYZ1 is an employee in ABC bank so there is a relationship between these two objects.

17.

What happens when only one argument is supplied to set() function?(a) All bits are set to 1 in a bitset(b) Bit corresponding to an argument is set to 1(c) All alternate bits are set to 1 in a bitset(d) First bit is set to 1I got this question during an online interview.My question is taken from Bitset in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct answer is (b) Bit corresponding to an argument is SET to 1

The BEST explanation: When only one argument is supplied to set() function then bit corresponding to that INDEX is set to 1.

18.

What is bitset in C++?(a) An array of bools consuming one bit per element(b) Vector of bools(c) C-like arrays of bool elements(d) Template classI have been asked this question at a job interview.I'm obligated to ask this question of Bitset in chapter Class Hierarchies, Library & Containers of C++

Answer»

Right choice is (a) An array of BOOLS consuming one bit per ELEMENT

To explain I would say: Bitset is a COLLECTION of BOOL variables with each element consuming only one bit. They are introduced for efficient use of MEMORIES.

19.

What are Engine Adaptors?(a) Class template that adopts a pseudo-random number generator engine(b) Class template that adopts a pseudo-random number generator engine to produce numbers with a given numbers of bits(c) Random number engine that generates pseudo-random numbers(d) Mersenne Twister 19937 generator generating 32-bit true random numberThe question was posed to me in an interview for job.This is a very interesting question from Generators in section Class Hierarchies, Library & Containers of C++

Answer»

Right answer is (b) Class template that ADOPTS a pseudo-random NUMBER generator engine to produce numbers with a given numbers of bits

Easy EXPLANATION - Engine ADAPTOR is a class template that adapts a pseudo-random number generator to produce random number having a specific number of bits.

20.

Which type of heap is implemented in STL heap?(a) max heap(b) min heap(c) middle heap(d) close heapThis question was posed to me during an interview.My question is from STL in division Class Hierarchies, Library & Containers of C++

Answer» CORRECT ANSWER is (a) max heap

Easiest explanation - C++ STL-heap implements max heap i.e. the front of heap CONTAINS the maximum of all the elements in a RANGE.
21.

What is a pair?(a) Container consisting of two data elements of the same type(b) Container consisting of two data elements of different type(c) Container consisting of one header and two data elements of the same type(d) Container consisting of two data elements can have the same or different typeI have been asked this question in an online interview.Enquiry is from STL topic in portion Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT choice is (d) Container CONSISTING of TWO data elements can have the same or different type

To explain: PAIR is a container defined in STL which consist of two elements which can be of same or different types.

22.

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 managementI have been asked this question in homework.The origin of the question is Defining a New Container topic in section Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT choice is (a) Storage management

The BEST explanation: Storage management is the use of the ALLOCATOR INTERFACE in the user-defined container.

23.

Where does the vector add the item?(a) End(b) Insert(c) Middle(d) StartI got this question in unit test.Asked question is from Almost Containers in section Class Hierarchies, Library & Containers of C++

Answer»

Correct choice is (a) END

To explain I would SAY: Vector allows insertion of ELEMENT at the end.

24.

How many types of class relationships are there?(a) 1(b) 2(c) 3(d) 4I got this question during an online exam.The above asked question is from Class Relationships topic in portion Class Hierarchies, Library & Containers of C++

Answer»

The correct choice is (d) 4

The explanation: There are basically four TYPES of CLASS RELATIONSHIPS namely Inheritance, Aggregation, Composition and ASSOCIATION relationships between classes.

25.

How many ways are there for constructing a bitset?(a) 1(b) 2(c) 3(d) 4This question was posed to me by my school principal while I was bunking the class.My question is from Bitset topic in division Class Hierarchies, Library & Containers of C++

Answer» RIGHT choice is (c) 3

For EXPLANATION: There are three ways of constructing a bitset. Direct CONSTRUCTION, using integer number and using binary string.
26.

What is the use of rank() function in C++?(a) Returns size of each dimension(b) Returns how many total elements can be stored in an array(c) Returns how many elements are in array currently(d) Returns the dimension of an arrayThis question was addressed to me during an online exam.My doubt stems from Array Type Manipulation topic in portion Class Hierarchies, Library & Containers of C++

Answer»

Correct OPTION is (d) Returns the DIMENSION of an ARRAY

The best I can EXPLAIN: rank() FUNCTION returns the rank of the array i.e. the dimension of an array. For example, int arr[10][10] has rank 2.

27.

What is the default random engine?(a) Random number engine that generates pseudo-random numbers(b) Mersenne Twister 19937 generator generating 32-bit true random number(c) Random number engine that generates true random numbers(d) Mersenne Twister 19937 generator generating 32-bit pseudo random numberThe question was posed to me in homework.My question is based upon Generators in chapter Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT CHOICE is (a) RANDOM number engine that GENERATES pseudo-random numbers

Explanation: Default random engine is a random number engine that generates pseudo random numbers.

28.

In how many different ways any-container can be constructed?(a) 1(b) 2(c) 3(d) 4The question was asked by my college professor while I was bunking the class.I would like to ask this question from STL Container Any in section Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT answer is (c) 3

Explanation: There are three basic WAYS of CONSTRUCTING any variable. They are done using copy INITIALIZATION, using the CONSTRUCTOR or using an assignment operator.

29.

How the list containers are implemented?(a) Using Double linked list(b) Using Single linked list(c) Using Single & Double linked list(d) Using linear linked listI got this question in an interview.I'm obligated to ask this question of Sequences in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct answer is (a) Using Double linked LIST

The EXPLANATION: List CONTAINERS are implemented as doubly-linked lists. Doubly linked lists can store each of the elements they contain in different and unrelated STORAGE LOCATIONS.

30.

Which of the following describes the protected access specifier?(a) The variable is visible only outside inside the block(b) The variable is visible everywhere(c) The variable is visible to its block and to it’s derived class(d) The variable is not visible to its blockI have been asked this question during a job interview.My question is based upon Access Control in section Class Hierarchies, Library & Containers of C++

Answer» RIGHT choice is (c) The variable is visible to its block and to it’s derived class

Best explanation: Protected members are visible to its block and to the derived CLASSES and not visible to OUTSIDE objects or VARIABLES.
31.

Which is the correct syntax of declaring a virtual function?(a) virtual int func();(b) virtual int func(){};(c) inline virtual func();(d) inline virtual func(){};The question was posed to me in an online quiz.This intriguing question comes from Inheritance topic in portion Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT answer is (a) virtual int func();

To explain I would say: To MAKE a FUNCTION virtual function we just need to add virtual keyword at the STARTING of the function declaration.

32.

Which type of relationship is modelled by Aggregation?(a) Is-A relationship(b) Has-A relationship(c) Part-Of relationship(d) Have-A relationshipI had been asked this question in an international level competition.Query is from Class Relationships in division Class Hierarchies, Library & Containers of C++

Answer»

The correct choice is (b) Has-A relationship

The BEST explanation: Aggregation models the has-a relationship between CLASSES. In this children can exist WITHOUT a parent, THEREFORE, they have a relationship.

33.

Composition is also a type of _______________ relationship.(a) Aggregation(b) Association(c) Inheritance(d) Both Aggregation an AssociationI got this question in an internship interview.My query is from Class Relationships topic in division Class Hierarchies, Library & Containers of C++

Answer» RIGHT option is (B) Association

The explanation: COMPOSITION is also a type of Both Aggregation and Association relationship. Composition RELATIONSHIPS are string relationships whereas others are a superset of this relationship.
34.

What is the use of reset function in bitset?(a) Used to make alternate bits zero(b) Used to make a bit 0 in a bitset(c) Used to make all bits 1(d) Used to make a bit(s) 0 in a bitsetThis question was addressed to me in an online interview.My query is from Bitset in section Class Hierarchies, Library & Containers of C++

Answer»

The correct choice is (d) USED to make a bit(s) 0 in a BITSET

Easiest explanation - header provides the RESET() FUNCTION to set bit(s) to 0 in a bitset variable.

35.

What is the use of count() function in bitset?(a) To count the number of 0’s(b) To count the number of 1’s(c) To count the number of total bits in a bitset(d) To count the number of low bitsThis question was posed to me in an online interview.I'd like to ask this question from Bitset topic in section Class Hierarchies, Library & Containers of C++

Answer»

Right choice is (B) To COUNT the number of 1’s

Easiest explanation - header PROVIDES the count() function to count the number of 1’s or HIGH bits in the bitset variable.

36.

Which of the following is correct about bitset and vector of bools?(a) Space consumed by bitset is less than vector(b) Bitset consume only 1 bit per element(c) Number of elements in bitset should be known at compile time whereas vector can have a dynamic size(d) All of the mentionedI have been asked this question in quiz.The origin of the question is Bitset in section Class Hierarchies, Library & Containers of C++

Answer»

The correct option is (d) All of the mentioned

Explanation: Bitset consumes less space COMPARED to BOOL vector HOWEVER the size for bitset is static WHEREAS for bool vector size is dynamic.

37.

Which function is used to calculate the norm of a complex number?(a) abs()(b) norm()(c) mod()(d) square_sum()This question was addressed to me in final exam.This intriguing question comes from Complex Library topic in portion Class Hierarchies, Library & Containers of C++

Answer»

Right ANSWER is (b) norm()

To EXPLAIN: HEADER provides norm() function to CALCULATE the norm of a complex number.

38.

Which function is used to get the real part of the complex number?(a) img_p()(b) imag_p()(c) real()(d) real_p()I have been asked this question in an interview for internship.I would like to ask this question from Complex Library topic in division Class Hierarchies, Library & Containers of C++

Answer»

Correct option is (c) real()

Best EXPLANATION: The real() FUNCTION is PROVIDED by the complex

to ACCESS the real part of a complex number object.

39.

What is random_device?(a) A pseudo-random number generator(b) Time-dependent random number generator(c) Simple random number generator(d) A true random number generatorI got this question in an online quiz.The origin of the question is Generators topic in chapter Class Hierarchies, Library & Containers of C++

Answer»

The correct OPTION is (d) A true RANDOM NUMBER GENERATOR

Explanation: random_device is a true random number generator not the PSEUDO random number generator.

40.

What is the difference between begin() and rbegin()?(a) both are the same(b) begin() returns an iterator to the first element and rbegin() returns an iterator to an element kept at the end of the vector(c) begin() returns an iterator to first element whereas rbegin() returns constant iterator to first element(d) begin() returns returns first element rbegin() returns voidI have been asked this question during a job interview.The origin of the question is seq_con Vector Class in section Class Hierarchies, Library & Containers of C++

Answer»

Right choice is (b) BEGIN() returns an iterator to the first ELEMENT and rbegin() returns an iterator to an element kept at the end of the VECTOR

The explanation is: begin() is USED to return the iterator to the first element of the vector whereas rbegin() is used to return the an element stored at in the LAST of a vector.

41.

How many items are there in sequence container?(a) 2(b) 3(c) 4(d) 5I got this question during an interview.My enquiry is from Sequences topic in division Class Hierarchies, Library & Containers of C++

Answer»

The correct choice is (d) 5

To explain: There are FIVE items in sequence container. They are ARRAY, vector, LIST, forward_list and dequeue.

42.

How many sets of requirements are need in designing a container?(a) 1(b) 2(c) 3(d) 4This question was posed to me in a job interview.The above asked question is from Container Design in division Class Hierarchies, Library & Containers of C++

Answer»

Correct OPTION is (c) 3

Easiest explanation - There are three sets of requirements. They are CONTAINER INTERFACE requirements, Allocator interface requirements and ITERATOR requirements.

43.

Which is the correct syntax of constructing a bitset?(a) bitset b;(b) bitset b(12);(c) bitset b(string(“1100”));(d) all of the mentionedI had been asked this question in final exam.I'd like to ask this question from Bitset in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct answer is (d) all of the MENTIONED

Best explanation: All of the above mentioned are correct SYNTAX of CONSTRUCTING a BITSET. However each has DIFFERENT way of interpretation.

44.

Which of the following is the limitation of bitset over vector bool?(a) Space(b) Size(c) Type(d) SpeedThe question was asked in final exam.My question is from Bitset in section Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT option is (b) Size

Easy explanation - BITSET size is static WHEREAS vector size is DYNAMIC therefore the size of a vector can be increased or decreased which is not possible in bitset.

45.

What is the use of is_array() function in C++?(a) To check if a variable is array type or not(b) To check if a variable is 1-D array type or not(c) To check if a variable is 2-D array type or not(d) To check if a variable is 1-D or 2-D array type or notThis question was posed to me in semester exam.My question is taken from Array Type Manipulation in section Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT option is (a) To CHECK if a variable is array type or not

The BEST explanation: is_array() FUNCTION is used to check whether a given variable is of array type or not.

46.

What is mersenne_twister_engine?(a) Pseudo-random number engine that generates random unsigned integers(b) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^w-1] for some w using Mersenne Twister algorithm(c) Pseudo-random number engine that generates random unsigned integers in the range [0, 2^w-1] for some w using lagged Fibonacci generator(d) Pseudo-random number engine that generates random signed integers in the range [0, 2^w-1] for some w using Mersenne Twister algorithmThe question was asked in an international level competition.Question is taken from Generators in chapter Class Hierarchies, Library & Containers of C++

Answer»

Correct option is (b) Pseudo-random NUMBER ENGINE that generates random unsigned INTEGERS in the range [0, 2^W-1] for some w using Mersenne Twister algorithm

The BEST explanation: mersenne_twister_engine is a Pseudo-random number engine that generates random unsigned integers in the range [0, 2^w-1] for some w using Mersenne Twister algorithm.

47.

What are Distributions in C++?(a) Objects that converts a sequence into a sequence having an ascending order(b) Objects that converts a sequence into a sequence having specific random variable distribution(c) Objects that converts a sequence into a sequence having a descending order(d) Objects that converts a sequence into a sequence having only the smallest, largest and medianI got this question in quiz.Enquiry is from Generators in chapter Class Hierarchies, Library & Containers of C++

Answer»

Right option is (b) Objects that converts a sequence into a sequence having specific RANDOM VARIABLE distribution

Easy explanation - Distributions are objects that CONVERT the sequence GENERATED by the generator into a sequence which has a specific random variable distribution like uniform, normal, binomial, etc.

48.

What is the use of sort_heap() function in heap?(a) To sort the elements in the heap into descending order(b) To sort the elements in the heap into ascending order(c) To sort the first half of the heap(d) To sort the second half of the heapThe question was asked in an online quiz.This question is from STL in portion Class Hierarchies, Library & Containers of C++

Answer»

The correct ANSWER is (b) To sort the ELEMENTS in the heap into ascending order

The explanation is: C++ STL-heap CONTAINER provides sort_heap() FUNCTION to sort the heap into ascending order which will no longer remain a heap.

49.

Which of the following header file is needed to use vectors in your program?(a) (b) (c) (d) I have been asked this question by my college professor while I was bunking the class.I want to ask this question from seq_con Vector Class in chapter Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT option is (b)

EXPLANATION: HEADER file contains all the IMPLEMENTATION of vector methods, hence we need to include this header file.

50.

What are the vectors?(a) Arrays with dynamic size(b) Arrays with different types of elements(c) Same as array classes(d) Arrays with static size but use template classesI had been asked this question in an online quiz.This interesting question is from seq_con Vector Class topic in portion Class Hierarchies, Library & Containers of C++

Answer»

The CORRECT OPTION is (a) ARRAYS with dynamic size

Best EXPLANATION: Vectors are just like arrays with the ability to resize itself whenever an ELEMENT is added to or deleted from it.