ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0, n = 5
for (each i from n down to 1)
for (each j from 1 to i)
if (j mod 2 == 0)
c = c + 1
end if
end for
end for
Print c- A. 5
- B. 6
- C. 8
- D. 10
Correct answer: B. 6
Counting even j values for each i: i=5 gives 2 (j=2,4), i=4 gives 2, i=3 gives 1, i=2 gives 1, i=1 gives 0. Total 2+2+1+1+0 = 6.
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? (Arrays are indexed from 0.) Integer arr[6] = {10, 20, 30, 40, 50, 60} Integer…
- What will be the output of the following pseudocode? Integer total = 0 for (each i from 1 to 3) for (each j from 1 to 3) if (i <= j) total…
- What will be the output of the following pseudocode (maximum product of any two elements)? (Arrays are indexed from 0.) Integer arr[8] =…
- What will be the output of the following pseudocode (counting words longer than the average word length)? List words = {"the", "quick"…
- What will be the output of the following pseudocode (grouping anagrams together and counting the groups)? List words = {"eat", "tea"…
- What will be the output of the following pseudocode (a do-while-style loop that always runs at least once)? Integer x = 10, c = 0 repeat x…