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. It uses an Iterator to loop through the "collection" - which is not a collection and does not have index.
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 loop variable to be accidently changed inside the loop while iterating.
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. The only code that will be executed is the finally clause and the release of any synchronized statement. public class ForLoopReturnExample { public static void main (String [] args) { for ( int i = 0 ; i < 10 ; ...
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 doesnt 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. Its most useful when we know in advance how many times a task is going to be repeated. for (int i = 0; i < 50; i++) { // do something } A while loop can execute a bl...
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 the statement to the statement its...
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. It is useful when we know in advance how many times a task is going to be repeated. for (int i = 0; i < 10; i++) { // perform some action } Java also supports enhanced for loop to ite...
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." ); break ; case CHAT: System.out.println( "...
11. Can we write switch case using a double variable?
No. We cannot switch on a value of type double. Only int, strings or enum variables are permitted.
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. boolean isTrue = true; if (! (! (isTrue) )) { } In the above example, applying NOT operation twice (double negation) do...
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. It causes the switch or loop statements to terminate the moment it is executed. It doesn't terminate the loop but causes the loop to jump to the next iteration. It terminates th...
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 { ... // Any break logic } while ( true );