LearnFrenzy
Sign in Take a free test

Methods and Recursion · Find the Error · Question 1

Error: missing return statement Answer, explanation & discussion

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

The program below does not compile. Which statement correctly describes the problem?

Main.java
public class Main {
    static String grade(int marks) {
        if (marks >= 60) return "pass";
        else if (marks >= 0) return "fail";
    }

    public static void main(String[] args) {
        System.out.println(grade(70));
    }
}
  • A grade must be void
  • B When marks is negative no return is reached; every path of a non-void method must return a value (add a final return or throw) Correct
  • C else if is not allowed
  • D marks must be final
Correct Answer: Option B

The compiler does not reason about values, only about paths.

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