The GATE Grind

GATE 2025 DA – Question 47

Programming, Data Structures and Algorithms · Programming in Python · 2 marks · Multiple choice

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. Only A
  2. Only B
  3. Only C
  4. A, C

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".