Chapter 6 Input in Java
Chapter 6 Input in Java
Input in Java
Class 10 - APC Understanding Computer Applications
with BlueJ
Question 1
Which of the following is the right way to use a comment in Java program?
1. /* comment */
2. /* comment
3. // comment //
4. */ comment */
Answer
/* comment */
Question 2
1. Syntax
2. Logical
3. Runtime
4. Executional
Answer
Runtime
Question 3
1. in.read( )
2. (char)(in.read( ))
3. in.readline( )
4. character.read( )
Answer
(char)(in.read( ))
Which of the following method will accept a string by using scanner object?
1. next( )
2. next.read( )
3. next.String( )
4. next.char( )
Answer
next( )
Question 5
1. nextline( )
2. nextLine( )
3. Nextline( )
4. NextLine( )
Answer
nextLine( )
Question 1
Question 2
Question 3
Question 4
Question 5
Assigning a value of 24.3 to a variable that is defined as int type is syntax error.
Question 6
Question 7
Scanner ob = new Scanner(System.in).
Question 8
The command line argument accepts the data value as an array of Strings.
Question 1
Integer.parseInt(in.readLine());
Answer
This statement accepts an integer from the user using Stream class and parses it into int data type using
Integer.parseInt() method.
Question 2
(char)(in.read());
Answer
This statement reads a character using Stream class and casts it explicitly into char data type.
Question 3
next();
Answer
next() method accepts a string from the user as a word using Scanner class.
Question 4
Answer
The given function accepts an integer value from the user at the time of execution of the program. The value for
variable b must be provided as arguments to the main() function.
Question 5
nextLine();
Answer
nextLine() method accepts a string from the user as a line of text using Scanner class.
Question 1
Answer
Question 2
Answer
Question 3
Answer
'import' keyword is used to import built-in and user-defined packages into our Java programs.
Question 4
Answer
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 Runtime 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 5
What are the different types of errors that take place during the execution of a program? Name them.
Answer
Logical errors and Run-Time errors occur during the execution of the program.
Question 6
Give two differences between Syntax error and Logical error.
Answer
Syntax errors occur when we violate the rules of writing the Logical errors occur due to our mistakes in
statements of the programming language. programming logic.
Question 1
In an election, there are two candidates X and Y. On the election day, 80% of the voters go for polling, out of which
60% vote for X. Write a program to take the number of voters as input and calculate:
import java.util.Scanner;
Question 2
A shopkeeper offers 10% discount on the printed price of a mobile phone. However, a customer has to pay 9% GST
on the remaining amount. Write a program in Java to calculate the amount to be paid by the customer taking printed
price as an input.
import java.util.Scanner;
Question 3
A man spends (1/2) of his salary on food, (1/15) on rent, (1/10) on miscellaneous activities. Rest of the salary is his
saving. Write a program to calculate and display the following:
import java.util.Scanner;
Question 4
Write a program to input time in seconds. Display the time after converting them into hours, minutes and seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import java.util.Scanner;
System.out.println(hrs
+ " Hours "
+ mins
+ " Minutes "
+ secs
+ " Seconds");
}
}
Output
Question 5
The driver took a drive to a town 240 km at a speed of 60 km/h. Later in the evening, he drove back at 20 km/h less
than the usual speed. Write a program to calculate:
Question 6
Write a program to input two unequal numbers. Display the numbers after swapping their values in the variables
without using a third variable.
Sample Input: a = 76, b = 65
Sample Output: a = 65, b = 76
import java.util.Scanner;
if (firstNum == secondNum) {
System.out.println("Invalid Input. Numbers are equal.");
return;
}
Question 7
A certain amount of money is invested for 3 years at the rate of 6%, 8% and 10% per annum compounded annually.
Write a program to calculate:
import java.util.Scanner;
Question 8
The co-ordinates of two points A and B on a straight line are given as (x1,y1) and (x2,y2). Write a program to calculate
the slope (m) of the line by using formula:
Slope = (y2 - y1) / (x2 - x1)
Take the co-ordinates (x1,y1) and (x2,y2) as input.
import java.util.Scanner;
Question 9
A dealer allows his customers a discount of 25% and still gains 25%. Write a program to input the cost of an article
and display its selling price and marked price.
import java.util.*;