Question 1
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
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!