Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

exam questions basic java

  • 13-12-2010 05:58PM
    #1
    Registered Users, Registered Users 2 Posts: 603 ✭✭✭


    System.out.println("One\nTwo\nGo\n");
    System.out.printf("<%6d><%3d><%-4d>\n",
    -18, 4, 9);
    double value = 6.241;
    System.out.printf("<%8.2f><%6.1f>\n",
    value, 3.14159);
    System.out.printf("Lab is <%8s>\n", "B2-005");
    boolean bVar = true;
    System.out.printf("%b\n", bVar);

    This is an exam question we were given in basic java programming and we have to show the output

    I know the first one is just
    One
    Two
    Go
    where \n means skip a line but im lost after that.Could anyone helpexplain it please?



Comments

  • Registered Users, Registered Users 2 Posts: 4,277 ✭✭✭km991148


    eoins23456 wrote: »
    System.out.println("One\nTwo\nGo\n");
    System.out.printf("<%6d><%3d><%-4d>\n",
    -18, 4, 9);
    double value = 6.241;
    System.out.printf("<%8.2f><%6.1f>\n",
    value, 3.14159);
    System.out.printf("Lab is <%8s>\n", "B2-005");
    boolean bVar = true;
    System.out.printf("%b\n", bVar);

    This is an exam question we were given in basic java programming and we have to show the output

    I know the first one is just
    One
    Two
    Go
    where \n means skip a line but im lost after that.Could anyone helpexplain it please?

    You probably wont be given the answer directly (I am sure its against the rules..) but here are some pointers:

    \n doesnt mean 'skip a line' but it prints the new line character, which you can think of as hitting the return key when typing (its not directly the same, but thats not for now..)

    You may also want to google for Java String Formatting or something. What is happening is that variable of one type (doubles, ints, bools etc) are being printed to the screen via printf. As part of this they get converted to formatted strings (maybe also look up what printf does and the arguments it takes?) as decided by the arguments passed to printf.

    This isnt the most correct answer, but hopefully it will guide you.

    If you dont care/dont need to learn about whats happening then I guess you could also do a quick exaple application in about the same time as it took to type it here...


  • Closed Accounts Posts: 152 ✭✭Feckfox


    eoins23456 wrote: »
    System.out.println("One\nTwo\nGo\n");
    System.out.printf("<%6d><%3d><%-4d>\n",
    -18, 4, 9);
    double value = 6.241;
    System.out.printf("<%8.2f><%6.1f>\n",
    value, 3.14159);
    System.out.printf("Lab is <%8s>\n", "B2-005");
    boolean bVar = true;
    System.out.printf("%b\n", bVar);

    This is an exam question we were given in basic java programming and we have to show the output

    I know the first one is just
    One
    Two
    Go
    where \n means skip a line but im lost after that.Could anyone helpexplain it please?

    You got the first three right.

    printf("%6d", 18) should give " 18" I think. It puts 6 spaces in front of the number. I used quotations there to make the spaces more obvious.

    I just looked it up and -4 would left align and 4 would right align to the width of 4 characters. Right align is the default.

    %8.2f is for a float number. It moves it 8 spaces to the right and rounds it to two decimal places.

    %8s is the same deal for a string.
    The last one prints the boolean value.


Advertisement