WorksheetsPython Sets and Dictionaries Worksheet
Total questions: 20
Worksheet time: 10mins
Which of the following is a characteristic of sets in Python?
Ordered collection
Allows duplicates
Mutable and supports mathematical set operations
Supports indexing and slicing
Why do sets in Python not support indexing?
They are mutable
They maintain order
They are unordered and use hash-based storage
They allow duplicates
How can you create a set from the string 'spam'?
set = 'spam'
set('spam')
{'spam'}
list('spam')
What happens when you try to add a duplicate element to a set?
It raises an error
The set remains unchanged
The duplicate replaces the existing one
The set becomes a list
Which elements can be added to a set?
Lists
Dictionaries
Tuples
Sets
What does the union operation on sets do?
Finds common elements
Combines all unique elements
Finds elements in one but not the other
Checks if one is subset of another
What is the difference between remove() and discard() in sets?
remove() does not raise error if element not present, discard() does
discard() does not raise error if element not present, remove() does
Both raise errors
No difference
What does pop() method do in a set?
Removes the first element
Removes and returns an arbitrary element
Removes the last element
Clears the set
How do you check if two sets have no elements in common?
issubset()
issuperset()
isdisjoint()
intersection()
What is the output of {x ** 2 for x in [1, 2, 3, 4]}?
{1, 4, 9, 16}
[1, 4, 9, 16]
(1, 4, 9, 16)
Error
Which of the following is true about dictionary keys in Python?
Can be mutable like lists
Must be unique and immutable
Can be duplicates
Maintain no order
How do you access a value in a dictionary?
dict.value(key)
dict[key]
dict.get(value)
dict.items(key)
What does dict.items() return?
List of keys
List of values
List of key-value tuples
The dictionary itself
How can you add a new key-value pair to a dictionary?
dict.add(key, value)
dict[key] = value
dict.insert(key: value)
dict.append({key: value})
What happens if you assign a value to an existing key in a dictionary?
Raises error
Adds a duplicate key
Overwrites the previous value
Ignores the assignment
Which method removes a specific key and returns its value?
del dict[key]
dict.pop(key)
dict.remove(key)
dict.discard(key)
What does dict.fromkeys(keys, value) do?
Creates a dictionary with keys and default value
Copies a dictionary
Updates values
Removes keys
How do you merge two dictionaries using update()?
dict1 | dict2
dict1.update(dict2)
dict1 + dict2
dict1.merge(dict2)
What is a frozenset in Python?
Mutable set
Immutable set that is hashable
Ordered set
Set that allows duplicates
In the problem of finding failures in subjects, which set operation is used to find total unique failures?
Intersection
Difference
Union
Symmetric difference
