Q
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) }
Question Info
Choose the Best Option
Click any option to instantly check if you're correct.
Explanation
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
aint→t = a; -
Assign
btoa→a = b; -
Assign
ttob→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)
Share This Question
Challenge a friend or share with your study group.