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