Conditional Constructs in Java

Conditional Constructs in Java

Basic if-else, Nested if-else, Ternary Operator , Switch-Case: Fall-Through Behavior, Enhanced Switch, Logical Operators, Switch with Strings, if-else with Logical NOT, Complex Conditional Logic, Nested Ternary Operator

1 / 10

What will the following code output?

int x = 10;
if (x++ > 10) {
System.out.println(“Greater”);
} else {
System.out.println(“Smaller”);
}
System.out.println(x);

2 / 10

What will the following code output?

int a = 5, b = 10;
if (a > b) {
if (a > 0) {
System.out.println(“Positive”);
} else {
System.out.println(“Negative”);
}
} else {
System.out.println(“Not Greater”);
}

3 / 10

What will the following code print?

int x = 5, y = 10;
int result = (x > y) ? x : y;
System.out.println(result);

4 / 10

What will the following code output?

int num = 2;
switch (num) {
case 1:
System.out.println(“One”);
case 2:
System.out.println(“Two”);
case 3:
System.out.println(“Three”);
default:
System.out.println(“Default”);
}

5 / 10

Which of the following is valid in Java’s enhanced switch (Java 12+)?

 

6 / 10

What will the following code output?

int x = 5, y = 10;
if (x < y && x++ < 10) { System.out.println(x); }

7 / 10

What will the following code output?

String fruit = “Apple”;
switch (fruit) {
case “Mango”:
System.out.println(“Mango”);
break;
case “Apple”:
System.out.println(“Apple”);
break;
default:
System.out.println(“Other”);
}

8 / 10

What will the following code output?

boolean flag = false;
if (!flag) {
System.out.println(“Flag is false”);
} else {
System.out.println(“Flag is true”);
}

9 / 10

What will the following code print?

int a = 10, b = 20, c = 30;
if (a > b || b < c && a > c) {
System.out.println(“Condition Met”);
} else {
System.out.println(“Condition Not Met”);
}

10 / 10

What will the following code output?

int x = 5;
int result = (x > 10) ? 100 : (x < 3) ? 200 : 300; System.out.println(result);

Your score is

Scroll to Top