C++ STANDARD LIBRARY MCQs Questions Answers

Q#1: Which header file is required to use the vector container in C++?
(A) <list>
(B) <vector>
(C) <array>
(D) <deque>
Answer: (B) <vector>

Q#2: The std::map in C++ stores elements as:
(A) Key-value pairs
(B) Only values
(C) Only keys
(D) Linked list
Answer: (A) Key-value pairs

Q#3: Which function is used to add an element at the end of a vector?
(A) push_front()
(B) append()
(C) push_back()
(D) insert()
Answer: (C) push_back()

Q#4: Which header file is required to use input/output streams like cin and cout?
(A) <stdio.h>
(B) <iostream>
(C) <fstream>
(D) <iomanip>
Answer: (B) <iostream>

Q#5: The Standard Template Library (STL) in C++ includes:
(A) Containers
(B) Algorithms
(C) Iterators
(D) All of the above
Answer: (D) All of the above

Q#6: Which STL container allows fast insertion and deletion at both ends?
(A) vector
(B) list
(C) deque
(D) array
Answer: (C) deque

Q#7: The std::set container stores:
(A) Duplicate elements
(B) Unique elements in sorted order
(C) Elements in insertion order
(D) Key-value pairs
Answer: (B) Unique elements in sorted order

Q#8: To sort a vector in ascending order using STL, which function is used?
(A) sort()
(B) arrange()
(C) order()
(D) organize()
Answer: (A) sort()

Q#9: Which STL container provides random access to elements and dynamic resizing?
(A) vector
(B) list
(C) set
(D) map
Answer: (A) vector

Q#10: The std::queue container follows which type of order?
(A) LIFO (Last In First Out)
(B) FIFO (First In First Out)
(C) Random
(D) Sorted
Answer: (B) FIFO (First In First Out)

Q#11: The std::stack container in STL is implemented using:
(A) Queue
(B) Vector or deque
(C) List only
(D) Array only
Answer: (B) Vector or deque

Q#12: Which header file is required to use the string class?
(A) <cstring>
(B) <string>
(C) <iostream>
(D) <vector>
Answer: (B) <string>

Q#13: std::algorithm header provides:
(A) Mathematical functions
(B) Standard algorithms like sort, find, and copy
(C) I/O operations
(D) Memory allocation
Answer: (B) Standard algorithms like sort, find, and copy

Q#14: What type of elements does std::multiset allow?
(A) Only unique elements
(B) Duplicates allowed
(C) Only integers
(D) Only strings
Answer: (B) Duplicates allowed

Q#15: Which iterator is used to traverse a map container?
(A) vector iterator
(B) map iterator
(C) set iterator
(D) list iterator
Answer: (B) map iterator

Q#16: Which function removes the last element from a vector?
(A) pop_front()
(B) remove_back()
(C) pop_back()
(D) erase_back()
Answer: (C) pop_back()

Q#17: std::unordered_map differs from std::map in that:
(A) It stores elements in sorted order
(B) It stores elements in hash table with no order guarantee
(C) It does not allow duplicate keys
(D) It is slower than map
Answer: (B) It stores elements in hash table with no order guarantee

Q#18: Which STL container is most efficient for frequent insertion/deletion in the middle?
(A) vector
(B) list
(C) deque
(D) array
Answer: (B) list

Q#19: std::pair is used to store:
(A) Two values of same type
(B) Two values of possibly different types
(C) Key-value pair only
(D) Multiple elements
Answer: (B) Two values of possibly different types

Q#20: Which of the following is NOT part of STL?
(A) Container
(B) Iterator
(C) Algorithm
(D) Exception handling
Answer: (D) Exception handling

Q#21: The find() function in STL algorithms returns:
(A) Index of element
(B) Iterator pointing to the element if found, else end iterator
(C) Boolean value
(D) Element itself
Answer: (B) Iterator pointing to the element if found, else end iterator

Q#22: Which STL container is suitable for implementing a priority queue?
(A) vector
(B) queue
(C) deque
(D) priority_queue
Answer: (D) priority_queue

Q#23: std::reverse() is used to:
(A) Reverse the elements of a container
(B) Sort elements
(C) Find an element
(D) Insert element
Answer: (A) Reverse the elements of a container

Q#24: Which container automatically sorts its elements as they are inserted?
(A) vector
(B) set
(C) list
(D) deque
Answer: (B) set

Q#25: std::accumulate() function is defined in which header?
(A) <numeric>
(B) <algorithm>
(C) <cmath>
(D) <iostream>
Answer: (A) <numeric>

Leave a Reply

Contents Copyrights Reserved By T4Tutorials