ProgrammingEasyTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
for (each i from 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. 1
- C. 9
- D. 8
Correct answer: C. 9
There are 6 multiples of 3 and 4 multiples of 5 up to 20, but 15 is in both: 6 + 4 − 1 = 9. Forgetting the overlap gives 10, reading 'or' as 'and' counts only 15 (1), and stopping the loop at 19 drops 20 (8).
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.) Integer n = 4521, r = 0 while…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 4) for (each j from i to 4) c = c + 1 end for end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function fun(Integer n) if (n…
- What will be the output of the following pseudocode? Integer a = 48, b = 18, t while (b != 0) t = b b = a mod b a = t end while Print a