1. What is the core execution difference between a while loop and a do-while loop?
Answer:
-
whileLoop (Entry-Controlled): Checks its condition before running the code block. If the initial condition isfalse, the code block inside will execute 0 times. -
do-whileLoop (Exit-Controlled): Runs the code block first, and then evaluates its condition at the bottom. The code block is guaranteed to execute at least 1 time, regardless of whether the initial condition is true or false.
Â
2. What happens if you forget to add a break statement inside a switch case block?
Answer: Omitting break causes fall-through behavior. Java will execute the matching case code block, and then automatically continue executing all subsequent case blocks below it sequentially-ignoring their conditional expressions-until it hits a break or reaches the end of the switch statement.
Â
3. How do break and continue alter the control flow inside a loop?
Answer:
-
break: Terminates the entire loop immediately. Control jumps out of the loop body to the next line of code below it. -
continue: Skips only the remaining lines of code in the current iteration and jumps straight to the next loop evaluation step (updating counter variables).
Â
4. What is an infinite loop? Write a minimal example using both for and while.
Answer: An infinite loop is a loop whose condition never evaluates to false, causing the loop body to run continuously until killed or system memory exhausts.
// Infinite while loop
while (true) {
/* runs forever */
}
// Infinite for loop
for (;;) {
/* runs forever */
}
5. What is a ternary operator, and how does it simplify an if-else statement?
Answer: The ternary operator (? :) is a shorthand syntax for simple if-else assignments. It evaluates a condition and yields one of two values.
// Standard if-else
int age = 19;
String status;
if (age >= 18) {
status = "Adult";
} else {
status = "Minor";
}
// Equivalent Ternary Operator
String status = (age >= 18) ? "Adult" : "Minor";
6. What data types are supported inside a Java switch expression?
Answer: Java Supports
-
Primitive types:
byte,short,char,int -
Wrapper classes:
Byte,Short,Character,Integer -
Enum types
-
String(supported starting in Java 7)
Â
7. Predict the output of the following loop:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i + " ");
}
Answer: 1 2 4 5Â
(Explanation: When i == 3, continue executes, skipping the System.out.print step for that iteration and jumping directly to i++).
Â
8. Can an if condition evaluate an integer value directly, like if (1) in C/C++?
Answer: No. In Java, conditions inside if, while, or for control statements must evaluate strictly to a boolean type (true or false). Passing an integer like if (1) or if (0) triggers a compile-time error: Incompatible types: int cannot be converted to boolean.
Â
9. What is a nested loop, and how many total iterations occur in the example below?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
// Code block
}
}
Answer: A nested loop is a loop placed inside another loop. The inner loop executes completely for every single iteration of the outer loop.
-
Outer loop runs 3 times ($i = 0, 1, 2$).
-
Inner loop runs 4 times for each outer pass ($j = 0, 1, 2, 3$).
-
Total execution iterations = $3 \times 4 =$ 12 iterations.
Â
10. Predict the output of this code snippet:
int count = 10;
do {
System.out.print(count + " ");
count++;
} while (count < 5);
Answer: 10