Java / Control Statements
1. Is there a way to access an iteration-counter in Java's for-each loop?
You'll have to provide your own counter. The reason is that the for-each loop internally does not have a counter; it is based on the Iterable interface....
2. Advantages of declaring loop variable as final in enhanced for-loop.
Enables using the loop variable in an anonymous inner class within the loop body. for (final int i : intList) { Runnable run = new Runnable() { public void run() { process(i); } }; new Thread(run).start(); } It prevents the...
3. Does returning a value from the loop body break the loop?
Yes. return statement breaks the loop and returns from the entire method immediately....
4. What happens when we forget break in switch case?
The switch statement will continue the execution of all case labels until if finds a break statement, even though those labels doesnΒt match the expression value.
5. What types of loops does Java support?
Java offers three different types of loops: for, while, and do-while. A for loop provides a way to iterate over a range of values....
6. How will you exit anticipatedly from a loop?
Using the break statement, we can terminate the execution of a loop immediately. for (int i = 0; ; i++) { if (i > 10) { break; } }
7. Difference between dead code and unreachable code in Java.
Dead code is a compiler warning while unreachable code is a compile time error. As per Java language specification, there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains...
8. What types of loops does Java support?
Java offers three different types of loops: for, while, and do-while. A for loop iterates over a range of values....
9. Which is considered as a selection statement?
if block is considered as selection statement; continue and break are jump statements, and for is a looping statement.
10. Can you create switch statement using enum?
Yes. public class EnumSwitchCaseExample { enum Communication { EMAIL, CHAT, PHONECALL, MESSAGE } public static void main (String [] args) { Communication mode = Communication.CHAT; switch (mode) { case EMAIL: System.out.println( "Communicated by Email."...
11. Can we write switch case using a double variable?
No. We cannot switch on a value of type double.
12. What is tautology and contradiction in boolean expressions?
The tautology refers to a logical expression which always evaluates to true, regardless of the logical value of its variables. For example, consider the below expresion in Java....
13. Difference between break and continue statements.
break continue Can be used in switch and loop (for, while, do while) statements. Can be only used with loop statements....
14. How is an infinite loop declared in Java?
Infinite loops are those loops that run infinitely without any breaking conditions. Using for Loop: for (;;) { // Business logic // Any break logic } Using while loop: while(true){ // Any break logic } Using do-while loop: do {...