Skip to content
Official PYQ · 1993

GATE CS 1993 Q7.4 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

What does the following code do?

var a, b: integer;
begin
    a:=a+b;
    b:=a-b;
    a:=a-b;
end;
Choose one option
View worked solution

Answer: A

Let the initial values of the variables aa and bb be a0a_0 and b0b_0 respectively.

We can trace the execution of the statements step-by-step:

  1. a := a + b;
    The new value of aa becomes:a=a0+b0a = a_0 + b_0
  2. b := a - b;
    Using the updated value of aa:b=(a0+b0)−b0=a0b = (a_0 + b_0) - b_0 = a_0
  3. a := a - b;
    Using the updated values of aa and bb:a=(a0+b0)−a0=b0a = (a_0 + b_0) - a_0 = b_0

After the execution of these three statements, the final value of aa is b0b_0 and the final value of bb is a0a_0. Thus, the values of aa and bb are exchanged (swapped) without using a temporary variable.

Therefore, the correct option is A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions