IAE Academy

GATE 2025 DA Q.47: solution and answer

GATE 2025 · DAQ.47 · 2 marks · MCQ

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. (A)

    Only A

  2. (B)

    Only B

  3. (C)

    Only C

  4. (D)

    A, C

See the question in the paper

Answer and solution

ANSWER

Only B contains "this" at the end, so the answer is (B).

Official answer: BSource: Official answer key, GATE 2025 DA (IIT Roorkee)

  1. Start: , , . In A,B,C = X,Y,Z the whole right side is evaluated first using the old values, then all three names are rebound together.

  2. Pass 1, first if: "this" is in A, so the right side is A-B, B-C, C-A. Now , , .

  3. Pass 1, second if: "that" is in B, so the right side is C|A, A|B, B|C. Now , , .

  4. Loop test: "other" is in C, so pass 2 runs. First if: "this" is in A, so A-B, B-C, C-A. Now , , .

  5. Pass 2, second if: "that" is not in B (which is ), so nothing changes.

  6. Loop test: "other" is not in C (which is ), so the loop ends.

  7. Final sets: , , . Only B contains "this", which is option (B).

KEY IDEA

In a tuple assignment, every expression on the right is computed from the old values before any name is rebound. Trace the three sets pass by pass and re-test each if and the while condition against the current sets.

WATCH OUT FOR

Forgetting that the second if is tested against the already-updated B. After pass 2's first if, B is {this}, so "that" is no longer in it and the second assignment is skipped. Another slip is updating the names one at a time instead of all together.

From the GATE 2025 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions