GATE DA 2025 | Question 47

MCQ · 2 marks · −0.66

Consider the following Python code snippet.

A={"this","that"}
B={"that","other"}
C={"other","this"}
while "other" in C:
    if "this" in A:
        A,B,C=A-B,B-C,C-A
    if "that" in B:
        A,B,C=C|A,A|B,B|C

When the above program is executed, at the end, which of the following sets contains "this"?

  1. Option A: Only A
  2. Option B: Only B (correct)
  3. Option C: Only C
  4. Option D: A, C

Answer

(B)

Explanation

Each tuple assignment evaluates its whole right-hand side with the old sets before assigning anything.

  • Pass 1, first if: A={"this"}, B={"that"}, C={"other"}.
  • Pass 1, second if ("that" is in B): A={"other","this"}, B={"this","that"}, C={"that","other"}.
  • Pass 2: "other" is still in C and "this" is in A, so A={"other"}, B={"this"}, C={"that"}. Now "that" is not in B, so the second if is skipped.

"other" is no longer in C, so the loop stops. Only B contains "this".

Answer: B