LearnFrenzy
Sign in Take a free test

Methods and Recursion · Find the Error · Question 9

Error: code after return Answer, explanation & discussion

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

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

Main.java
public class Main {
    static int discount(int price) {
        return price / 10;
        System.out.println("discount computed");
    }

    public static void main(String[] args) {
        System.out.println(discount(500));
    }
}
  • A price must be double
  • B The println comes after return in the same block, so it is unreachable – javac rejects it Correct
  • C Methods cannot print
  • D return needs parentheses
Correct Answer: Option B

Move the println before the return.

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