Consider the following program segment to swap two variables a and b using a third variable. The statements are jumbled. Arrange them in the correct order to perform the swap successfully:
void swap(int a, int b) { a = b; // (1) b = t; // (2) int t = 0; // (3) t = a; // (4) }
We are asked to swap two variables a and b using a third variable t. Let's carefully analyze the correct sequence.
The standard swapping logic using a temporary variable t is:
Declare t → int t = 0;
Store a in t → t = a;
Assign b to a → a = b;
Assign t to b → b = t;
So, the correct order of statements:
int t = 0; → t = a; → a = b; → b = t;
Looking at the labels given in the question:
a = b; → (1)
b = t; → (2)
int t = 0; → (3)
t = a; → (4)
Mapping to our logic: 3 → 4 → 1 → 2 ✅
Answer: (b) (3) (4) (1) (2)