Input in Java Input in Java Scanner Input, Reading Multiple Inputs, BufferedReader Input, Input Validation, Input Using Command-Line Arguments, Input Validation with hasNext(), Mixed Inputs, Reading Character Input 1 / 10 What will happen if the following code is executed? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); System.out.println(x); } } It reads the next integer from the input. It throws a NoSuchElementException if no input is provided. It throws an InputMismatchException if the input is not an integer. Both b and c. nextInt() throws InputMismatchException if input is not an integer and NoSuchElementException if there’s no input. nextInt() throws InputMismatchException if input is not an integer and NoSuchElementException if there’s no input. 2 / 10 What will the following code output? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(“10 20 30”); System.out.println(sc.nextInt() + sc.nextInt() + sc.nextInt()); } } 60 102030 Compilation Error Runtime Error The Scanner reads integers separated by spaces and computes their sum. The Scanner reads integers separated by spaces and computes their sum. 3 / 10 What is the primary advantage of using BufferedReader over Scanner in Java? BufferedReader is faster because it reads input in chunks. BufferedReader can handle both text and binary input. BufferedReader does not require parsing for numeric input. BufferedReader is compatible with all input types without exceptions. BufferedReader uses a buffer to read larger blocks of data at once, improving performance for large inputs BufferedReader uses a buffer to read larger blocks of data at once, improving performance for large inputs 4 / 10 What will the following code output? import java.io.*; public class Test { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); System.out.println(line); } } Compilation Error. It always prints null. It throws a IOException. It prints the first line of input entered by the user. The readLine() method reads a single line of text from the user input. The readLine() method reads a single line of text from the user input. 5 / 10 What will happen if invalid input is provided in the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(“Enter an integer: “); int x = sc.nextInt(); System.out.println(“You entered: ” + x); } } Input: abc It enters an infinite loop. It reads abc as 0. It throws an InputMismatchException. It compiles but gives unexpected output. nextInt() expects an integer, and non-integer input results in an exception. nextInt() expects an integer, and non-integer input results in an exception. 6 / 10 What will the following code print if executed with java Test 10 20? public class Test { public static void main(String[] args) { System.out.println(args[0] + args[1]); } } 30 1020 Compilation Error Runtime Error Command-line arguments are treated as String values, so args[0] + args[1] concatenates the two strings Command-line arguments are treated as String values, so args[0] + args[1] concatenates the two strings 7 / 10 What is the output of the following code if the input is 123? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); if (sc.hasNextInt()) { System.out.println(sc.nextInt()); } else { System.out.println(“Invalid input”); } } } Invalid input 123 0 Compilation Error hasNextInt() checks whether the next token is an integer. If true, it reads and prints the integer. hasNextInt() checks whether the next token is an integer. If true, it reads and prints the integer. 8 / 10 What will the following code output for input 10 Hello 20? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextInt(); String y = sc.next(); int z = sc.nextInt(); System.out.println(x + ” ” + y + ” ” + z); } } 10 and then Hello 20 on the next line. Compilation Error 10 Hello 20 Runtime Error. The Scanner correctly parses the input as an integer, a string, and another integer in order. The Scanner correctly parses the input as an integer, a string, and another integer in order. 9 / 10 What is the output of the following code? import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); System.out.println(ch); } } Input: Java J Java Compilation Error Runtime Error sc.next() reads the first token as a string, and charAt(0) extracts the first character.) sc.next() reads the first token as a string, and charAt(0) extracts the first character.) 10 / 10 What will the following program output if the user inputs “45 10”? import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print(“Enter two numbers: “); int num1 = sc.nextInt(); int num2 = sc.nextInt(); System.out.println(“Sum: ” + (num1 + num2)); System.out.println(“Difference: ” + (num1 – num2)); System.out.println(“Product: ” + (num1 * num2)); System.out.println(“Quotient: ” + (num1 / num2)); } } Sum: 45 Difference: 35 Product: 450 Quotient: 4.5 Sum: 55 Difference: -35 Product: 450 Quotient: 4 Sum: 55 Difference: 35 Product: 450 Quotient: 4 Sum: 45 Difference: 35 Product: 450 Quotient: 4.5 The program reads two integers, performs arithmetic operations, and prints the results. Integer division truncates the result to an integer. The program reads two integers, performs arithmetic operations, and prints the results. Integer division truncates the result to an integer. Your score is By WordPress Quiz plugin