ProgrammingEasyAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
for (i = 1 to 20)
if (i mod 3 == 0 or i mod 5 == 0)
c = c + 1
end if
end for
Print c- A. 10
- B. 9
- C. 6
- D. 4
Correct answer: B. 9
Multiples of 3 up to 20: 3, 6, 9, 12, 15, 18 (6). Multiples of 5: 5, 10, 15, 20 (4). 15 is in both and is counted once by 'or': 6 + 4 − 1 = 9. Adding without removing the overlap gives 10.
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? (/ is integer division.) Integer n = 2405, c = 0, s = 0 while (n > 0) s = s + n mod…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 4) for (j = 1 to 4) if (j > i) break end if c = c + j end…
- What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift; precedence as in C: << binds…
- What will be the output of the following pseudocode? Function f(Integer n) if (n <= 1) return 1 end if return f(n - 1) + 2 f(n - 2) End…
- What will be the output of the following pseudocode? (Strings are indexed from 0.) String s = "PSEUDOCODE" Integer v = 0 for (i = 0 to…
- What will be the output of the following pseudocode? Integer x = 1 for (k = 1 to 10) x = (x 3) mod 7 end for Print x