Results 1 to 5 of 5

Thread: Code

  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Code

    hi all,

    i was going through a site consists of scjp questions and i bumped into this

    public class Test {
    public static void main(String[] args){
    int j = 0;
    for(; j < 3; j++){
    if (j==1) break out;
    System.out.print(j + "\n");
    }
    out:{System.out.println("bye");}
    }
    }
    1. The code will fail to compile.
    2. The code will run with no out put
    3. This will run and print 0, 1 , 2 and "bye"
    4. This will run and print 1 and "bye"

    Now it says 1 is the correct answer and the reason it gives is that "This will fail to compile because the labelled block does not enclose the break statement."

    But i think 3 is the right answer. What do you all think what the right answer might be??

    Thank you and regards

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Code

    suppose you are a java compiler and you must compile this code. you know that the keyword break or continue can have an argument. now when you arrive to the line with break out you will search for the label out and you will not find it. what will you do? to be a reliable compiler you would throw an error with the message that the label out is missing.

    I'm also preparing for the exam. If you have further questions let me know.
    Last edited by j2me64; 03-26-2012 at 10:34 AM.

  3. #3
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Code

    Quote Originally Posted by j2me64 View Post
    suppose you are a java compiler and you must compile this code. you know that the keyword break or continue can have an argument. now when you arrive to the line with break out you will search for the label out and you will not find it. what will you do? to be a reliable compiler you would throw an error with the message that the label out is missing.

    I'm also preparing for the exam. If you have further questions let me know.
    thank you and i surely will.

  4. #4
    Tott3 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: Code

    This will break the code i THINK

    for(; j < 3; j++){

    I'm not very good at Java so I'm not 100% sure tho.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Code

    Quote Originally Posted by Tott3 View Post
    This will break the code i THINK

    for(; j < 3; j++){

    I'm not very good at Java so I'm not 100% sure tho.
    Nope, the initializer part of the for-loop is optional, so that code is syntactically correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 03-25-2012, 04:40 AM
  2. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  3. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •