0% found this document useful (0 votes)
5 views

Input in Java

The document provides an overview of input in Java programming, including definitions of key concepts such as initialization of variables, the Scanner class, and the import keyword. It explains different types of errors in Java, including syntax and run-time errors, and discusses the use of comments and methods for reading various data types. Additionally, it differentiates between various input methods and error types, offering examples for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Input in Java

The document provides an overview of input in Java programming, including definitions of key concepts such as initialization of variables, the Scanner class, and the import keyword. It explains different types of errors in Java, including syntax and run-time errors, and discusses the use of comments and methods for reading various data types. Additionally, it differentiates between various input methods and error types, offering examples for clarity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Input in Java

1. In programming what is an Input?


Programming involves taking in some data, processing it using some instructions and giving some meaningful result.
Taking in some data is called Input.

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

3. What is the function of a Scanner class? In which package does it belong?


The scanner class is a class in java.util, which allows the user to read values of various types, from either keyboard
or a file.

4. What is the use of the keyword import?


import keyword is used to import built-in and user-defined packages into our Java program.

5. What is a package? Give an example.


In Java, a package is used to group related classes. Packages are of 2 types:
Built-In packages — These are provided by Java API
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.

6. What do you understand by parameterized input?


The process of passing values to arguments in a method during execution is called parameterized input.

7. What is a bug? What is debugging?


A bug is an error, flaw, failure or fault in a program or system that causes it to produce an incorrect or unexpected
result, or to behave in unintended ways. The process of removing a bug is called debugging.

8. What is the different type of errors that may occur in a Java program?
Errors in a program in Java can broadly be classified into three categories: • Syntax errors • Logical errors • Run-time
errors

9. What are syntax errors? Give two examples.


A syntax error is an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular
program. The common syntax errors are:
• Missing semicolon
• Mismatched braces in classes and methods.

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.

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

12. Differentiate between nextInt( ) and nextFloat( ) methods.


nextInt( ) nextFloat( )
Scans the next token of input as an int Scans the next token of input as a float

1
13. Explain different input types of Java Sanner class.
Method Description Example
nextShort() Reads a short value from the user Scanner sc = new Scanner (System.in);
short a=sc.nextShort();
System.out.print(a);
nextInt( ) Reads a int value from the user Scanner sc = new Scanner (System.in);
int a=sc.nextInt();
System.out.print(a);
nextLong( ) Reads a long value from the user Scanner sc = new Scanner (System.in);
long a=sc.nextLong();
System.out.print(a);
nextFloat ( ) Reads a float value from the user Scanner sc = new Scanner (System.in);
float a=sc.nextFloat();
System.out.print(a);
nextDouble( ) Reads a double value from the user Scanner sc = new Scanner (System.in);
double a=sc.nextDouble();
System.out.print(a);
next( ) It read input from the input device till the Scanner sc = new Scanner (System.in);
space character. String a=sc.next(); // computer science
System.out.print(a); // computer
nextLine( ) It read input from the input device till the Scanner sc = new Scanner (System.in);
line change. String a=sc.nextLine(); // computer science
System.out.print(a); // computer science
next ( ) .charAt(0) next() function returns the next Scanner sc = new Scanner (System.in);
token/word in the input as a string and char a=sc.next().charAt(0); // computer science
charAt(0) function returns the first System.out.print(a); // c
character in that string,

14. Differentiate between Syntax and logical errors.


Syntax errors logical errors
Syntax Errors occur when we violate the rules of writing Logical Errors occur due to our mistakes in
the statements of the programming language. programming logic.
Program fails to compile and execute. Program compiles and executes but does not give the
desired output.
Syntax Errors are caught by the compiler. Logic errors need to be found and corrected by the
people working on the program.

15. Difference between Single-Line and Multi-Line comments in Java.


Single-Line Multi-Line
Single-line comments in Java are used to comment out a Multi-line comments in Java are used to comment out
single line or a part of it. blocks of text spanning multiple lines.
They begin with two forward slashes (//) and extend to They start with /* and end with */. Everything between
the end of the line. these markers is considered a comment and is ignored
by the Java compiler.
Single-line comments are helpful for brief explanations Multi-line comments help provide detailed explanations
or notes relevant to the local code. or temporarily disable a block of code during debugging.
Syntax Syntax
// comment text /*
This is a multi-line comment.
It can span several lines.
The Java compiler ignores everything in this block.
*/

2
16. Difference between the next() and the nextLine() method:
next() nextLine()
It read input from the input device till the space It read input from the input device till the line change.
character.
It cannot read those words having space in it. It can read those words having space in it.
Syntax to scan input: Syntax to scan input:
Scanner.next() Scanner.nextLine()

You might also like