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);
}
}

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());
}
}

3 / 10

What is the primary advantage of using BufferedReader over Scanner in Java?

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);
}
}

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

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]);
}
}

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”);
}
}
}

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);
}
}

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

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));
}
}

Your score is

Scroll to Top