Sunday, 5 August 2012

Question 162 : Statements

Given:
3. public class Breaker {
4. static String o = "";
5. public static void main(String[] args) {
6. z:
7. o = o + 2;
8. for(int x = 3; x < 8; x++) {
9. if(x==4) break;
10. if(x==6) break z;
11. o = o + x;
12. }
13. System.out.println(o);
14. }
15. }
What is the result?

A. 23
B. 234
C. 235
D. 2345
E. 2357
F. 23457
G. Compilation fails.

2 comments:

  1. Its because java does not allow you to jump from inside a loop to another statement.

    while you jump out of a loop to a label. the label should be immediately above the loop and no other statement should should come in between.

    The program will work if you put the label "z:" after the "o = o + 2;" statement.

    ReplyDelete