DEV Community

Cover image for πŸš€ Day 21 of My Automation Journey – If / Else Tricky Questions
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 21 of My Automation Journey – If / Else Tricky Questions

Today I explored some tricky and confusing scenarios in Java if / else statements.

At first, if-else looks simple… but small mistakes in syntax, conditions, or indentation can completely change the output 😲

So I practiced 12 tricky questions to understand how Java actually executes them.

πŸ”Ή What is if Statement?

βœ” Executes code only when condition is true

πŸ” Scenario 1

int x = 5;

if (x > 2)
    System.out.println("Hello");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
5 > 2 β†’ true

πŸ“€ Output

Hello
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 2

int x = 5;

if (x < 2)
    System.out.println("Hello");

System.out.println("Hi");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

5 < 2 β†’ false β†’ if block skipped

But the second statement is outside if, so it executes.

πŸ“€ Output

Hi
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 3

if(true)
{
    System.out.println("Hello");
}
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

Condition is always true

πŸ“€ Output

Hello
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 4

if(false)
{
    System.out.println("Hello");
}
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

Condition is always false

πŸ“€ Output

(No Output)
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 5

int x = 5;

if (x > 2)
    System.out.println("Hello");
else
    System.out.println("Hi");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
5 > 2 β†’ true
πŸ“€ Output

Hello
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 6 (Important ⚠️)

int x = 10;

if (x = 20) {
    System.out.println("True");
}
Enter fullscreen mode Exit fullscreen mode

❌ Explanation
x = 20 β†’ Assignment, NOT comparison
Java expects boolean condition, but x = 20 returns int.

πŸ“€ Result

Compile Time Error
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Correct way:

if (x == 20)
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 7 (Dangling Else Problem)

int x = 5;

if (x > 2)
    if (x < 10)
        System.out.println("A");
    else
        System.out.println("B");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
if (x > 2) β†’ true
if (x < 10) β†’ true

πŸ‘‰ else always attaches to the nearest if

πŸ“€ Output

A
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 8

int a = 0;

if (a)
    System.out.println("Yes");
else
    System.out.println("No");
Enter fullscreen mode Exit fullscreen mode

❌ Explanation

`Java does NOT allow:

if(a)

Condition must be boolean`

πŸ“€ Result

Compile Time Error
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 9 (Very Important πŸ”₯)

int x = 0;

if (x++ == 0 && ++x == 2)
    System.out.println("True");

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

βœ… Explanation

`Step by step:

x = 0

x++ == 0 β†’ true (x becomes 1)
++x == 2 β†’ x becomes 2 β†’ true

Both conditions true β†’ print "True"`

πŸ“€ Output

True
2
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 10 (Short Circuit OR)

int x = 10;

if (x == 10 || x++ == 11)
    System.out.println(x);
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
`x == 10 β†’ true

πŸ‘‰ OR (||) stops execution if first condition is true
πŸ‘‰ x++ is NOT executed`

πŸ“€ Output

10
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 11 (Common Mistake ⚠️)

int x = 5;

if (x > 2)
    System.out.println("A");
    System.out.println("B");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
Only first line is part of if
Second line always executes

πŸ“€ Output

A
B
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 12 (Nested if)

int x = 10, y = 5;

if (x > y)
    if (y > 0)
        if (x > 20)
            System.out.println("A");
        else
            System.out.println("B");
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation
x > y β†’ true
y > 0 β†’ true
x > 20 β†’ false

So else executes.

πŸ“€ Output

B
Enter fullscreen mode Exit fullscreen mode

🧠 Key Takeaways

βœ” if works only with boolean conditions
βœ” = is assignment, == is comparison
βœ” else always binds to nearest if
βœ” Without {}, only one statement belongs to if
βœ” Short-circuit operators (&&, ||) can skip execution
βœ” Increment operators (++) change values during evaluation

🏁 Conclusion

Today’s session helped me understand how small syntax differences can completely change program behaviour.

These tricky questions are very useful for:

βœ” Java interviews
βœ” Writing correct automation logic
βœ” Debugging real-world code

Step by step, I’m becoming more confident in Java fundamentals for automation testing πŸš€

πŸ€– 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)