LearnFrenzy
Sign in Take a free test

Decision Making · Find the Error · Question 4

Bug: a branch that can never run Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

Students with 90 or more should get a distinction. What does the program print for 95 marks, and why?

main.c
#include <stdio.h>

int main(void) {
    int marks = 95;
    if (marks >= 40)
        puts("pass");
    else if (marks >= 90)
        puts("distinction");
    else
        puts("fail");
    return 0;
}
  • A distinction
  • B pass – every mark ≥ 90 is already ≥ 40, so the first branch wins and the distinction branch is unreachable Correct
  • C fail
  • D pass and distinction
Correct Answer: Option B

In a ladder only the first true branch runs, so the more specific test must come first: check >= 90 before >= 40.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links