Java MCQ
Java Tricky If and Switch Questions
What is the output?
int x = 5;
if (x = 10) {
System.out.println("x is 10");
} else {
System.out.println("x is not 10");
}
if (x = 10) {
System.out.println("x is 10");
} else {
System.out.println("x is not 10");
}
In Java, the condition in an if statement must be a boolean expression. Using assignment (=) instead of equality (==) causes a compilation error because x=10 returns an int value, not boolean.
What will be printed?
int x = 0;
if (x = 0) {
System.out.println("Zero");
} else {
System.out.println("Not zero");
}
if (x = 0) {
System.out.println("Zero");
} else {
System.out.println("Not zero");
}
This is a common mistake. The condition uses assignment (=) instead of comparison (==). In Java, if conditions require boolean expressions, and x=0 returns an int, causing compilation error.
What is the output?
boolean b1 = true;
boolean b2 = false;
if (b1 = b2) {
System.out.println("Equal");
} else {
System.out.println("Not equal");
}
boolean b2 = false;
if (b1 = b2) {
System.out.println("Equal");
} else {
System.out.println("Not equal");
}
This uses assignment (=) not comparison (==). b1 = b2 assigns false to b1 and returns false. Since the condition is false, "Not equal" is printed.
What will be printed?
int x = 2;
switch(x) {
case 1: System.out.print("One ");
case 2: System.out.print("Two ");
case 3: System.out.print("Three ");
default: System.out.print("Default");
}
switch(x) {
case 1: System.out.print("One ");
case 2: System.out.print("Two ");
case 3: System.out.print("Three ");
default: System.out.print("Default");
}
Without break statements, switch cases "fall through". Execution starts at case 2 and continues through all subsequent cases including default.
What is the output?
String day = "MONDAY";
switch(day) {
case "MONDAY":
case "TUESDAY": System.out.print("Weekday ");
case "SATURDAY": System.out.print("Weekend "); break;
default: System.out.print("Invalid");
}
switch(day) {
case "MONDAY":
case "TUESDAY": System.out.print("Weekday ");
case "SATURDAY": System.out.print("Weekend "); break;
default: System.out.print("Invalid");
}
Case "MONDAY" has no break, so it falls through to "TUESDAY" case (printing "Weekday "), then continues to "SATURDAY" case (printing "Weekend ") before hitting the break.
What will be printed?
int x = 10;
if (x > 5)
System.out.println("A");
System.out.println("B");
else
System.out.println("C");
if (x > 5)
System.out.println("A");
System.out.println("B");
else
System.out.println("C");
Without braces {}, only the first statement after if is considered part of the if block. The second print statement is outside the if, making the else statement orphaned, causing compilation error.
What is the output?
int x = 1;
switch(x) {
case 1: System.out.print("One ");
break;
case 2: System.out.print("Two ");
break;
case 1: System.out.print("Another One ");
break;
default: System.out.print("Default");
}
switch(x) {
case 1: System.out.print("One ");
break;
case 2: System.out.print("Two ");
break;
case 1: System.out.print("Another One ");
break;
default: System.out.print("Default");
}
Duplicate case labels are not allowed in switch statements. Having two cases with value 1 causes a compilation error.
What will be printed?
Integer num = null;
switch(num) {
case 1: System.out.println("One"); break;
case 2: System.out.println("Two"); break;
default: System.out.println("Default");
}
switch(num) {
case 1: System.out.println("One"); break;
case 2: System.out.println("Two"); break;
default: System.out.println("Default");
}
When using wrapper classes in switch statements, if the variable is null, it will throw NullPointerException at runtime when trying to unbox the value.
What is the output?
int x = 10;
if (x == 10); {
System.out.println("x is 10");
}
if (x == 10); {
System.out.println("x is 10");
}
The semicolon after if condition ends the if statement. The code block that follows is just a regular block that always executes, printing "x is 10".
What will be printed?
String fruit = "APPLE";
switch(fruit) {
case "APPLE": System.out.print("Apple ");
case "ORANGE": System.out.print("Orange "); break;
case "BANANA": System.out.print("Banana "); break;
default: System.out.print("Unknown");
}
switch(fruit) {
case "APPLE": System.out.print("Apple ");
case "ORANGE": System.out.print("Orange "); break;
case "BANANA": System.out.print("Banana "); break;
default: System.out.print("Unknown");
}
The "APPLE" case doesn't have a break statement, so execution falls through to the "ORANGE" case, printing both "Apple " and "Orange " before hitting the break.