Reading input Flashcards

1
Q

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

A

b) To read user input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

a) It creates a new Scanner object to read input from the keyboard

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

b) It closes the Scanner object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

a) It converts a string to an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

a) It prints the number along with its corresponding ASCII character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

a) To convert the number into its ASCII equivalent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

a) It prints ‘The character for ASCII code 68 is D’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

a) It tells Scanner to read from the console input

How well did you know this?
1
Not at all
2
3
4
5
Perfectly