Reading input Flashcards
What is the purpose of importing the Scanner library in Java?
a) To perform arithmetic operations
b) To read user input
c) To display output on the console
d) To manage files
b) To read user input
What does the following line of code do? Scanner inputScanner = new Scanner(System.in);
a) It creates a new Scanner object to read input from the keyboard
b) It imports the Scanner library
c) It closes the input stream
d) It prints input from the user to the console
a) It creates a new Scanner object to read input from the keyboard
In the provided example, what is the role of the inputScanner.close()
method?
a) It reads user input
b) It closes the Scanner object
c) It converts the input into a number
d) It prints a message to the console
b) It closes the Scanner object
What is the purpose of Integer.parseInt(inputStr)
in the code?
a) It converts a string to an integer
b) It converts an integer to a string
c) It prints the input as a string
d) It reads input from the console
a) It converts a string to an integer
What does the program do when the user enters a number between 31 and 256?
public static void main(String[] args) { System.out.print("Enter a number between 31 and 256: "); Scanner inputScanner = new Scanner(System.in); String inputStr = inputScanner.nextLine(); inputScanner.close(); int number = Integer.parseInt(inputStr); System.out.printf("The character for ASCII code %d is %c%n", number, (char) number); }
a) It prints the number along with its corresponding ASCII character
b) It closes the program
c) It multiplies the number by 2
d) It adds the number to an existing list
a) It prints the number along with its corresponding ASCII character
Why is the char
type used in the following line? System.out.printf("The character for ASCII code %d is %c", number, (char) number);
a) To convert the number into its ASCII equivalent
b) To store a numeric value
c) To perform arithmetic operations
d) To represent a string of characters
a) To convert the number into its ASCII equivalent
What does the program do if the user inputs the number 68?
a) It prints ‘The character for ASCII code 68 is D’
b) It prints ‘The character for ASCII code D is 68’
c) It adds 68 to the total
d) It converts 68 to a string
a) It prints ‘The character for ASCII code 68 is D’
What is the role of the System.in
parameter in the Scanner constructor?
a) It tells Scanner to read from the console input
b) It prints input to the console
c) It converts input to a string
d) It adds input to a list
a) It tells Scanner to read from the console input