DEV Community

Cover image for πŸš€ Day 23 of My Automation Journey – Switch Case Tricky Questions πŸ”₯
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 23 of My Automation Journey – Switch Case Tricky Questions πŸ”₯

Today I explored advanced and tricky scenarios in Java Switch Case.

At first glance, switch-case looks simple… but when you remove break, use default, or mix data types β€” things get very tricky 😲

This is also a favorite topic in interviews.

πŸ”Ή Before Java 8 – Traditional Switch

int x = 2;

switch(x) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    case 3:
        System.out.println("Three");
        break;
    default:
        System.out.println("Default");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Two
Enter fullscreen mode Exit fullscreen mode

βœ” break stops execution
βœ” Only matching case executes

πŸ”Ή After Java 8+ (Arrow Syntax)

int x = 2;

switch(x) {
    case 1 -> System.out.println("Hi");
    case 2 -> System.out.println("Welcome");
    default -> System.out.println("Hello");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Welcome
Enter fullscreen mode Exit fullscreen mode

βœ” No need for break
βœ” No fall-through

πŸ”Ή Switch Expression (Java 12+)

int x = 2;

int result = switch(x) {
    case 1 -> 10;
    case 2 -> 20;
    default -> 0;
};

System.out.println(result);
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

20
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Java 17+ Pattern Matching

Object obj = "Hello";

switch(obj) {
    case String s -> System.out.println(s.length());
    case Integer i -> System.out.println(i * 2);
    default -> System.out.println("Other");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

5
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ Tricky Questions
πŸ” 1. Missing break (Fall-through)

int x = 2;

switch(x) {
    case 1:
        System.out.println("One");
    case 2:
        System.out.println("Two");
    case 3:
        System.out.println("Three");
    default:
        System.out.println("Default");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Two
Three
Enter fullscreen mode Exit fullscreen mode

Default
βœ… Explanation

βœ” Starts from matching case β†’ case 2
βœ” No break β†’ continues executing all below

πŸ” 2. char vs ASCII

char ch = 'A';

switch(ch) {
    case 65:
        System.out.println("ASCII 65");
        break;
    case 'A':
        System.out.println("Character A");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

ASCII 65
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” 'A' = 65 internally
βœ” First matching case executes

πŸ” 3. Expression inside switch

int x = 10;

switch(x / 2) {
    case 5:
        System.out.println("Five");
        break;
    case 10:
        System.out.println("Ten");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Five
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” x/2 = 5 β†’ matches case 5

πŸ” 4. Multiple case labels

int x = 2;

switch(x) {
    case 1:
    case 2:
        System.out.println("One or Two");
        break;
    case 3:
        System.out.println("Three");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

One or Two
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Multiple cases share same block

πŸ” 5. Missing break again

int x = 2;

switch(x) {
    case 1:
    case 2:
        System.out.println("One or Two");
    case 3:
        System.out.println("Three");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

One or Two
Three
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Fall-through again due to missing break

πŸ” 6. Fall-through chain

int x = 3;

switch(x) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
    case 3:
        System.out.println("Three");
    case 4:
        System.out.println("Four");
        break;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Three
Four
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Starts at case 3
βœ” No break β†’ continues

πŸ” 7. Default in middle

int x = 2;

switch(x) {
    default:
        System.out.println("Default");
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Two
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Matching case runs directly
βœ” Default is ignored

πŸ” 8. Default fall-through

int x = 5;

switch(x) {
    case 1:
        System.out.println("One");
    default:
        System.out.println("Default");
    case 5:
        System.out.println("Five");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Default
Five
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” No match β†’ default executes
βœ” Then continues to case 5

πŸ” 9. final variable in case

final int a = 1;
int b = 2;

switch(b) {
    case a:
        System.out.println("A");
        break;
    case a + 1:
        System.out.println("A+1");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

A+1
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” final makes it constant β†’ allowed in case

πŸ” 10. String fall-through

String s = "Java";

switch(s) {
    case "Java":
        System.out.println("Java");
    case "JAVA":
        System.out.println("JAVA");
        break;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Java
JAVA
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Missing break β†’ fall-through

πŸ” 11. null in switch

String s = null;

switch(s) {
    case "Hi":
        System.out.println("Hi");
        break;
    default:
        System.out.println("Default");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Result

NullPointerException
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” switch does NOT accept null

πŸ” 12. Duplicate case

int x = 1;

switch(x) {
    case 1:
        System.out.println("One");
        break;
    case 1:
        System.out.println("Duplicate");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Result

Compile Time Error
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Duplicate case not allowed

πŸ” 13. Duplicate constant

final int a = 2;

switch(2) {
    case a:
        System.out.println("A");
        break;
    case 2:
        System.out.println("Two");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Result

Compile Time Error
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Both represent same value β†’ duplicate

πŸ” 14. Nested switch

int x = 1;

switch(x) {
    case 1:
        switch(x + 1) {
            case 2:
                System.out.println("Inner Two");
        }
    case 2:
        System.out.println("Outer Two");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Inner Two
Outer Two
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Inner switch executes
βœ” No break β†’ outer continues

🧠 Key Takeaways

βœ” Missing break = fall-through
βœ” default can be anywhere
βœ” final variables allowed in case
βœ” Duplicate case = compile error
βœ” null not allowed
βœ” Arrow syntax avoids fall-through

🏁 Conclusion

Today I learned how small mistakes in switch-case can lead to unexpected outputs.

These tricky questions are extremely helpful for:

βœ” Java interviews
βœ” Debugging code
βœ” Writing clean logic in automation

Step by step, I’m strengthening my Java fundamentals for Selenium Automation πŸš€

πŸ€– A Small Note

I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)