Question & Answer

Which of the following code segments can be used to interchange the values of the variables num1 and num2?

Which of the following code segments can be used to interchange the values of the variables num1 and num2?
A
num1 ↔ num2
num2 ↔ num1

B
temp ↔ num1
num1 ↔ temp
num2 ↔ num1

C
temp ↔ num1
num2 ↔ num1
num1 ↔ temp

D
temp ↔ num1
num1 ↔ num2
num2 ↔ temp

1 Answer

Verified archive
Quizzma TeamJanuary 5, 2025

The correct answer is D:


temp ← num1

num1 ← num2

num2 ← temp

Explanation: This code segment uses a temporary variable (`temp`) to temporarily hold the value of `num1`. It then assigns the value of `num2` to `num1`, and finally assigns the value stored in `temp` (original value of `num1`) to `num2`. This effectively interchanges their values without losing any information. If you have more questions or need clarification, feel free to ask!