ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a - b, b - a, len(a | b), a & b == {4, 3})
  1. A. {1, 2} {5} 5 True
  2. B. {1, 2, 5} {5} 5 True
  3. C. {1, 2} {5} 7 True
  4. D. {1, 2} {5} 5 False

Correct answer: A. {1, 2} {5} 5 True

a - b keeps elements of a not in b: {1, 2}; b - a is {5}. The union {1, 2, 3, 4, 5} has 5 elements. a & b is {3, 4}, and sets compare by contents regardless of order, so == {4, 3} is True. {1, 2, 5} would be the symmetric difference a ^ b.

More Python Programming questions

Exams that ask this topic