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- A. 0 1
- B. 1 0
- C. 1 1
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode (finding the missing number from 1..n)? (Arrays are indexed from 0.) Integer arr[5] =…
- What will be the output of the following pseudocode (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed…
- What will be the output of the following pseudocode (summing a range with a step)? Function sumRange(Integer a, Integer b, Integer step)…
- What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)? Function factAcc(Integer n, Integer…
- What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)? Function gcdList(Array arr) Integer r =…
- What will be the output of the following pseudocode (counting permutations of a string via recursion)? Function countPerms(String s) if…