Class 10 Computer Applications – Chapter INPUT IN JAVA (Scanner Class)
1. Which package is required to use the Scanner class?
• Answer: [Link] package.
2. Write the statement to import the Scanner class.
• Answer: import [Link];
3. How do you create a Scanner object?
• Answer: Scanner sc = new Scanner([Link]);
4. Which Scanner method is used to input an integer?
• Answer: nextInt()
5. Which Scanner method is used to input a word?
• Answer: next()
6. Which Scanner method is used to input a full sentence?
• Answer: nextLine()
7. Write the output of:
[Link]("Enter number:"); // no error
(Only prints the text)
• Answer: Enter number:
8. Why do we use [Link] in Scanner?
• Answer: [Link] represents standard keyboard input.
SECTION B – Programs
1. Accept a name and age from the user and display them.
import [Link].*;
class NameAge {
public static void main() {
Scanner sc = new Scanner([Link]);
[Link]("Enter name:");
String name = [Link]();
[Link]("Enter age:");
int age = [Link]();
[Link](name + " : " + age);
}
}
2. Accept two integers and display their sum.
import [Link].*;
class SumInput {
public static void main() {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Sum = " + (a + b));
}
}
3. Accept a full sentence using nextLine() and count characters.
import [Link].*;
class SentenceLength {
public static void main() {
Scanner sc = new Scanner([Link]);
String s = [Link]();
[Link]("Length = " + [Link]());
}
}
4. Accept radius (double) and display area of circle.
import [Link].*;
class CircleArea {
public static void main() {
Scanner sc = new Scanner([Link]);
double r = [Link]();
double area = 3.14 * r * r;
[Link]("Area = " + area);
}
}