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 ");
            break;
        case 3:
            System.out.print("three ");
        case 4:
            System.out.print("four ");
         default:
            System.out.print("def ");
    }
}
Explanation

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.

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести