ProgrammingModerateTCS NQT · Infosys

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0
for (each i from 1 to 5)
    if (a[i] > a[i - 1] AND a[i] > a[i + 1])
        c = c + 1
    end if
end for
Print c
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: A. 3

Interior elements larger than both neighbours: 3 (between 1 and 2), 4 (between 2 and 1) and 5 (between 1 and 2). The end elements are never checked. c = 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic