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"?
B
Step-by-Step Solution
Insight: This is a set loop tracing question. The critical rule is that in simultaneous assignment, all right-hand side expressions are evaluated using the old values before any assignment occurs.
Exam route: Trace the sets A, B, and C iteration by iteration. Evaluate the right-hand sides of the assignments using the current state, then update the sets, and finally recheck the loop condition.
Learning route:
Initial state: A={"this","that"}, B={"that","other"}, C={"other","this"}
Iteration 1:
- Loop condition: "other" in C is True.
- First if-block: "this" in A is True.
Evaluate RHS: A-B={"this"}, B-C={"that"}, C-A={"other"}.
Assign: A={"this"}, B={"that"}, C={"other"}.
- Second if-block: "that" in B is True.
Evaluate RHS: C|A={"other","this"}, A|B={"this","that"}, B|C={"that","other"}.
Assign: A={"other","this"}, B={"this","that"}, C={"that","other"}.
Iteration 2:
- Loop condition: "other" in C is True.
- First if-block: "this" in A is True.
Evaluate RHS: A-B={"other"}, B-C={"this"}, C-A={"that"}.
Assign: A={"other"}, B={"this"}, C={"that"}.
- Second if-block: "that" in B is False. Skip.
Iteration 3:
- Loop condition: "other" in C is False (C is {"that"}). Loop terminates.
Final state: A={"other"}, B={"this"}, C={"that"}.
The element "this" is present only in set B.