ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Array a = [4, 1, 7, 3, 2, 8] (three queries are not needed here). The procedure below counts the elements that are strictly greater than the element before them (a rise), for positions 2 to n. What is printed?

Array a = [4, 1, 7, 3, 2, 8] (three queries are not needed here). The procedure below counts the elements that are strictly greater than the element before them (a rise), for positions 2 to n. 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. 3
  2. B. 0
  3. C. 5
  4. D. 2

Correct answer: D. 2

The rises are 1 to 7 and 2 to 8, so the count is 2. Counting the falls gives 3, counting equal neighbours gives 0 and the number of adjacent pairs is 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic