Question Detail

COS 102 6 lecture note; fundamentals of programming Objective Question

Question

What is the output of the following Python code snippet?
s1 = {10,20,30}
s2 = {50,20,"10"}
s3 = s1.union(s2)
print(s3)
Options
A 16
B Error
C 1
D {'10', 50, 20, 10, 30}
Correct Answer
Correct Answer

Option D is the correct answer.

Detailed Explanation

The union() method for sets returns a new set containing all items from both sets. Since sets do not allow duplicate elements, the number 20 appears only once. The string '10' is distinct from the integer 10. The output should be a set containing all unique elements from s1 and s2: {10, 20, 30, 50, "10"}. The order of elements in a set is not guaranteed, so any permutation of these elements is valid.

Hint

Remember that sets store unique elements and can contain mixed data types. The union() method combines unique elements from both sets.

Question Info