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
2️⃣ if-else
java
QUBITS OF DPK
3️⃣ if-else-if Ladder
java
QUBITS OF DPK
4️⃣ Nested if
java
QUBITS OF DPK
5️⃣ Switch
java
QUBITS OF DPK
Ternary Operator
java
QUBITS OF DPK
Switch Fall-Through — Critical Trap
java
QUBITS OF DPK
🆆 Enhanced Switch (Java 14+)
java
QUBITS OF DPK
When to Use What
️ All Traps
java
QUBITS OF DPK
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
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.