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!:
Solution
The code will print out: :World!::Hello :
Here's the step-by-step explanation:
-
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!". -
String c = scnr.next();This line uses thenext()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 variablec. -
String b = scnr.nextLine();This line uses thenextLine()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 variableb. -
String a = scnr.next();This line again uses thenext()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 variablea. -
System.out.println(":" + a + ":" + b + ":");This line prints out the string ":" followed by the value ofa, then another ":", then the value ofb, and finally another ":". So it will print out:World!::Hello :.
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
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.