Class:9
Subject: Computer Applications
Chapter 5: Input in Java
Name the following
Question 1
a package needed to import scanner class
java.util
Question 2
a method that accepts a character through scanner object
charAt()
Question 3
a package needed to import Stream Reader Class
java.io
Question 4
a method to accept an exponential value through scanner object
nextDouble()
Question 5
a method that accepts an integer token through scanner object
nextInt()
Write down the syntax with reference to Java
Programming
Question 2
to accept a fractional value (float) 'm' through Scanner Class
Scanner in = new Scanner(System.in);
float m = in.nextFloat();
Question 6
to create a scanner object
Scanner in = new Scanner(System.in);
Differentiate between the following
Question 1
nextInt( ) and nextFloat( ) methods
nextInt( ) nextFloat( )
Scans the next token of input as an int Scans the next token of input as a float
Question 2
Syntax and logical errors
Syntax Errors Logical Errors
Syntax Errors occur when we violate the rules of Logical Errors occur due to our mistakes in
writing the statements of the programming language. programming logic.
Program compiles and executes but doesn't
Program fails to compile and execute.
give the desired output.
Logic errors need to be found and corrected
Syntax Errors are caught by the compiler.
by the people working on the program.
Answer the following
Question 1
What do you mean by scanner class?
Scanner class is used to get user input. It is present in java.util package.
Question 2
What are the different ways to give inputs in a Java Program?
Java provides the following ways to give input in a program:
1. Using Function Argument.
2. Using InputStreamReader class.
3. Using Scanner class.
4. Using Command Line Arguments.
Question 3
What is the use of the keyword import?
import keyword is used to import built-in and user-defined packages into our Java
program.
Question 4
What is a package? Give an example.
In Java, a package is used to group related classes. Packages are of 2 types:
1. Built-In packages — These are provided by Java API
2. User-Defined packages — These are created by the programmers to
efficiently structure their code.
java.util, java.lang are a couple of examples of built-in packages.
Question 5
What is the use of the keyword 'import' in Java programming?
import keyword is used to import built-in and user-defined packages into our Java
program.
Question 6
What is a compound statement? Give an example.
Two or more statements can be grouped together by enclosing them between
opening and closing curly braces. Such a group of statements is called a compound
statement.
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
System.out.println("a is less than b");
a = 10;
b = 20;
System.out.println("The value of a is " + a);
System.out.println("The value of b is " + b);
Question 7
Write down the syntax to input a character through scanner class with an example.
Syntax:
char <variable name> = <Scanner Object>.next().charAt(0);
Example:
Scanner in = new Scanner(System.in);
char ch = in.next().charAt(0);
Question 8
What do you understand by 'Run Time' error? Explain with an example
Errors that occur during the execution of the program primarily due to the state of
the program which can only be resolved at runtime are called Run Time errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all non-zero values of n entered by the user. When
the user enters zero, a run-time error will occur as the program is trying to perform
an illegal mathematical operation of division by 0. When we are compiling the
program, we cannot say if division by 0 error will occur or not. It entirely depends
on the state of the program at run-time.
Question 9
What are the different types of errors that take place during the execution of a program?
Logical errors and Run-Time errors occur during the execution of the program.
Question 10
Distinguish between:
(a) Testing and Debugging
Testing Debugging
In the process of Testing, we check if the program is In the process of Debugging, we
working as expected and find out the errors if it is not correct the errors that were found
giving the expected output. during testing.
(b) Syntax error and Logical error
Syntax Error Logical Error
Syntax Errors occur when we violate the rules of
Logical Errors occur due to our mistakes in
writing the statements of the programming
programming logic.
language.
Program compiles and executes but doesn't
Program fails to compile and execute.
give the desired output.
Logical errors need to be found and
Syntax Errors are caught by the compiler. corrected by people working on the
program.