Pseudo-code · Question 15 · Hard

What will be the output of the following pseudocode? (Note: array indices start at 0 and for-loop bounds are inclusive.)

Integer arr[5] = {3, 1, 4, 1, 5}
Integer i, s
Set s = 0
for (i = 0 to 4)
    if (arr[i] > arr[(i + 1) mod 5])
        s = s + arr[i]
    else
        s = s - arr[i]
    end if
end for
Print s
  1. 4
  2. 0
  3. 14
  4. 10
Answer and explanation

Answer: D. 10

Explanation: Each element is compared with the next one, wrapping around at the end. 3 > 1 adds 3; 1 > 4 fails, subtract 1; 4 > 1 adds 4; 1 > 5 fails, subtract 1; 5 > 3 (wrapped to arr[0]) adds 5. Total 3 - 1 + 4 - 1 + 5 = 10.

Previous: What will be the output of the following pseudocode? (Note: for-loop bounds… · Next: What will be the output of the following pseudocode? (Note: / denotes integer…

Practise pseudo-code

Try a fresh set of pseudo-code questions with an explanation after every answer, free and without signing up: Pseudo-code practice. Or sit a full placement mock test.

More pseudo-code questions

More technical & programming practice

C Programming · Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge