ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer arr[6] = {10, 20, 30, 40, 50, 60}
Integer evenSum = 0, oddSum = 0
for (each i from 0 to 5)
if (i mod 2 == 0)
evenSum = evenSum + arr[i]
else
oddSum = oddSum + arr[i]
end if
end for
Print evenSum, oddSum, evenSum - oddSum- A. 90 120 -30
- B. 120 90 30
- C. 100 110 -10
- D. 90 120 30
Correct answer: A. 90 120 -30
Even-indexed elements (indices 0,2,4) are 10+30+50=90. Odd-indexed elements (indices 1,3,5) are 20+40+60=120. The difference evenSum-oddSum = 90-120 = -30.
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 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…
- What will be the output of the following pseudocode (searching a 2D array, stopping both loops once found)? (Arrays are indexed from 0.)…