ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Array a = [3, 1, 4, 1, 5, 9, 2, 6] is scanned from left to right. The procedure below counts the positions i (from 2 to n) where a[i] is smaller than a[i-1]. What is printed?

Array a = [3, 1, 4, 1, 5, 9, 2, 6] is scanned from left to right. The procedure below counts the positions i (from 2 to n) where a[i] is smaller than a[i-1]. What is printed?
c = 0
for i = 2 to n
    if a[i] < a[i-1] then c = c + 1
end for
Print c
  1. A. 4
  2. B. 0
  3. C. 2
  4. D. 3

Correct answer: D. 3

The falls are 3 to 1, 4 to 1, 9 to 2, so there are 3 falls. Counting the rises gives 4, counting equal neighbours gives 0, and skipping the comparison at index 1 leaves only 2 falls.

More Pseudocode and Code Tracing questions

Exams that ask this topic