Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line contains the error?

public class Sample71
{
    public static void main(String[] args)
    {
        int grade = 75;
        switch (grade / 10)
        {
            case 7:
                assert grade >= 70 && grade < 80;
            case 6:
                System.out.println("pass");
                break;
            default:
                System.out.println("other");
        }
    }
}

  • A.int grade = 75;
  • B.Every line above is correct as written.
  • C.case 6:
  • D.assert grade >= 70 && grade < 80;

Answer: C

The case 7 branch has no break, so control falls through into case 6 and prints pass by accident rather than by design. The assertion above it is true and passes. Fall-through is legal and occasionally intended, but here it makes the 70s share a branch with the 60s silently.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.