Challenge - 5 Problems
Master of Union and Intersection
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this union operation?
Consider two sets
A and B. What will be printed after the union operation?Python
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)Attempts:
2 left
๐ก Hint
Union combines all unique elements from both sets.
โ Incorrect
The union operator
| returns a set with all unique elements from both sets. Here, elements 1, 2, 3, 4, and 5 appear in either A or B.โ Predict Output
intermediate2:00remaining
What is the output of this intersection operation?
Given two sets
X and Y, what will be printed after the intersection operation?Python
X = {10, 20, 30, 40}
Y = {30, 40, 50, 60}
print(X & Y)Attempts:
2 left
๐ก Hint
Intersection returns only elements common to both sets.
โ Incorrect
The intersection operator
& returns a set with elements present in both sets. Here, 30 and 40 are common.๐ง Conceptual
advanced2:00remaining
Which option shows the correct way to find the union of two sets using a method?
You want to find the union of sets
set1 and set2 using a set method. Which option is correct?Attempts:
2 left
๐ก Hint
Union method returns a new set with all unique elements from both sets.
โ Incorrect
The
union() method returns a new set with all elements from both sets. add() adds a single element, intersection() finds common elements, and update() modifies the set in place.โ Predict Output
advanced2:00remaining
What is the output of this combined union and intersection code?
Given sets
a and b, what will be printed?Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
result = (a | b) & a
print(result)Attempts:
2 left
๐ก Hint
First union all elements, then intersect with set a.
โ Incorrect
The union
a | b is {1, 2, 3, 4, 5, 6}. Intersecting with a keeps only elements in a, so result is {1, 2, 3, 4}.๐ง Debug
expert2:00remaining
What error does this code raise?
Examine the code below. What error will it raise when run?
Python
set_a = {1, 2, 3}
set_b = [3, 4, 5]
result = set_a | set_b
print(result)Attempts:
2 left
๐ก Hint
The union operator requires both operands to be sets.
โ Incorrect
The union operator
| cannot be used between a set and a list. This causes a TypeError.