GATE 2025 DA – Question 47
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|CWhen the above program is executed, at the end, which of the following sets contains "this"?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (B) Only B
Explanation
Trace the loop. At the start $A = \{this, that\}$, $B = \{that, other\}$ and $C = \{other, this\}$. Round 1: since "this" is in A, the first assignment gives $A = A - B = \{this\}$, $B = B - C = \{that\}$ and $C = C - A = \{other\}$. Now "that" is in B, so the second assignment gives $A = C \cup A = \{other, this\}$, $B = A \cup B = \{this, that\}$ and $C = B \cup C = \{that, other\}$. "other" is still in C, so round 2 runs. "this" is in A, so $A = \{other\}$, $B = \{this\}$ and $C = \{that\}$. "that" is not in B, and "other" is not in C, so the loop ends. Only B contains "this".