Cosmic Module

J

Qubits of DPK

March 14, 2026

Core Java

Layman Explanation

Your brain makes decisions all day — if hungry eat, else continue working. Conditional statements give Java the same decision-making ability.

Real World Analogy

Traffic signal — Green: go, Yellow: slow, Red: stop. Java evaluates conditions exactly like you read a traffic light.

5 Conditional Constructs

1️⃣ if

java
QUBITS OF DPK
1if (temperature > 37) {
2    System.out.println("Fever!");
3}

2️⃣ if-else

java
QUBITS OF DPK
1if (age >= 18) {
2    System.out.println("Can vote");
3} else {
4    System.out.println("Cannot vote");
5}

3️⃣ if-else-if Ladder

java
QUBITS OF DPK
1if (marks >= 90)      System.out.println("A");
2else if (marks >= 75) System.out.println("B");
3else if (marks >= 60) System.out.println("C");
4else                  System.out.println("F");
5// Once match found → rest SKIPPED

4️⃣ Nested if

java
QUBITS OF DPK
1if (age >= 18) {
2    if (hasVoterID) {
3        System.out.println("Can vote");
4    }
5}
6// Always use {} to avoid dangling else trap

5️⃣ Switch

java
QUBITS OF DPK
1switch (day) {
2    case 1: System.out.println("Monday"); break;
3    case 2: System.out.println("Tuesday"); break;
4    default: System.out.println("Weekend");
5}

Ternary Operator

java
QUBITS OF DPK
1String category = (age >= 18) ? "Adult" : "Minor";
2// (condition) ? valueIfTrue : valueIfFalse

Switch Fall-Through — Critical Trap

java
QUBITS OF DPK
1switch (x) {
2    case 1:
3        System.out.println("One");   // no break!
4    case 2:
5        System.out.println("Two");   // ALSO runs if x==1!
6}
7// Fix: always add break;

🆆 Enhanced Switch (Java 14+)

java
QUBITS OF DPK
1String day = switch (number) {
2    case 1 -> "Monday";
3    case 2 -> "Tuesday";
4    default -> "Weekend";
5};
6// Arrow syntax → no fall-through risk, returns value directly

When to Use What

️ All Traps

java
QUBITS OF DPK
1// Trap 1 — Fall-through without break
2// Trap 2 — Dangling else (belongs to nearest if)
3// Trap 3 — float/double NOT allowed in switch
4// Trap 4 — Assignment vs comparison: if(x=5) vs if(x==5)
5// Trap 5 — Ternary can't execute multiple statements
6// Trap 6 — nested ternary beyond 2 levels → unreadable

Interview Answer (SDE-2)

"Java has 5 conditional constructs. if executes block when true. if-else adds alternative. if-else-if ladder checks multiple conditions — only first match executes. Nested if handles compound conditions — always use braces to avoid dangling else. Switch is cleaner for single variable against fixed values — critical trap is missing break causing fall-through. Java 14+ enhanced switch with arrow syntax eliminates fall-through entirely. Ternary is a one-line if-else for value assignment."

Interview Questions & MAANG-Level Answers

Q1. What is fall-through in switch and how do you prevent it?
Fall-through means when a matching case executes, execution continues into the NEXT case automatically unless stopped. This happens when break is missing. Example: if x=1 and case 1 has no break, both case 1 and case 2 code runs. Prevention: always add break at end of each case, or use Java 14+ arrow syntax (case 1 -> ...) which has no fall-through by design. Intentional fall-through (multiple cases sharing same code) should be explicitly commented.
Q2. What is dangling else and how do you avoid it?
Dangling else is when an else is ambiguous about which if it belongs to. In Java, else always belongs to the nearest preceding if. Example:
java
QUBITS OF DPK
1if (a > 0)
2    if (a > 5) System.out.println("big");
3else System.out.println("negative"); // belongs to inner if, not outer!
Avoidance: ALWAYS use curly braces {} even for single-statement if blocks. This is also a standard code style in production.
Q3. What types can switch work with in Java?
byte, short, int, char, String (Java 7+), enum, and their wrapper classes (Byte, Short, Integer, Character). NOT allowed: long, float, double, boolean. Java 21 adds pattern matching allowing any Object type in switch expressions.
Q4. What is the difference between ternary and if-else?
Ternary is an expression — it returns a value and fits in one line: String result = (x > 0) ? "pos" : "neg". if-else is a statement — it executes blocks of code and can contain multiple statements. Ternary cannot execute multiple statements or void operations. Use ternary for simple value assignment; use if-else for complex logic, multiple statements, or when readability matters.
Q5. What does enhanced switch (Java 14+) improve?
Three improvements: (1) Arrow syntax eliminates fall-through risk entirely. (2) Switch becomes an expression that directly returns a value — no need to assign inside each case. (3) Multiple labels per case with commas (case 1, 2, 3 ->). (4) Compiler enforces exhaustiveness — must cover all cases or have default. (5) yield keyword for returning value from multi-statement block cases.