0% found this document useful (0 votes)
25 views3 pages

Java Loops Conditionals Interview QA Full

Java loops statement

Uploaded by

musicamrutha3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Java Loops Conditionals Interview QA Full

Java loops statement

Uploaded by

musicamrutha3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Interview Questions on Loops and Conditionals (Full Set)

Q1. Difference between if-else and switch?


Ans: if-else handles ranges/conditions. switch works with discrete values (int, char, String, enum).
Q2. Can we use switch with String?
Ans: Yes, from Java 7 onwards.
Q3. Can we use switch with multiple cases having same code?
Ans: Yes, using fall-through without break.
Q4. What is the default case in a switch? Is it mandatory?
Ans: Executes if no case matches. It is optional.
Q5. Can we use switch with boolean, float, or double?
Ans: No, only byte, short, int, char, enum, String.
Q6. What is the difference between == and equals()?
Ans: == compares references, equals() compares content.
Q7. Can if statement work without else?
Ans: Yes, else is optional.
Q8. What is nested if?
Ans: An if inside another if.
Q9. What happens if we don’t use break in a switch?
Ans: Fall-through occurs; next case executes.
Q10. Difference between if-else-if ladder and switch?
Ans: if-else checks sequentially, switch jumps to case directly.
Q11. Can we write switch(null)?
Ans: No, it throws NullPointerException.
Q12. Can ternary operator ?: replace if-else?
Ans: Yes, for simple conditions.
Q13. Difference between if(x==true) and if(x)?
Ans: Both work if x is boolean, but if(x) is preferred.
Q14. Can we use return inside if?
Ans: Yes, it terminates method immediately.
Q15. Which is faster: if-else or switch?
Ans: switch is faster for discrete values.
Q16. Difference between for, while, and do-while?
Ans: for = known iterations, while = condition first, do-while = executes once before check.
Q17. Difference between while(true) and for(;;)?
Ans: Both are infinite loops.
Q18. What is an enhanced for loop?
Ans: for-each loop to iterate arrays/collections.
Q19. Can we remove elements from a collection in for-each?
Ans: No, use Iterator to avoid ConcurrentModificationException.
Q20. Difference between break and continue?
Ans: break exits loop, continue skips current iteration.
Q21. Can we use a loop without body?
Ans: Yes, using ; (empty statement).
Q22. Can a loop run infinitely?
Ans: Yes, if condition is always true.
Q23. Entry-controlled vs exit-controlled loops?
Ans: Entry-controlled: condition checked first (for, while). Exit-controlled: executes once before check (do-wh
Q24. Can we nest loops in Java?
Ans: Yes, loop inside another loop.
Q25. What is loop unrolling?
Ans: Optimization where loop iterations are expanded to reduce overhead.
Q26. Can we label loops in Java?
Ans: Yes, using labels with break/continue.
Q27. Difference between continue in normal vs labeled loop?
Ans: Normal continue → next iteration, labeled continue → skips outer loop iteration.
Q28. For-each vs normal for loop performance?
Ans: For-each is simpler, normal for allows index access.
Q29. Can we declare variable inside loop condition?
Ans: Yes, e.g., for(int i=0; i<5; i++).
Q30. Can we use multiple conditions in a loop?
Ans: Yes, with comma operator in for loop.
Q31. What if we use float/double in loops?
Ans: Works but may cause precision issues.
Q32. Difference between forEach method (Java 8) and for-each loop?
Ans: forEach is a Stream/Collection method using lambdas.
Q33. Can we break out of multiple nested loops at once?
Ans: Yes, using labeled break.
Q34. What is infinite recursion?
Ans: Method calls itself endlessly, behaves like infinite loop.
Q35. Maximum number of times a loop can run?
Ans: Up to Integer.MAX_VALUE theoretically.
Q36. Scope of variable declared inside for loop?
Ans: Accessible only within the loop.
Q37. Can we use loops without conditions?
Ans: Yes, e.g., for(;;).
Q38. Can break be used without loops?
Ans: No, except inside switch.
Q39. Can continue be used without loops?
Ans: No, only inside loops.
Q40. Difference between recursion and loops?
Ans: Loops use iteration, recursion calls method repeatedly.

You might also like