0
0
Pythonprogramming~20 mins

Frozen set behavior in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Frozen Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output 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)
Afrozenset({1, 2, 3, 4, 5})
B{1, 2, 3, 4, 5}
Cfrozenset([1, 2, 3, 4, 5])
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Remember that union returns a set or frozen set depending on the caller.
โ“ Predict Output
intermediate
2:00remaining
Frozen 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)
Afrozenset({1, 2, 3, 4})
BTypeError
CAttributeError
DNo output, element added silently
Attempts:
2 left
๐Ÿ’ก Hint
Frozen sets do not support methods that modify them.
โ“ Predict Output
advanced
2:00remaining
Frozen 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])
ANone
BKeyError
CTypeError
Dvalue1
Attempts:
2 left
๐Ÿ’ก Hint
Frozen sets with the same elements are equal and hash the same.
โ“ Predict Output
advanced
2:00remaining
Frozen 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)
Afrozenset({2, 3})
B{2, 3}
CTypeError
Dfrozenset({4})
Attempts:
2 left
๐Ÿ’ก Hint
The intersection method returns the same type as the caller.
๐Ÿง  Conceptual
expert
2:00remaining
Why are frozen sets hashable but sets are not?
Which statement best explains why frozen sets can be used as dictionary keys but sets cannot?
ASets are mutable but frozen sets are implemented in C, making them hashable.
BFrozen sets are immutable, so their hash value does not change, while sets are mutable and unhashable.
CFrozen sets store elements in sorted order, sets do not.
DFrozen sets use less memory than sets, making them hashable.
Attempts:
2 left
๐Ÿ’ก Hint
Think about what makes an object usable as a dictionary key.