Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 22 STL Containers


Section 22.2 STL Basics
22.1  Three components in STL are ____________.
A. containers
B. iterators
C. algorithms
D. linked lists
E. vectors

22.2  Three types of containers are ____________.
A. sequence containers
B. linear containers
C. associative containers
D. container adapters
E. wrapper containers

22.3  Which of the following are sequence containers?
A. list
B. queue
C. deque
D. map
E. set

22.4  Which of the following are associative containers?
A. list
B. queue
C. deque
D. map
E. set

22.5  Which of the following are container adapters?
A. list
B. queue
C. deque
D. multimap
E. stack

22.6  Which of the following are the common features for all containers?
A. Each container has a no-arg constructor.
B. Each container has a copy constructor.
C. Each container has the empty() function to check whether a container is empty.
D. Each container has the size() function to return the number of elements in the container.
E. Each container supports the relational operators (<, <=, >, >=, ==, and !=).

22.7  Which of the following are first-class containers?
A. list
B. queue
C. deque
D. multimap
E. stack

22.8  Which of the following containers use iterators?
A. list
B. queue
C. deque
D. multimap
E. stack

22.9  Which of the following are the common features for all first-class containers?
A. Each first-class container has the swap function.
B. Each first-class container has the max_size() function.
C. Each first-class container has the clear() function.
D. Each first-class container has the erase function.
E. Each first-class container has the add function.

Section 22.3 Iterators
22.10  To obtain an iterator for the first element in a container c, use ____________.
A. c.first()
B. c.head()
C. c.begin()
D. c.rbegin()

22.11  To obtain an iterator that points to the next element after the last element in a container, use _____.
A. c.last()
B. c.tail()
C. c.end()
D. c.rend()

22.12  To declare an iterator for a vector v of the int type, use __________.
A. vector::iterator p
B. iterator p
C. vector<int>::iterator p
D. vector<>::iterator p

22.13  Types of iterators are _________.
A. input iterators
B. output iterators
C. forward iterators
D. bidirectional iterators
E. random access iterators

22.14  ________ supports random access iterators.
A. vector
B. deque
C. list
D. set
E. map

22.15  ________ supports bidirectional iterators.
A. vector
B. deque
C. list
D. set
E. map

Section 22.4 Sequence Containers
22.16  A ______ is efficient if the elements are appended to the end, but it is expensive to insert or delete elements anywhere except at the end.
A. vector
B. deque
C. list

22.17  It is efficient for insertion at both front and end of a _____, but it is still expensive to insert or delete elements in the middle of it.
A. vector
B. deque
C. list

22.18  A ________ is good for applications that require frequent insertion and deletion in the middle of it.
A. vector
B. deque
C. list

22.19  Which of the following are common functions in sequence containers?
A. assign(n, element)
B. push_back(element)
C. pop_back()
D. insert(position, element)

22.20  To insert an element to a vector v, use ________.
A. v.assign(0, element)
B. v.insert(0, element)
C. v.push_back(element)
D. v.add(element)

22.21  To remove the last element from a vector v, use ________.
A. v.removeAt(v.size() - 1)
B. v.remove()
C. v.pop_back(element)
D. v.pop_back()

22.22  To remove a specified element from a list v, use ________.
A. v.removeAt(0)
B. v.remove(element)
C. v.pop_back(element)
D. v.pop_back()

22.23  You can apply the sort function on ____________.
A. vector
B. deque
C. list

22.24  You can apply the reverse function on ____________.
A. vector
B. deque
C. list

Section 22.5 Associative Containers
22.25  A ________ can have duplicate elements.
A. set
B. multiset
C. map
D. multimap

22.26  __________ are defined in the <set> header file.
A. set
B. multiset
C. map
D. multimap

22.27  All the elements in ________ are sorted on keys.
A. set
B. multiset
C. map
D. multimap

22.28  To insert an element to a set s, use ________.
A. s.push_back(element)
B. s.insert(element)
C. s.add(element)
D. s.insert(0, element)

22.29  To find a specified element in a set s, use _________.
A. s.search(element)
B. s.find(element)
C. search(s, element)
D. find(s, element)

22.30  To create a map with int key type and string value, use _______.
A. map<string, int> map1
B. map<int, string> map1
C. map<int> map1
D. map<string> map1

22.31  To insert a int key and string value to map1, use _________.
A. map1.insert(100, "John Smith");
B. map1.insert(map<int, string>::value_type("John Smith", 100));
C. map1.insert(map<int, string>::value_type(100, "John Smith"));
D. map1.insert("John Smith", 100);

Section 22.6 Container Adapters
22.32  Which of the following functions are in the stack class.
A. pop()
B. top()
C. push(element)
D. size()
E. empty()

22.33  Which of the following functions are in the deque class.
A. pop()
B. top()
C. push(element)
D. size()
E. empty()

22.34  Which of the following functions are in the priority_queue class.
A. pop()
B. top()
C. push(element)
D. size()
E. empty()