Input In Java
Name the following
Question 1
a package needed to import scanner class
[Link]
Question 2
a method that accepts a character through scanner object
charAt()
Question 3
a package needed to import Stream Reader Class
[Link]
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 1
to accept an integral value 'p' through Stream Class
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
int p = [Link]([Link]());
Question 2
to accept a fractional value (float) 'm' through Scanner Class
Scanner in = new Scanner([Link]);
float m = [Link]();
Question 3
to accept a character 'd' through Stream Class
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
char d = (char)[Link]();
Question 4
to accept a fraction value 'n' in double data type through Stream Class
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
double n = [Link]([Link]());
Question 5
to accept a word 'wd' through Stream Class
InputStreamReader read = new InputStreamReader([Link]);
BufferedReader in = new BufferedReader(read);
String wd = [Link]();
Question 6
to create a scanner object
Scanner in = new Scanner([Link]);
Differentiate between the following
Question 1
nextInt( ) and nextFloat( ) methods
nextInt( ) nextFloat( )
Accept the next token of input as an int Accept 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 fails to compile and execute. Program compiles and executes but doesn't
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.
Answer the following
Question 1
What do you mean by scanner class?
Scanner class is used to get user input. It is present in [Link] 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:
[Link], [Link] 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
*/
[Link]("a is less than b");
a = 10;
b = 20;
[Link]("The value of a is " + a);
[Link]("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([Link]);
char ch = [Link]().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 [Link];
class RunTime
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int result = 100 / n;
[Link]("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:
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 language. programming logic.
Program fails to compile and execute. Program compiles and executes but doesn't
give the desired output.
Syntax Errors are caught by the compiler. Logical errors need to be found and
corrected by people working on the
program.
Mathematical Library Methods
Choose the correct answer
Question 1
Which of the following is false to find square of a number?
1. [Link](a,2)
2. a*a
3. [Link](a,2) ✓
4. All of the above
Question 2
What type of value is returned by [Link]( )?
1. int
2. float
3. double ✓
4. All
Question 3
Which of the following syntax is true to find the square root of a number?
1. sqrt(a)
2. [Link](a) ✓
3. Squareroot(a)
4. None
Question 4
Name the class that is used for different Mathematical functions.
1. [Link] ✓
2. [Link]
3. [Link]
4. None
Question 5
Give the output of the [Link](x); when x = -9.99
1. -9.99
2. 9.99 ✓
3. 0.99
4. None
Question 6
Give the output of [Link](x); when x = 9.0
1. 3
2. 3.0 ✓
3. 3.00
4. all
Predict the output
Question 1
[Link]([Link](10.24));
Output
3.2
Question 2
[Link]([Link](-99.4));
Output
-99.0
Question 3
[Link]([Link](42.875));
Output
3.5
Question 4
[Link]([Link](-25.5, -12.5));
Output
-25.5
Question 5
[Link]([Link](-0.95));
Output
-0.0
Question 6
[Link]([Link](-18.51));
Output
-19
Question 7
[Link]([Link](-77.66, -87.45));
Output
-77.66
Question 8
[Link]([Link](-0.88));
Output
-1.0
Question 9
[Link]([Link](98.5));
Output
98.0
Question 10
[Link]([Link](65.5));
Output
66.0
Write down the syntax for the following functions
Question 1
To find the smaller between two numbers p and q
Answer
[Link](p, q)
Question 2
To find the absolute value of a number m
Answer
[Link](m)
Question 3
To find the exponent of a number k
Answer
[Link](k)
Question 4
To find the square root of a number d
Answer
[Link](d)
Question 5
To find the rounded-off of a number b
Answer
[Link](b)
Predict the return data type of the following functions
Question 1
[Link]( );
Answer
double
Question 2
[Link]( );
Answer
double
Question 3
[Link]( );
Answer
double
Question 4
[Link]( );
Answer
int or long
Question 5
[Link]( );
Answer
double
Question 6
[Link]( )
Answer
double
Distinguish between them with suitable examples
Question 1
[Link]( ) and [Link]( )
Answer
[Link]( ) [Link]( )
Returns the smallest double value that is Returns the largest double value that is less
greater than or equal to the argument and is than or equal to the argument and is equal to a
equal to a mathematical integer mathematical integer.
double a = [Link](65.5); double b = [Link](65.5);
In this example, a will be assigned the value of In this example, b will be assigned the value of
66.0 as it is the smallest integer greater than 65.0 as it is the largest integer smaller than
65.5. 65.5.
Question 2
[Link]( ) and [Link]( )
Answer
[Link]( ) [Link]( )
Rounds off its argument to the Rounds off its argument to the nearest mathematical
nearest mathematical integer and integer and returns its value as an int or long type. If
returns its value as a double type. argument is float, return type is int, if argument is double,
return type is long.
At mid-point, it returns the integer At mid-point, it returns the higher integer.
that is even
double a = [Link](1.5); long a = [Link](1.5);
double b =[Link](2.5); long b = [Link](2.5);
Both, a and b will have a value of 2.0 a will have a value of 2 and b will have a value of 3