LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 6

Error: a switch-expression block without yield Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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

Main.java
public class Main {
    public static void main(String[] args) {
        int marks = 82;
        String grade = switch (marks / 10) {
            case 10, 9 -> "A+";
            case 8 -> {
                String g = "A";
                System.out.println("computing");
            }
            default -> "B";
        };
        System.out.println(grade);
    }
}
  • A case 10, 9 is invalid
  • B A block arm of a switch expression must end by yielding a value (yield g;); this block completes without one Correct
  • C marks / 10 is not an int
  • D println is not allowed in a switch
Correct Answer: Option B

return would not work here either – it would try to return from main.

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