Multiple choice general knowledge

Swap the value of two variable in one statement without using any temporary variable

  1. a=a+b; b=a-b; a=a-b;

  2. c=a+b; b=a-b; c=a-b;

  3. c=a-b; b=a+b; c=a-b;

  4. a=a+b; b=a-b; c=a-b;

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The classic arithmetic swap without a temporary variable uses three statements: a=a+b; b=a-b; a=a-b. This works because after the first line, a holds the sum; then b gets the difference (original a), and finally a gets the difference (original b).