ProgrammingHardTCS NQT Advanced · Capgemini
What will be the output of the following pseudocode? (&& stops as soon as the left side is false)
What will be the output of the following pseudocode? (&& stops as soon as the left side is false)
Integer c = 0, k = 0
Function f(Integer x)
c = c + 1
return (x mod 3 == 0)
End Function
for (each i from 1 to 10)
if (f(i) && f(i + 1))
k = k + 1
end if
end for
Print k, c- A. 0 13
- B. 0 20
- C. 3 13
- D. 0 10
Correct answer: A. 0 13
&& short-circuits: f(i + 1) is called only when f(i) is true, i.e. for i = 3, 6, 9, and then 4, 7, 10 are not multiples of 3. So k stays 0 and c = 10 + 3, giving 0 13.
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? (All variables are integers, so / is integer division) Procedure show(Integer n) if…
- What will be the output of the following pseudocode? (| is bitwise OR) Integer x = 0, c = 0 while (x < 1000) x = x | (x + 1) c = c + 1 end…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer arr[7] = {1, 2, 3, 4, 5, 6, 7} reverse(arr, 0, 2)…
- What will be the output of the following pseudocode? (the static s keeps its value between calls; in f(n - 1) + s the call is evaluated…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 6) for (each j from 1 to 6) if ((i j) mod 4 == 0)…
- What will be the output of the following pseudocode? (Strings are indexed from 0; + joins strings) String s = "AAABCCDAA", r = "" Integer…