Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 5

Given the code fragment:

What is the result?

    Correct Answer: E

    The loop starts with i=0 and i < 10. At i=0, i % 5 is 0, so the default case is executed, which causes a break statement, and then i is printed and incremented to 1. When i=1, i % 5 is 1, case 1 is executed, incrementing i to 2 and then continues back to the beginning of the loop without printing. On i=2, i % 5 is 2, case 2 gets executed, which sets i to i *= 2 * i, resulting in i becoming 8, and then it breaks to print and increment i to 9. At i=9, i % 5 is 4, so case 4 is executed, incrementing i to 10 and then continues again back to the beginning, thus avoiding the print statement. Finally, the loop exits as i is now 10, and thus, only the numbers 0 and 8 are printed.

Discussion
pikosssOption: E

E is correct i*= 2*i; -> i=2x2x2 -> 8

Ankit1010Option: E

E is correct answer

APJ1Option: E

E is correct

james2033Option: E

public class Foo { public static void main(String[] args) { for (var i = 0; i < 10; i++) { switch (i % 5) { case 2: i *= 2 * i; break; case 3: i++; break; case 1: case 4: i++; continue; default: break; } System.out.print(i + " "); i++; } } } // Result: // 0 8

[Removed]Option: E

E is correct! i = 0, 0%5 = 0, default get's hit. 0 get's printed and incremented to 1. i = 1, case 1 has no break so we increment i to 2 and continue. i=2, 2%5 = 2 so 2 * 2 *2= 8. we print 8 and increment to 9 i=9, 9%5 = 4. we increment i to 10 and continue. We exit the for loop as 10 is not smaller than 10.

LebanninOption: D

i=0 -> 0%5 is 0 so we go to default and break, then print 0 and increment it, so the next iteration will have i = 2. I=2 -> 2%5 is 2 so we double I then break from the loop with I = 4 print 4, increment to 5 and continue. I=6 -> 6%5 is 1 so we just increment and continue (go to the next iteration) I=8 -> 8%5 is 3 so we just increment to 9 and break, print 9 then increment and reach 10 which is the end. So the answer is D.

StavokOption: E

E is correct

AlanRMOption: E

E is correct