Input in Java – ICSE Class 9
Highlights
• Java uses the Scanner class (in [Link] package) to read input from
the user.
• Common input types: integer, double, string (word/line), and boolean.
• Syntax to import Scanner: import [Link];
• Create Scanner object: Scanner sc = new Scanner([Link]);
• Use methods: nextInt(), nextDouble(), next(), nextLine(),
nextBoolean() for reading respective types.
• Always prompt the user so they know what to enter!
• Remember to close Scanner (good practice), e.g., [Link]();
• Mixing nextInt() and nextLine() may need handling extra line breaks.
20 Multiple Choice Questions (with answers)
No. Question Options Answer
1 Which package contains the Scanner a) [Link] b) [Link] c) b)
class? [Link] d) [Link] [Link]
2 How do you import the Scanner a) import a)
class? [Link]; b) im-
import [Link]; port
[Link];
3 Which object is used to take input a) Input b) Scanner c) b)
from the user? Read d) Stream Scan-
ner
4 To create a Scanner object to read a) Scanner sc = new a)
from the keyboard, which syntax is Scanner([Link]); b) Scan-
correct? Scanner sc = new ner
Scanner([Link]); sc =
new
Scan-
ner([Link]);
5 Which method is used to take integer a) next() b) nextInt() c) b)
input? getInt() d) inputInt() nex-
tInt()
6 Which method is used to read a a) nextLine() b) read() c)
single word string? c) next() d) input() next()
7 Which method reads an entire line a) next() b) nextLine() b)
including spaces? c) read() d) line() nextLine()
8 What is the return type of a) int b) double c) float b)
nextDouble()? d) String dou-
ble
1
No. Question Options Answer
9 What will happen if input type does a) Program gives error c)
not match method called? b) Program crashes c) Ex-
Exception thrown d) cep-
None tion
thrown
10 To close a Scanner object, which a) [Link]() b) close() c) a)
method is used? scannerStop() d) [Link]()
[Link]()
11 Scanner class reads input from which a) [Link] b) b)
stream for standard input? [Link] c) [Link] Sys-
d) None [Link]
12 Which of the following is not a a) nextDouble() b) d)
Scanner method? nextFloat() c) nextWord()
nextByte() d)
nextWord()
13 Can Scanner be used to read input a) Yes b) No a)
from files? Yes
14 What is the default delimiter for a) Comma b) Space c) b)
Scanner? Newline d) Tab Space
15 Which method reads a boolean a) nextBoolean() b) a)
input? getBoolean() c) next-
readBoolean() d) bool() Boolean()
16 Which method returns the next token a) nextLine() b) next() b)
as a String? c) getLine() d) input() next()
17 What is the output of [Link]() a) 42 b) “42” c) 4 d) a) 42
if the input is 42? Error
18 Scanner is a part of which Java a) JDK 1.0 b) JDK 1.5 b)
version onwards? c) JDK 1.2 d) JDK 1.8 JDK
1.5
19 What is the purpose of [Link] a) To output data b) To b)
while creating Scanner? input data c) To read To
files d) None in-
put
data
20 Which of the following is correct to a) next() b) nextLine() b)
read a String with spaces? c) nextString() d) nextLine()
getString()
2
10 Beginner Coding Questions with Solutions
1. Integer Input
import [Link];
public class InputInteger {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int num = [Link]();
[Link]("You entered: " + num);
[Link]();
}
}
2. Double Input
import [Link];
public class InputDouble {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a decimal: ");
double d = [Link]();
[Link]("You entered: " + d);
[Link]();
}
}
3. Single Word Input
import [Link];
public class InputWord {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String word = [Link]();
[Link]("Word is: " + word);
[Link]();
}
}
4. Full Line Input
import [Link];
public class InputLine {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
3
[Link]("Enter a line: ");
String line = [Link]();
[Link]("Line entered: " + line);
[Link]();
}
}
5. Boolean Input
import [Link];
public class InputBoolean {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter true or false: ");
boolean b = [Link]();
[Link]("Boolean entered: " + b);
[Link]();
}
}
6. Add Two Numbers
import [Link];
public class SumTwoIntegers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
int sum = a + b;
[Link]("Sum is: " + sum);
[Link]();
}
}
7. Even or Odd
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter an integer: ");
int n = [Link]();
if(n % 2 == 0) {
[Link](n + " is even");
} else {
4
[Link](n + " is odd");
}
[Link]();
}
}
8. Average of Two Doubles
import [Link];
public class AverageOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first decimal number: ");
double x = [Link]();
[Link]("Enter second decimal number: ");
double y = [Link]();
double avg = (x + y) / 2;
[Link]("Average is: " + avg);
[Link]();
}
}
9. Greet User
import [Link];
public class GreetUser {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
[Link]();
}
}
10. Age Check (Major/Minor)
import [Link];
public class AgeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();
if(age >= 18) {
[Link]("You are an adult.");
} else {
[Link]("You are a minor.");
5
}
[Link]();
}
}
5 Additional More Challenging Programs (with Solutions)
11. Swap Two Numbers (without third variable)
import [Link];
public class SwapWithoutTemp {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
// Swapping
a = a + b;
b = a - b;
a = a - b;
[Link]("After swap: a = " + a + ", b = " + b);
[Link]();
}
}
12. Check Palindrome Word
import [Link];
public class PalindromeWord {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a word: ");
String word = [Link]();
String rev = "";
for(int i = [Link]()-1; i >=0; i--) {
rev += [Link](i);
}
if([Link](rev)) {
[Link](word+" is a palindrome.");
} else {
[Link](word+" is NOT a palindrome.");
}
[Link]();
}
6
}
13. Time in Seconds to h:m:s converter
import [Link];
public class TimeConverter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter seconds: ");
int s = [Link]();
int h = s / 3600;
int m = (s % 3600) / 60;
int sec = s % 60;
[Link](h+" hour "+m+" minute "+sec+" second");
[Link]();
}
}
14. Mixed Data Input & Output
import [Link];
public class MixedInput {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your roll number: ");
int roll = [Link]();
[Link](); // Clear buffer
[Link]("Enter your school: ");
String school = [Link]();
[Link]("Hello, "+name+". Roll: "+roll+", School: "+school);
[Link]();
}
}
15. Input N integers and Calculate the Sum
import [Link];
public class SumN {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("How many numbers? ");
int n = [Link]();
int sum = 0;
for(int i=0; i<n; i++) {
[Link]("Enter number " + (i+1) + ": ");
7
sum += [Link]();
}
[Link]("Sum is: " + sum);
[Link]();
}
}