Knowee
Questions
Features
Study Tools

What will the following code print out when it is run?Scanner scnr = new Scanner("Hello \n\n World!");String c = scnr.next();String b = scnr.nextLine();String a = scnr.next();System.out.println(":" + a + ":" + b + ":");Group of answer choices:Hello:World!::World!: ::Hello: :: : World!:

Question

What will the following code print out when it is run?Scanner scnr = new Scanner("Hello \n\n World!");String c = scnr.next();String b = scnr.nextLine();String a = scnr.next();System.out.println(":" + a + ":" + b + ":");Group of answer choices:Hello:World!::World!: ::Hello: :: : World!:

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code will print out: :World!::Hello :

Here's the step-by-step explanation:

  1. Scanner scnr = new Scanner("Hello \n\n World!"); This line creates a new Scanner object that will scan through the string "Hello \n\n World!".

  2. String c = scnr.next(); This line uses the next() method of the Scanner class to find and return the next complete token from the scanner, which is "Hello". The token is then stored in the string variable c.

  3. String b = scnr.nextLine(); This line uses the nextLine() method of the Scanner class to move the scanner position to the end of the current line and return the text that was skipped over. This includes any line separator characters. In this case, it will return " " (a space), and this is stored in the string variable b.

  4. String a = scnr.next(); This line again uses the next() method of the Scanner class to find and return the next complete token from the scanner, which is "World!". The token is then stored in the string variable a.

  5. System.out.println(":" + a + ":" + b + ":"); This line prints out the string ":" followed by the value of a, then another ":", then the value of b, and finally another ":". So it will print out :World!::Hello :.

This problem has been solved

Similar Questions

What will be printed out on each line?Scanner input = new Scanner("This\nhas 5\n4\nlines\n");System.out.println("%" + input.next() + "%"); System.out.println("%" + input.nextLine() + "%"); System.out.println("%" + input.nextLine() + "%"); System.out.println("%" + input.nextInt() + "%"); System.out.println("%" + input.nextLine() + "%"); System.out.println("%" + input.nextLine() + "%");

What will be the output of the following program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world";   String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}

Select the correct answerWhat will be the output of the following Java program?class java  {    public static void main(String args[])     {          int w = 5;       int x = 10;       first:        {        second:         {          third:           {             if (w == x >> 1)              break second;          }          System.out.println(w);        }        System.out.println(x);       }    }   }Options5 1051010 5

What will be printed out on each line?String note = "Minsub\n22\nCS";Scanner input = new Scanner(note);System.out.println("#" + input.nextLine() + "#"); System.out.println(input.nextInt()); System.out.println("#" + input.nextLine() + "#");

What will be printed when the following code is executed? String firstname = "John"; String surname = "Smith"; surname.toUpperCase(); System.out.print(fir

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.