What will be the result of the following program?

public class SwitchTest {
    public static void main(String[] args) {
        byte k = 0;
        
        switch(k) {
            default:
                int h = 3;
                System.out.println(h);
                break;
            case 0:
                int h = 7;
                System.out.println(h);
            case 1:
                int h = 11;
                System.out.println(h);
        }
    }  
}
Explanation
There will be a compile-time error "h is already defined".
The local variables declared in the block switch have a common area of visibility.
To avoid compilation errors you can use a separate block { } for each case / default.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes