Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

exam questions basic java

  • 13-12-2010 4: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