ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode? (&& binds more tightly than ||, and both stop evaluating as soon as the result is known.)
What will be the output of the following pseudocode? (&& binds more tightly than ||, and both stop evaluating as soon as the result is known.)
Integer cnt = 0, hits = 0
Function chk(Integer v)
cnt = cnt + 1
return v > 2
End Function
for (each i from 1 to 5)
if (chk(i) || chk(i - 1) && chk(i + 1))
hits = hits + 1
end if
end for
Print hits, cnt- A. 3 10
- B. 3 7
- C. 3 9
- D. 2 7
Correct answer: B. 3 7
The condition is chk(i) || (chk(i − 1) && chk(i + 1)). i = 1: chk(1) false, chk(0) false, so chk(2) is skipped — 2 calls. i = 2: chk(2) false, chk(1) false — 2 calls. i = 3, 4, 5: chk(i) is true, so the right side is skipped — 1 call each. hits = 3, cnt = 2 + 2 + 3 = 7. '3 10' reads the condition left to right as (chk(i) || chk(i − 1)) && chk(i + 1).
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? Integer c = 0, i for (i = 1; i <= 50; i = i + 1) i = i 2 c = c + 1 end for Print c, i
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[8] = {1, 2, 3, 4, 6, 7, 8, 9} Integer i = 0, j…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; a 'from a down to b' loop does nothing when a < b.)…
- What will be the output of the following pseudocode? // 32-bit signed integers; overflow wraps around (two's complement) Integer f = 1 for…
- What will be the output of the following pseudocode? Integer a = 100, b = 7, steps = 0 while (a != b) if (a > b) a = a - b else b = b - a…
- What will be the output of the following pseudocode? (/ is integer division; calls counts every call, including the first.) Integer calls…