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)