Which branch fires — Pseudocode Aptitude Question
Which branch fires: What value of grade is printed? Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 45 seconds
Question
What value of grade is printed?
Integer marks, grade
Set marks = 72
If marks >= 90 Then
Set grade = 1
Else If marks >= 75 Then
Set grade = 2
Else If marks >= 60 Then
Set grade = 3
Else
Set grade = 4
End If
Print gradeOptions
- 1
- 2
- 3
- 4
Show the answer and the worked solution
Correct answer: C. 3
Worked solution
- marks >= 90 is false, since 72 is below 90.
- marks >= 75 is false, since 72 is below 75.
- marks >= 60 is true, so grade = 3 and the rest of the chain is skipped.
Answer: 3.
The idea
An if / else-if ladder is exclusive: the first true condition wins and no later test is even evaluated. Reading the conditions out of order is the usual source of error.
Shortcut. Place 72 in its band: it sits in 60–74, the third band from the top, so grade 3 is printed.
Common trap. 4 tempts a candidate who sees 72 fail both upper tests and jumps straight to the final Else, overlooking the marks >= 60 rung that catches it first.