GATE DA 2025 · Programming in Python

MCQ · 2 marks · Problem PROG-PYBS-0005

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

Solve it on GATE DA practice →More Python Programming problems

Show the worked solutionTrace the sets step by step. Initially \(A=\{\text{this},\text{that}\}\), \(B=\{\text{that},\text{other}\}\), \(C=\{\text{other},\text{this}\}\).Iteration 1 ("other" \(\in C\)? Yes):"this" \(\in A\)? Yes \(\Rightarrow A,B,C=A-B,\,B-C,\,C-A=\{\text{this}\},\{\text{that}\},\{\text{other}\}\)"that" \(\in B\) (now \(\{\text{that}\}\))? Yes \(\Rightarrow A,B,C=C\cup A,\,A\cup B,\,B\cup C=\{\text{other},\text{this}\},\{\text{this},\text{that}\},\{\text{that},\text{other}\}\)Iteration 2 ("other" \(\in C\)? Yes, \(C=\{\text{that},\text{other}\}\)):"this" \(\in A\) (now \(\{\text{other},\text{this}\}\))? Yes \(\Rightarrow A,B,C=A-B,\,B-C,\,C-A=\{\text{other}\},\{\text{this}\},\{\text{that}\}\)"that" \(\in B\) (now \(\{\text{this}\}\))? No — skip.Check loop condition: "other" \(\in C\) (now \(\{\text{that}\}\))? No — loop ends.Final state: \(A=\{\text{other}\},\ B=\{\text{this}\},\ C=\{\text{that}\}\). Only \(B\) contains "this".Answer: Only BClick Here for Detail SolutionGateOverflow discussion: https://gateoverflow.in/460985/gate-da-2025-question-37

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com