What will be the result of the following code execution?

for(int i = 0 ; i < 4 ; ) {
    switch(new Integer(i++)) {
        case 1:
            System.out.print("one ");
        case 3:
            System.out.print("three ");
        case 4:
            System.out.print("four ");
            System.out.print("def ");

Four cycle iterations will be performed
when i = 0 def will be printed out
when i = 1 one will be printed out
when i = 2 def will be printed out
when i = 3 three four def will be printed out

Due to the i++ postfix notation the previous value in a switch operator will be checked, and given the absence of break operator in case 3 and case 4 branches leads to their consequent execution.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes