Challenge - 5 Problems
Frozen Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput of frozen set union operation
What is the output of this Python code?
Python
a = frozenset([1, 2, 3]) b = frozenset([3, 4, 5]) result = a.union(b) print(result)
Attempts:
2 left
๐ก Hint
Remember that union returns a set or frozen set depending on the caller.
โ Incorrect
The union method of a frozen set returns a new frozen set containing all unique elements from both sets.
โ Predict Output
intermediateFrozen set mutability test
What happens when you try to add an element to a frozen set?
Python
fs = frozenset([1, 2, 3]) fs.add(4)
Attempts:
2 left
๐ก Hint
Frozen sets do not support methods that modify them.
โ Incorrect
Frozen sets are immutable and do not have an add method, so calling add raises AttributeError.
โ Predict Output
advancedFrozen set as dictionary key behavior
What is the output of this code?
Python
fs1 = frozenset([1, 2]) fs2 = frozenset([2, 1]) d = {fs1: 'value1'} print(d[fs2])
Attempts:
2 left
๐ก Hint
Frozen sets with the same elements are equal and hash the same.
โ Incorrect
Frozen sets are hashable and two frozen sets with the same elements are equal, so fs2 can be used to access the dictionary key fs1.
โ Predict Output
advancedFrozen set intersection with mutable set
What is the output of this code?
Python
fs = frozenset([1, 2, 3]) s = {2, 3, 4} result = fs.intersection(s) print(result)
Attempts:
2 left
๐ก Hint
The intersection method returns the same type as the caller.
โ Incorrect
The intersection of a frozen set and a set returns a frozen set containing elements common to both.
๐ง Conceptual
expertWhy are frozen sets hashable but sets are not?
Which statement best explains why frozen sets can be used as dictionary keys but sets cannot?
Attempts:
2 left
๐ก Hint
Think about what makes an object usable as a dictionary key.
โ Incorrect
Only immutable objects with a fixed hash value can be dictionary keys. Frozen sets are immutable, so their hash is stable. Sets can change, so they are unhashable.
