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 5 of 5

Thread: Please see multiple questions I got wrong on an exam, what I did wrong?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Please see multiple questions I got wrong on an exam, what I did wrong?

    Here is a list of what I had gotten wrong on an exam:

    boolan a = false, b = true, c = true, d = false, e = true;

    1. System.out.println a != c;

    I had converted it to a==c, and had answered FALSE. I'm checking the answer again -- and since a = false, c = true; the result of a == c would be FALSE. FALSE was my answer.

    But I got the answer wrong, did I read too much into it and convert the not equals into ==, when I should've left it alone, and resulting in a != c being TRUE. because that would be true, a(false) doesn't equal c(true.

    2. This was a question that asked for showing the EXACT output that will be created by execution of the following program.

    package javaapplication1;
     
     
    public class JavaApplication1
    {
     
        public static void main (String[] args) 
        {
            int manny = 40, moe = 23, jack = 26;
            System.out.printf ("%3d%3d%3d\n", manny, moe, ++jack);
                if (manny > moe)
                    if (moe > jack) {
                            moe = 40;
                            System.out.printf ("%3d%3d\n", ++jack, moe--);
                        }
                    else
                            manny-=manny;
                else
                    if(moe > jack)
                            jack = 50;
                    else
                        System.out.printf ("%3d%3d\n", ++jack, moe--);
              System.out.printf("%3d%3d%3d\n", manny, moe, ++jack);
               if (manny > moe)
                   if (moe > jack)  {
                            moe = 31;
                            System.out.printf("%3d%3d\n", ++jack, moe--);
                   }
               else
                            manny -= manny;
           else
               if(moe > jack)
                       jack = 50;
               else
                   System.out.printf ("%3d%3d\n", ++jack, moe--);
           System.out.printf("%3d%3d%3d\n", manny, moe, ++jack);
        }
    }

    Here is how I tracked my variables, I would cross them out as I read through the program and the most bottom one would be the newest value
    manny moe jack
    40 23 26
    0 22 27
    28
    29

    My output looked like this(everything had to be exact, spacing is indicated by the underscore _ )
    OUTPUT
    _40_23_27
    _28_23
    __0_22_29


    Please help me learn what I did wrong.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Please see multiple questions I got wrong on an exam, what I did wrong?

    1. You're confusing me with your thought process, so I'm going to ignore it for a bit. This is what I see:

    Given:
    a = false, c = true

    The statement:
    a != c (say, "a does not equal c")

    is TRUE, because (substitute from the givens):
    false != true (say, "false does not equal true")

    I don't have time now for #2, but I'll look at it later.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please see multiple questions I got wrong on an exam, what I did wrong?

    _40_23_27
    __0_23_28
    _29 23
    __0_22_30

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Please see multiple questions I got wrong on an exam, what I did wrong?

    Did you get number 2 right or wrong?

    If you got it wrong, a great way to figure out why is to actually type that program up and compile and run it. Add some print statements so you know the value of each variable on each line.
    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!

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please see multiple questions I got wrong on an exam, what I did wrong?

    Quote Originally Posted by ggx7 View Post
    Please help me learn what I did wrong.
    ggx7
    It looks like you are evaluating statements that are made within an "else" that shouldn't be evaluated.
    Eg
    The first if statement
    if (manny > moe)

    is true, so you would never evaluate the "else" statement for that, even though by the time you get to that 'else' line, the original if statement would now be evaluated as false (because you aren't evaluating that if statement again, it was true when it was evaluated and that's all that matters, so skip the else).

    Follow Kevin's advice and just compile this code and see what you get...

Similar Threads

  1. Multiple questions(SOLVED/CLOSED)
    By albin1228 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 17th, 2013, 01:48 PM
  2. [SOLVED] Multiple questions - Java beans
    By wdh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2012, 01:40 PM
  3. [SOLVED] PLZ HELP! Somethng wrong with code, assignment due today! Print if multiple of...
    By Rstuart970 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2012, 02:35 AM
  4. Some help on some multiple questions
    By djl1990 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 6th, 2012, 01:00 PM
  5. 50 SCJP Mock Exam Questions with Answers
    By JavaPF in forum The Cafe
    Replies: 3
    Last Post: July 6th, 2010, 11:10 AM