What keyword is used to define a basic conditional statement in Java?
if
What keyword is used to define a multi-way branch statement in Java?
switch
What operator checks for equality between two values?
==
What operator represents logical AND in Java?
&&
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.
What is the output of:
if (5 > 3) {
System.out.println("Yes");
}
The condition 5 > 3 is true, so 'Yes' is printed.
What keyword is used to exit the switch block?
break
What is used when comparing Strings?
.equals()
What is the result of:
true || false
true
What is used when a program needs to check for multiple conditions, especially when conditions depend on each other hierarchically?
Nested if-else
What is the purpose of the 'else' keyword?
'else' executes when the 'if' condition is false.
What happens if a 'break' statement is omitted in a switch case?
Omitting 'break' causes fall-through.
What operator checks if two values are not equal?
!=
What is the output of:
if (!(5 > 3 && 2 < 4)) {
System.out.println("No");
} else {
System.out.println("Yes");
}
Yes
Why is it bad to use a single '=' in an if condition?
'=' is an assignment operator.
Use '==' for comparison, not '='.
What is wrong with this code: if (x = 5) { }?
Use '==' for comparison, not '='.
What is the purpose of the 'default' case in a switch statement?
'default' handles unmatched values.
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.
What does this evaluate to:
(5 > 3 && 2 < 1) ?
False because the AND fails because 2 < 1 is false.
What is the issue with this code:
if (x > 5); {
System.out.println("Error");
}
The semicolon makes the if condition irrelevant.
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.
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'
What is the result of:
(3 > 5) == (2 > 4) ?
Both conditions are false, so the equality check returns true.
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'.
What is wrong with this code:
int x;
if (x > 0) {
System.out.println("Positive");
}
x is not initialized