Java System.out.println Method Quiz

IntuitiveTungsten avatar
IntuitiveTungsten
·
·
Download

Start Quiz

Study Flashcards

22 Questions

What is the purpose of System.out.println in Java?

To print output to the console and move to a new line

How is the data to be output given in System.out.println?

Through a plus sign

What is the difference between System.out.println and System.out.print?

println generates a new line, print does not

What is the purpose of importing packages and classes in Java?

To make libraries easily accessible to programs

Which package is automatically imported into every Java program?

java.lang.Integer

What is the benefit of specifying a class instead of using (*) when importing packages?

It avoids naming conflicts

Which class in Java is used for simple keyboard input starting from version 5.0?

Scanner

What statement is required to make the Scanner class available to a Java program?

import java.util.Scanner;

What object is created by the following code: Scanner keyboard = new Scanner(System.in);?

keyboard

Which method reads one int value typed at the keyboard and assigns it to a variable?

nextInt()

What does the method 'nextDouble' do in Java's Scanner class?

Reads a double value

How are multiple inputs separated when using the Scanner class?

By whitespace

When using 'keyboard.next()', how is a string read?

Reads one string of non-whitespace characters delimited by whitespace

'keyboard.nextLine()' reads what in Java?

'\n' character input when the Enter key pressed

What can happen when combining 'nextInt()' method and 'nextLine()' method in Java?

There might be problems due to line terminator characters

What does whitespace refer to in Java's Scanner class context?

Blank spaces, tabs, and line breaks that separate inputs

What is the value of the variable n in the given code?

2

What is the value of the variable s1 in the given code?

""

What is the value of the variable s2 in the given code?

"heads are better than"

When should a program prompt the user for input?

Whenever the program needs data from the user

What is the purpose of echoing the user's input in a program?

To expose subtle errors in the user's input

How can the input delimiter be changed when using the Scanner class?

By calling the useDelimiter() method

Study Notes

Output in Java

  • System.out is an object that is part of the Java language.
  • println is a method invoked by the System.out object, used for console output.
  • The data to be output is given as an argument in parentheses.
  • A plus sign is used to connect more than one item.
  • Every invocation of println generates a new line after it finishes.

Differences between println and print

  • print is a method that can be invoked by the System.out object.
  • print is similar to println, except that it does not generate a new line.
  • With println, the next output goes on a new line.
  • With print, the next output goes on the same line.

Importing Packages and Classes

  • Libraries in Java are called packages.
  • A package is a collection of classes that is stored in a manner that makes it easily accessible to any program.
  • To use a class that belongs to a package, the class must be imported using an import statement.
  • Classes found in the package java.lang are imported automatically into every Java program.
  • It is better to specify the class instead of using the * wildcard.

Console Input Using the Scanner Class

  • The Scanner class is used for simple keyboard input.
  • The Scanner class is included in Java starting from version 5.0.
  • To use the Scanner class, a program must include the import java.util.Scanner statement.
  • The Scanner class is used to create an object that reads input from the keyboard.
  • The Scanner object can be given any name.

Methods in the Scanner Class

  • nextInt() reads one int value typed in at the keyboard and assigns it to a variable.
  • nextDouble() reads one double value typed in at the keyboard and assigns it to a variable.
  • next() reads one string of non-whitespace characters delimited by whitespace characters.
  • nextLine() reads an entire line of keyboard input.
  • Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method.

Pitfalls of Using the Scanner Class

  • The nextLine() method reads the remainder of a line of text starting wherever the last keyboard reading left off.
  • This can cause problems when combining it with different methods for reading from the keyboard.

Programming Tips

  • A program should always prompt the user when they need to input some data.
  • A program should always echo all input that it receives from the keyboard.

Other Input Delimiters

  • The delimiters that separate keyboard input can be changed when using the Scanner class.
  • The useDelimiter() method can be used to change the input delimiter.

Test your knowledge on how to use the System.out.println method in Java for console output. Learn about providing arguments, connecting multiple items, and generating new lines. Also, compare println with the print method.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Java Programming
10 questions

Java Programming

CreativeTrust6309 avatar
CreativeTrust6309
Java OOP Quiz
10 questions

Java OOP Quiz

FastestValley avatar
FastestValley
Use Quizgecko on...
Browser
Browser