Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Confusing question.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confusing question.

    Hey guys,
    I'm new here and new to programming, doing my first programming course this semester. I have an upcoming test tomorrow and I've been reviewing questions that the lecturer gave us.
    Anyways,






    This is the question I'm confused about. No matter how I think about it, my answer will always be D, but when I execute the code B prints.
    Can anyone explain this to me?

    Thanks.
    Cheers,
    Fok


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Confusing question.

    I would actually vote for C. Are you sure you coded it exactly how it's displayed here? Hint: check your brackets.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Confusing question.

    Kevin is correct, total syntax error. Evil teacher.

    Anyways, you are correct to think it would be D if there was no syntax error. Each pass prints i and i+1 then adds 1 to i.
    Pass1: 0 1
    Pass2: 1 2
    Pass3: 2 3
    Overall output: 0 1 1 2 2 3...

    Your code always executes as B? Could you post your code as you have it running and we could tell you where you went wrong?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,971
    Thanks
    56
    Thanked 2,359 Times in 2,330 Posts

    Default Re: Confusing question.

    Good example of poor formatting of code and how it makes the code harder to read and understand.
    The code within the for loop is not indented. There aren't any {}s to enclose the statements within the for loop statement.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusing question.

    int i = 0;

    public void act()
    {

    {
    for (int i = 0; i < 10; i ++)
    System.out.print(i + " ");
    System.out.print( (i+1) + " ");

    }

    }

    Is what I have. Had to add the i = 0; myself or it would have error.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Confusing question.

    Yeah, that would give you an error.

    --- Update ---

    The reason you are seeing B (even though i'm pretty sure it would be followed by an error) is because a for loop without proper brackets will only loop the line immediately following it. This is poor practice and causes a lot of confusion. Always use brackets around your whole for loop. "But I do have brackets around my whole for loop" you say. No...the open bracket is in the wrong place.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    619
    My Mood
    Sleepy
    Thanks
    10
    Thanked 94 Times in 87 Posts

    Default Re: Confusing question.

    @fok: So c is correct, but your code won't produce b either.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Confusing question.

    Actually, the problem is your int i = 0 at the top. That's not in the original question code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confusing question.

    Yes, but if it's not there I get syntax error

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Confusing question.

    Quote Originally Posted by fok View Post
    Yes, but if it's not there I get syntax error
    Ding ding ding. Take a look at the question again, particular answer C.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Peice of code confusing
    By BLUEJ in forum Java Theory & Questions
    Replies: 2
    Last Post: February 18th, 2013, 05:48 PM
  2. Confusing LWJGL Error
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 14th, 2013, 12:20 PM
  3. a very confusing exception
    By deathlypest in forum Java Theory & Questions
    Replies: 29
    Last Post: June 11th, 2012, 01:17 PM
  4. Button help, confusing error.
    By camboy8 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 10:25 PM
  5. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 01:17 AM