Question Detail

COS 102 6 lecture note; fundamentals of programming Objective Question

Question

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

Option D is the correct answer.

Detailed Explanation

The union() method for sets returns a new set containing all unique elements from both sets. The string '10' is distinct from the integer 10. Thus, the union will include all five unique elements: 10, 20, 30, 50, and '10'.

Hint

Recall how the union() method works for Python sets, especially when different data types represent similar values (e.g., int 10 vs. string '10').

Question Info