Class 10th Ch.1 Part V(B) Inputs in Java
Class 10th Ch.1 Part V(B) Inputs in Java
1 REVISION OF CLASS IX
PART V(b): INPUTS IN JAVA .
REVIEW INSIGHT
Q.1 What is the purpose of the following function?
Ans: (a) nextInt( )function is used to accept an integer value from the user at the time of
execution.
(b) nextLine( )function accepts a string including spaces at the time of execution.
Q.2 Why do you need to mention the keyword ‘import’ in the program?
Ans: The keyword ‘import’ allows to include a package in the program.
A java program is designed to displayed all natural number from 1 to 10 . Identify the error in
Java program depicted in the above illustration.
a. Logical Error c. Syntax Error
b. Compilation Error d. Runtime Error
2. Which of the following is the right way to use a comment in Java program?
1. /* comment */
2. /* comment
3. // comment //
4. */ comment */
6. Which of the following method will accept a string by using scanner object?
1. next( )
2. next.read( )
3. next.String( )
4. next.char( )
2. (char)(in.read());
Answer
This statement reads a character using Stream class and casts it explicitly into char data
type.
3. next();
Answer
next() method accepts a string from the user as a word using Scanner class.
5.nextLine();
Answer
nextLine() method accepts a string from the user as a line of text using Scanner class.
4. What are the different types of errors that take place during the execution of a
program?
Answer
Logical errors and Run-Time errors occur during the execution of the program.
3. 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
Answer
import java.util.Scanner;
System.out.println(hrs
+ " Hours "
+ mins
+ " Minutes "
+ secs
+ " Seconds");
}
}
4. 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:
1. the total time taken by the driver
2. the average speed during the whole journey
[Hint: average speed = total distance / total time]
Answer
public class Journey
{
public static void main(String args[]) {
5. 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
Answer
import java.util.Scanner;
if (firstNum == secondNum) {
System.out.println("Invalid Input. Numbers are equal.");
return;
}
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;
Answer
import java.util.Scanner;
public class CompoundInterest
{
public static void main(String args[]) {
7. 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.
Answer
import java.util.Scanner;