Python Collections and comprehensions Quiz
Question 1
What is the primary advantage of using list comprehension over traditional loops in Python?
List comprehension is more memory-efficient.
List comprehension is faster.
List comprehension allows complex logic.
List comprehension supports parallel processing.
Question 2
When using list comprehension, what happens if the iterable is empty?
It raises a ValueError.
It creates an empty list.
It raises a SyntaxError.
It results in an infinite loop.
Question 3
What is the main purpose of the Counter
class in the collections
module?
To store key-value pairs in order
To count the occurrences of elements in an iterable
To merge multiple dictionaries into one
To create a dictionary that prevents deletions
Question 4
Which statement is true about an OrderedDict
?
It does not maintain the order of elements
It raises a KeyError
if a missing key is accessed
It remembers the order in which keys were inserted
It allows only numerical keys
Question 5
What happens when you try to access a missing key in a defaultdict
?
It returns None
It raises a KeyError
It removes the key from the dictionary
It returns the default value set for the dictionary
Question 6
What is the purpose of the ChainMap
class in the collections
module?
It chains multiple dictionaries into a single unit
It ensures all keys in a dictionary are unique
It provides a way to count elements in a sequence
It maintains the insertion order of keys
Question 7
Output of given code is
from collections import deque
queue = deque(['name','age','DOB'])
print(queue)
['name', 'age', 'DOB']
deque(['name', 'age'])
deque['name', 'age', 'DOB']
deque(['name', 'age', 'DOB'])
Question 8
Deque in Python is implemented using the module __________
collections
collect
dequeue
collection
Question 9
What is the purpose of the following list comprehension?
words = ['python', 'list', 'comprehension']
unique_starting_letters = {word[0].upper() for word in words}
Creates a list of unique starting letters of words.
Creates a set of unique starting letters of words.
Creates a dictionary with starting letters as keys and words as values.
Counts the occurrences of each starting letter.
Question 10
Explain the term "nested list comprehension."
It refers to using multiple conditions in a single list comprehension.
It involves using a list comprehension inside another list comprehension.
It is a type of list comprehension specific to nested lists.
It is an advanced feature not related to list comprehensions.
There are 10 questions to complete.