Pseudo-code · Question 23 · Medium

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

Integer arr[6] = {5, 8, 2, 9, 4, 7}
Integer i, m1, m2
Set m1 = arr[0], m2 = -1
for (i = 1 to 5)
    if (arr[i] > m1)
        m2 = m1
        m1 = arr[i]
    else if (arr[i] > m2)
        m2 = arr[i]
    end if
end for
Print m2
  1. 7
  2. 8
  3. 5
  4. 9
Answer and explanation

Answer: B. 8

Explanation: m1 tracks the largest value and m2 the second largest. After the scan m1 is 9 and m2 is 8, so 8 is printed.

Previous: What will be the output of the following pseudocode? · Next: What will be the output of the following pseudocode?

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