0% found this document useful (0 votes)
11 views8 pages

Class 10th Ch.1 Part V(B) Inputs in Java

The document provides a comprehensive review of Java programming concepts, focusing on input handling using the Scanner class, error types, and common programming practices. It includes explanations of various functions, error handling, and practical exercises for input and output operations. Additionally, it contains multiple-choice questions, fill-in-the-blank exercises, and programming tasks to reinforce learning.

Uploaded by

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

Class 10th Ch.1 Part V(B) Inputs in Java

The document provides a comprehensive review of Java programming concepts, focusing on input handling using the Scanner class, error types, and common programming practices. It includes explanations of various functions, error handling, and practical exercises for input and output operations. Additionally, it contains multiple-choice questions, fill-in-the-blank exercises, and programming tasks to reinforce learning.

Uploaded by

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

CH.

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.

Q.3 What happens when


(A) the values of a variable is divided by zero?
Ans: when a value is divided by zero, ‘Runtime’ error occurs.
(B) semicolon is missing in the input / output statement
Ans: semicolon is missing in the input / output statement , then ‘Syntax’ error occur.

Q.4 Name the method of scanner class that is used to input:


(a) a decimal number from the consol.
Ans: nextFloat( ) or nextDouble( )
(b) a word or a line of text from the keyboard.
Ans: next( ) or nextLine( )

Q.5 What are the various types of error in java?


Ans: There are three types of error in Java:
i.Syntax Error ii.Runtime Error iii. Logical Error

Q.6 Assertion and Reasoning based question :


Assertion(A): Comments are executable statement in any programming language .
Reasoning(R): They make the program more informative for the user.
Based on the above discussion, choose the appropriate statement from the given option
given below :
(A) Both Assertion(A) and Reason(R) are true and Reason(R) is the correct explanation of
Assertion (A).
(B) Both Assertion(A) and Reason(R) are true and Reason(R) is not correct explanation of
Assertion (A).
(C) Assertion(A) is true and Reason(R) is false.
(D) Assertion(A) is false and Reason(R) is true

Q.7 Case study based question:


Scanner class is the latest development in java which allows the user to read values of
various data types. It is defined within a package that must be included in the program to
avail its inbuilt facilities . An object of scanner class must be created to accept the values of
a specific data types . One of the advantage of Scanner class over InputStreamReader is that
the values can be entered using less number of statements .
Based on the above discussion , answer the following questions:
(A) While designing Java program, the keyword _____________ is used to include the
package of scanner class.
i. Include ii. Import iii. Introduce iv. insert
(B) What will be filled in the statement given below ?
Scanner sc = ………………… Scanner (System.in)
i. Key ii. def iii. new iv. Scan
(C) Which of the following package will you use to avail scanner class?
i. Java.Utility ii. Java.util iii. Java.io iv. Java.scanner.class
(D)Which of the following statement is true for a scanner class?
i. It doesn’t accept float type values as an input.
ii.It accepts a word but not a sentence as an input.
iii.It doesn’t accept a single character as an input.
iv. It accepts numeric as well as alphanumeric values as an input.
EXERCISE
Q.1 Multiple choice questions:
1. public class Display
{
public static void main(String args[ ])
{
int a= 1
for(a = 1, a <= 10, a++);
{
System.out.println(a);
}
}
}

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 */

3. Given: int p=55/0;


Name the type of error in the given statement:
1. Syntax
2. Logical
3. Runtime
4. Executional

4.Missing a semicolon in a statement is called …………….. error.


a. Logical b. Syntax c. Runtime d. Executional

5. The …………… method will accept a character using stream class.


1. in.read( )
2. (char)(in.read( ))
3. in.readline( )
4. character.read( )

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( )

7. Which of the following is a method of Java Scanner class?


1. nextline( )
2. nextLine( )
3. Nextline( )
4. NextLine( )

8.Name the type of error in the given statement below :


double x; y; z;
a. Logical error b. Syntax error c. Runtime error d.No
error
9. Read the following text and choose the correct answer:
This is one of the methods to accept the value from the user at the time of execution of the
program.
The variable whose value are to be input must be provided as arguments to the main ( )
function.
Which of the following methods of java programming is discussing about ?
a.Using Scanner Class b. Using Function Argument
c. Using Command Ling Argument d. Using InputStream Class Reader.

Q.2 Fill in the blanks


1. The package needed to import StreamReader class is java.io.
2. nextInt( ) function accepts an integer by using scanner object.
3. The package needed to import Scanner class is java.util.
4. The keyword import is used to include a package in a program.
5. Assigning a value of 24.3 to a variable that is defined as int type is syntax error.
6. input statement is used to accept value at run time.
7. Scanner ob = new Scanner(System.in);
8. The command line argument accepts the data value as an array of Strings.

