Introduction to Problem Solving · Class 11 · CBSE · Science Computer Science

Consider the following pseudocode for finding the largest of three numbers: read a, b, c; largest = a; if b > largest then largest = b; if c > larges…

Practice question from Introduction to Problem Solving (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · MCQ · Q-2C91EC

Consider the following pseudocode for finding the largest of three numbers: read a, b, c; largest = a; if b > largest then largest = b; if c > largest then largest = c; display largest. If the second if statement is replaced by an else-if connected to the first if statement, what problem can occur?

  1. A.The value of a can never be used as the initial largest value
  2. B.The algorithm will always display the smallest number
  3. C.The comparison with c may be skipped even when c is the largest numberCorrect answer
  4. D.The algorithm will become an infinite loop

Solution

The comparison with c must be performed independently after updating largest with b. Using else-if can skip that comparison when b is greater than a, even if c is greater than both.

More from Introduction to Problem Solving

All of Introduction to Problem Solving

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed