ProgrammingEasyAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer s = 0
for (i = 1 to 5)
for (j = i to 5)
s = s + 1
end for
end for
Print s- A. 25
- B. 15
- C. 10
- D. 20
Correct answer: B. 15
The inner loop runs 5, 4, 3, 2, 1 times: 15 = 5·6/2. Running j from 1 to 5 every time would give 25; starting j at i + 1 would give 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? Integer n = 1234, r = 0 while (n > 0) r = r 10 + (n mod 10) n = n / 10 // integer…
- What will be the output of the following pseudocode? Integer x = 5, y = 9 x = x ^ y // ^ is bitwise XOR y = x ^ y x = x ^ y Print x, y, (5…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n <= 0) return 0 return (n mod 10) + f(n / 10) //…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1 to 100) if ((i mod 3 == 0) OR (i mod 5 == 0)) if (NOT (i mod…
- What will be the output of the following pseudocode? Integer a[] = {5, 3, 8, 1, 9, 2} Integer m1 = -1, m2 = -1 for (each x in a) if (x >…
- What will be the output of the following pseudocode? Integer Function fun(Integer a, Integer b) if (b == 0) return 1 if (b mod 2 == 0)…