Java MCQ

Previous Java Multiple Choice Questions Next

Java Tricky If and Switch Questions

1

What is the output?
int x = 5;
if (x = 10) {
  System.out.println("x is 10");
} else {
  System.out.println("x is not 10");
}

Correct Answer: C) Compilation Error

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.

2

What will be printed?
int x = 0;
if (x = 0) {
  System.out.println("Zero");
} else {
  System.out.println("Not zero");
}

Correct Answer: C) Compilation Error

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.

3

What is the output?
boolean b1 = true;
boolean b2 = false;
if (b1 = b2) {
  System.out.println("Equal");
} else {
  System.out.println("Not equal");
}

Correct Answer: B) 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.

4

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");
}

Correct Answer: B) Two Three Default

Without break statements, switch cases "fall through". Execution starts at case 2 and continues through all subsequent cases including default.

5

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");
}

Correct Answer: B) Weekday Weekend

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.

6

What will be printed?
int x = 10;
if (x > 5)
  System.out.println("A");
  System.out.println("B");
else
  System.out.println("C");

Correct Answer: C) Compilation Error

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.

7

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");
}

Correct Answer: C) Compilation Error

Duplicate case labels are not allowed in switch statements. Having two cases with value 1 causes a compilation error.

8

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");
}

Correct Answer: C) NullPointerException

When using wrapper classes in switch statements, if the variable is null, it will throw NullPointerException at runtime when trying to unbox the value.

9

What is the output?
int x = 10;
if (x == 10); {
  System.out.println("x is 10");
}

Correct Answer: A) 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".

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");
}

Correct Answer: B) Apple Orange

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.

Previous Java Multiple Choice Questions Next