Skip to content
GATEiit

GATE DA 2025 Q47: Python sets in a while loop: which set ends up with "this"?

Question

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

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 2/3 mark in GATE.
Show answer and explanation

Answer: B (official GATE 2025 final answer key)

Concept used: Set difference and union, and simultaneous tuple assignment in Python.

First step: Trace the first pass: A−B = {"this"}, B−C = {"that"}, C−A = {"other"}. All three right-hand sides use the old values, because tuple assignment evaluates them first.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2025 DA question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key