ProgrammingModerateSSC CGL Tier 2 · IBPS PO Mains

Array a = [3, 8, 7, 2, 7]. A linear scan returns the 0-based index of the first occurrence of 7. What is printed?

Array a = [3, 8, 7, 2, 7]. A linear scan returns the 0-based index of the first occurrence of 7. What is printed?
for i = 0 to n - 1
    if a[i] == 7 then print i; stop
end for
  1. A. 4
  2. B. 1
  3. C. 3
  4. D. 2

Correct answer: D. 2

The first 7 sits at position 2 counting from 0, which is the third element. Scanning from the end finds index 4, finding the first value above 7 gives index 1, and reporting a 1-based position gives 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic