StreakPeaked· Practice

ExamsGATETechnical

Let $A$ be a square matrix of size $n \times n$. Consider the following pseudocode. What is the expected output? $C = 100$ for $i = 1$ to $n$ do for $j = 1$ to $n$ do { Temp = $A[i][j] + C$; $A[i][j] = A[j][i]$; $A[j][i] = Temp - C$; } for $i = 1$ to $n$ do for $j = 1$ to $n$ do output($A[i][j]$);

  1. The matrix $A$ itself
  2. Transpose of the matrix $A$
  3. Adding 100 to the upper diagonal elements and subtracting 100 from lower diagonal elements of $A$
  4. None of the above

Correct answer: The matrix $A$ itself

Solution

The code swaps $A[i][j]$ and $A[j][i]$ using a temporary variable, while the added constant $C$ is canceled out in the assignment to $A[j][i]$. Since the loops run over all pairs, each off-diagonal pair is effectively swapped twice, restoring the original matrix. Therefore the final output is the matrix itself.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →