KEMBAR78
Input Chapter Class9 | PDF | Integer (Computer Science) | Java (Programming Language)
0% found this document useful (0 votes)
14 views4 pages

Input Chapter Class9

The document provides an overview of input handling in Java, detailing the Scanner class and InputStreamReader for user input. It includes syntax examples for accepting various data types, explanations of key concepts like packages and comments, and distinctions between testing and debugging, as well as error types. Additionally, it covers initialization of variables and run-time errors with examples.

Uploaded by

suneerv252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Input Chapter Class9

The document provides an overview of input handling in Java, detailing the Scanner class and InputStreamReader for user input. It includes syntax examples for accepting various data types, explanations of key concepts like packages and comments, and distinctions between testing and debugging, as well as error types. Additionally, it covers initialization of variables and run-time errors with examples.

Uploaded by

suneerv252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ICSE COMPUTER APPLICATION

CHAPTER – INPUT IN JAVA

QUESTION ANSWERS

I. Name the following

1. A package needed to import scanner class


java.util
2. A method that accepts a character through scanner object
charAt()
3. A package needed to import Stream Reader Class
java.io
4. A method to accept an exponential value through scanner object
nextDouble()
5. A method that accepts an integer token through scanner object
nextInt()

II. Write down the syntax with reference to Java Programming

1. To accept an integral value 'p' through Stream Class

InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
int p = Integer.parseInt(in.readLine());

2. To accept a fractional value (float) 'm' through Scanner Class

Scanner in = new Scanner(System.in);


float m = in.nextFloat();

3. To accept a character 'd' through Stream Class

InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
char d = (char)in.read();
4. To accept a fraction value 'n' in double data type through Stream Class

InputStreamReader read = new InputStreamReader(System.in);


BufferedReader in = new BufferedReader(read);
double n = Double.parseDouble(in.readLine());
5. To accept a word 'wd' through Stream Class
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
String wd = in.readLine();

6. To create a scanner object


Scanner in = new Scanner(System.in);

III. Answers the following:

1. What do you mean by scanner class?


Scanner class is used to get user input. It is present in java.util package.

2. What are the different ways to give inputs in a Java Program?


Java provides 4 ways to read user input from console:

a) By using assignment statement


b) By using function argument
c) By using Input Stream Reader class
d) By using Scanner class

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.

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.
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.

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.

For example:
int a = 1, b = 20;
if (a < b) {
System.out.println("a is less than b");
}
else
{
System.out.println("b is less than a”);}

7.What is initialization of variables? Explain with a help of an example.


Initialization is the assignment of an initial value for a variable during declaration.
For example, int a=5; is a statement where 5 is stored initially into a memory location
represented
with the name a.+

8. Distinguish between:
(a) Testing and Debugging

Testing Debugging

Testing is a process in which a program Debugging is a process in which the


is validated. errors in the program are removed.

It is a positive activity that seeks to It is a negative activity in a sense that it


demonstrate that the program is is centered around elimination of
correct known errors or bugs.

Testing is complete when all desired Debugging is finished when there are
verifications against specification have no errors and the program is ready for
been performed. execution.
(b) Syntax error and Logical error

Syntax Error Logical Error

Syntax Errors occur when rules of


Logical Errors occur due to our
grammar of the programming language
mistakes in programming logic.
are not followed.

Program compiles and executes but


Program fails to compile and execute.
doesn't give the desired output.

Logical errors need to be found and


Syntax Errors are caught by the
corrected by people working on the
compiler.
program.

(c) nextInt() and nextFloat()

nextInt() nextFloat()
The nextInt() method is used to The nextFloat() method is used to
input integers through keyboard in input fractional numbers (float)
Scanner class. through keyboard in Scanner class.

9. What are comments? Name the different types of comments used in Java.
A comment is a programmer-readable explanation or annotation in the source code
of a computer program. The different types of comments in Java are:
• Single line comment
• Multiline comment

10. What are run-time errors? Give two examples.


An error that occurs during the execution of a program is called run time error.
The run-time errors are:
• Dividing an integer by zero.
• Accessing an element that is out of bounds of an array.

You might also like