ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode (searching a 2D array, stopping both loops once found)? (Arrays are indexed from 0.)

What will be the output of the following pseudocode (searching a 2D array, stopping both loops once found)? (Arrays are indexed from 0.)
Integer arr[3][2] = {{1, 2}, {3, 4}, {5, 6}}
Integer target = 4
Boolean found = false
Integer locRow = -1, locCol = -1
for (each i from 0 to 2)
    if (found)
        break
    end if
    for (each j from 0 to 1)
        if (arr[i][j] == target)
            locRow = i
            locCol = j
            found = true
            break
        end if
    end for
end for
Print locRow, locCol
  1. A. 0 1
  2. B. 1 0
  3. C. 1 1
  4. D. 2 0

Correct answer: C. 1 1

4 is located at row 1, column 1 of the matrix; the inner break stops scanning that row, and the found flag checked at the top of the outer loop prevents any further rows from being scanned.

More Pseudocode and Code Tracing questions

Exams that ask this topic