If statements
Switch Statements
Comparison Operators
Logical Operators
Pitfalls and Best Practices
100

What keyword is used to define a basic conditional statement in Java?

if

100

What keyword is used to define a multi-way branch statement in Java?

switch

100

What operator checks for equality between two values?

==

100

What operator represents logical AND in Java?

&&

100

What makes a switch statement less suitable than an if statement for checking conditions with logical operators?

switch statements only compare a single variable against discrete values, not logical expressions.

200

What is the output of:

if (5 > 3) { 

   System.out.println("Yes"); 

}

The condition 5 > 3 is true, so 'Yes' is printed.

200

What keyword is used to exit the switch block?

break

200

What is used when comparing Strings?

.equals()

200

What is the result of:
 true || false

true


200

What is used when a program needs to check for multiple conditions, especially when conditions depend on each other hierarchically?

Nested if-else

300

What is the purpose of the 'else' keyword?

 'else' executes when the 'if' condition is false.

300

What happens if a 'break' statement is omitted in a switch case?

Omitting 'break' causes fall-through.

300

What operator checks if two values are not equal?

!=

300

What is the output of: 

if (!(5 > 3 && 2 < 4)) { 

   System.out.println("No"); 

} else {

   System.out.println("Yes"); 

}

Yes

300

Why is it bad to use a single '=' in an if condition?

'=' is an assignment operator.
Use '==' for comparison, not '='.

400

What is wrong with this code: if (x = 5) { }?

Use '==' for comparison, not '='.

400

What is the purpose of the 'default' case in a switch statement?

'default' handles unmatched values.

400

What is the output of: 

if (10 < 20) { 

      System.out.println("Less"); 

} else { 

       System.out.println("More");

}

The condition 10 < 20 is true, so 'Less' is printed.

400

What does this evaluate to: 

(5 > 3 && 2 < 1) ?

False because the AND fails because 2 < 1 is false.

400

What is the issue with this code: 

if (x > 5); { 

   System.out.println("Error"); 

}

The semicolon makes the if condition irrelevant.

500

What does this code print:
int x = 10; 

if (x > 5) {
     System.out.println("A");
} else if (x > 8) {
      System.out.println("B");
} else {
       System.out.println("C");
}

The first true condition ('x > 5') prints 'A' and exits. 

500

What does this switch print: 

int x = 2; 

switch (x) { 

case 1: 

    System.out.println("One"); 

    break; 

case 2: 

     System.out.println("Two"); 

case 3: 

     System.out.println("Three"); 

     break; 

}

Case 2 lacks a break, so it prints 'Two' and 'Three'

500

What is the result of: 

(3 > 5) == (2 > 4) ?

Both conditions are false, so the equality check returns true.

500

What is the output of: 

if (!(true && false) || (3 > 2)) {         

   System.out.println("Yes"); 

} else {

   System.out.println("No"); 

}

The expression evaluates to true, printing 'Yes'.

500

What is wrong with this code: 

int x;

if (x > 0) {

   System.out.println("Positive");

}

x is not initialized