Q.3 Explain the following function:


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.

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.

4. public static void main(int b)


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.

5.nextLine();
Answer
nextLine() method accepts a string from the user as a line of text using Scanner class.

Q.4 Answer the following


1. What are the different ways to give input in a Java Program?
Answer
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.
2. What is the use of the keyword 'import' in Java programming?
Answer
import keyword is used to import built-in and user-defined packages into our Java program.

3. What is a Runtime error? Explain with an example.


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.

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.

5. Distinguish between Syntax error and Logical error.


Answer

Syntax Errors Logical Errors

Syntax Errors occur when we violate the rules


Logical Errors occur due to our
of writing the statements of the programming
mistakes in programming logic.
language.

Program compiles and executes but


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

Logic errors need to be found and


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

Q.5 Unsolved Java programs:


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:
1. number of votes received by X
2. number of votes received by Y
Answer
import java.util.Scanner;
public class Polling
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of voters: ");
long totalVoters = in.nextLong();
long votedVoters = Math.round(0.8 * totalVoters);
long xVotes = Math.round(0.6 * votedVoters);
long yVotes = votedVoters - xVotes;

System.out.println("Total Votes: " + votedVoters);


System.out.println("Votes of X: " + xVotes);
System.out.println("Votes of Y: " + yVotes);
}
}
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.
Answer
import java.util.Scanner;

public class Invoice


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the MRP: ");


double mrp = in.nextDouble();

double priceAfterDiscount = mrp - (0.1 * mrp);


double priceWithGST = priceAfterDiscount + (priceAfterDiscount * 0.09);

System.out.println("Price after 10% discount and 9% GST: " + priceWithGST);


}
}

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;

public class Time


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Time in seconds: ");


int inputTime = in.nextInt();

int hrs = inputTime / 3600;


int mins = (inputTime % 3600) / 60;
int secs = (inputTime % 3600) % 60;

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[]) {

int distance = 240;


float speed = 60.0f;
float returnSpeed = speed - 20;

float time2Reach = distance / speed;


float time2Return = distance / returnSpeed;
float totalTime = time2Reach + time2Return;

float avgSpeed = (distance * 2) / totalTime;

System.out.println("Total time: " + totalTime);


System.out.println("Average speed: " + avgSpeed);
}
}

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;

public class NumberSwap


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.println("Please provide two unequal numbers");

System.out.print("Enter the first number: ");


int firstNum = in.nextInt();

System.out.print("Enter the second number: ");


int secondNum = in.nextInt();

if (firstNum == secondNum) {
System.out.println("Invalid Input. Numbers are equal.");
return;
}
firstNum = firstNum + secondNum;
secondNum = firstNum - secondNum;
firstNum = firstNum - secondNum;

System.out.println("First Number: " + firstNum);


System.out.println("Second Number: " + secondNum);
}
}
6. 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:
1. the amount after 3 years.
2. the compound interest after 3 years.
Accept certain amount of money (Principal) as an input.
Hint: A = P * (1 + (R1 / 100))T * (1 + (R2 / 100))T * (1 + (R3 / 100))T and CI = A - P

Answer
import java.util.Scanner;
public class CompoundInterest
{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter the principal: ");


double principal = in.nextDouble();
System.out.println(principal);

float r1 = 6.0f, r2 = 8.0f, r3 = 10.0f;

double amount = principal * (1 + (r1 / 100)) * (1 + (r2 / 100)) * (1 + (r3 / 100));


double ci = amount - principal;

System.out.println("Amount after 3 years: " + amount);


System.out.println("Compound Interest: " + ci);
}
}

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;

public class LineSlope


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print("Enter x coordinate of point A: ");


int x1 = in.nextInt();

System.out.print("Enter y coordinate of point A: ");


int y1 = in.nextInt();
System.out.print("Enter x coordinate of point B: ");
int x2 = in.nextInt();
System.out.print("Enter y coordinate of point B: ");
int y2 = in.nextInt();
float lineSlope = (y2 - y1) / (float)(x2 - x1);

System.out.println("Slope of line: " + lineSlope);


}
}
8. 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.
Hint: SP = ((100 + p%) / 100) * CP and MP = (100 / (100 - d%)) * SP
Answer
import java.util.*;

public class CalculatePrice


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

System.out.print("Enter cost price of article:");


double cp = in.nextDouble();

int disc = 25, profit = 25;


double sp = 0.0, mp = 0.0;

sp = ((100 + profit) / 100.0) * cp;


System.out.println("Selling price of article = Rs. " + sp);

mp = (100.0 / (100 - disc)) * sp;


System.out.println("Marked price of article = Rs. " + mp);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy