ProgrammingHardAmazon OA · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer m[3][3] = {{5, 1, 7}, {2, 9, 3}, {8, 4, 6}}
Integer c = 0
Boolean peak
for (each i from 0 to 2)
for (each j from 0 to 2)
peak = true
for each (di, dj) in {(-1,0), (1,0), (0,-1), (0,1)}
if (cell (i+di, j+dj) is inside the grid AND m[i+di][j+dj] >= m[i][j])
peak = false
end if
end for
if (peak)
c = c + 1
end if
end for
end for
Print c- A. 5
- B. 1
- C. 4
- D. 3
Correct answer: A. 5
A cell counts if it is larger than each of its up/down/left/right neighbours. The centre 9 qualifies, and so do all four corners: 5 (neighbours 1, 2), 7 (1, 3), 8 (2, 4) and 6 (3, 4). c = 5; it is not just the single largest value.
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? String s = "CODE IS FUN", r = "", w = "" for (each i from 0 to length(s) - 1) if…
- What will be the output of the following pseudocode? (substring(t, i, k) is the k characters starting at index i; indices start at 0)…
- What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift) Integer a = 13, b = 11, carry, rounds = 0…
- What will be the output of the following pseudocode? ((b & 1) is evaluated before == ; & is AND, << and >> are shifts) Integer a = 13, b =…
- What will be the output of the following pseudocode? (^ is XOR, >> is right shift) Integer s = 0, g for (each i from 0 to 7) g = i ^ (i >>…
- What will be the output of the following pseudocode? Function possible(Integer out[5]) Stack st = empty Integer next = 1 for (each i from…