0% found this document useful (0 votes)
15 views5 pages

Chapter 7 Computer

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)
15 views5 pages

Chapter 7 Computer

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

CHAPTER – 7

INPUT IN JAVA
PART – A VERY SHORT QUESTIONS
1. Name the stream that is used to handle binary I/O of primitive data type and
String values.
→ DataInputStream and DataOutputStream
2. What is the use of Buffered streams?
→ To improve efficiency of I/O operations by reducing disk access.
3. Give the commands used for simple output in Java.
→ System.out.print() and System.out.println()
4. Name the three predefined I/O streams that use the console - input, error and
output streams.
→ System.in, System.out, System.err
5. What type of data are handled by Data Streams?
→ Primitive data types (int, char, float, etc.) and Strings.
6. What can you do with the Scanner class?
→ Read input from keyboard, files, or other input sources.
7. To which package do the Scanner and Printer class belong?
→ java.util (Scanner), java.io (Printer classes)
8. Write the syntax to create a Scanner object.
→ Scanner sc = new Scanner(System.in);
9. Name the package that contains Scanner class.
→ java.util
10.What is the use of the keyword import?
→ To access classes from other packages.
11.Name the type of error in each of the following:
(i) Division by a variable that contains zero → Runtime Error
(ii) Multiplication operator used when division required → Logical Error
(iii) Missing semicolon → Syntax Error
12.Complete the code to create an object of scanner class
→ Scanner sc = new Scanner(System.in);
13.Name the type of error in each given case:
(i) Math.sqrt(36 – 45); → Runtime Error (square root of negative number)
(ii) int a; b; c; → Syntax Error
14.What is the difference between the Scanner class element nextInt() and
next()?
→ nextInt() → Reads an integer value.
→ next() → Reads a single word (string without spaces).
PART – B SHORT QUESTIONS
Part B: Short Questions
1. What is a package? Give an example.
→ A package is a collection of related classes and interfaces.
Example: java.util (contains Scanner, ArrayList, etc.)

2. Explain various usage/advantages of Java Packages.


 Avoids name conflicts
 Provides reusability of code
 Easier to maintain
 Provides access control
 Supports modular programming

3. Explain different types of errors.


 Syntax Error → Grammar/format mistakes in code.
 Runtime Error → Errors during execution (e.g., divide by zero).
 Logical Error → Program runs but gives wrong result.

4. How is runtime error different from other two types of errors?


→ Runtime errors occur while executing the program, not during compilation.
Example: Divide by zero.
(Syntax and logical errors don’t cause the program to crash at runtime.)

5. Write about the three predefined I/O streams.


 System.in → Standard input stream (keyboard).
 System.out → Standard output stream (console).
 System.err → Standard error stream.

6. Explain the uses of Java Packages.


→ Organizes classes, prevents naming conflicts, allows reusability, makes searching and
maintenance easier, provides access control.

7. List the advantages of using Scanner class.


 Easy to use for input.
 Can read different types of data (int, float, string).
 Supports input from keyboard, files, and streams.

8. Write a program to find the Addition of three numbers using Scanner class.
import java.util.*;
class AddThree
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("Sum = " + (a + b + c));
}
}

9. Program: Accept perpendicular and base of a Right Angled Triangle.


Display hypotenuse, area, perimeter.
import java.util.*;
class RightTriangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double p = sc.nextDouble();
double b = sc.nextDouble();
double h = Math.sqrt(p*p + b*b);
double area = (p * b) / 2;
double perimeter = p + b + h;
System.out.println("Hypotenuse: " + h);
System.out.println("Area: " + area);
System.out.println("Perimeter: " + perimeter);
}
}

10. Program: Accept length & breadth of rectangle. Display area & perimeter.
import java.util.*;
class Rectangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int b = sc.nextInt();
System.out.println("Area = " + (l * b));
System.out.println("Perimeter = " + (2 * (l + b)));
}
}

11. Program: Input 3 numbers & compute average.


import java.util.*;
class Average
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
double avg = (a + b + c) / 3.0;
System.out.println("Average = " + avg);
}
}

12. Program: Input and swap two numbers using Scanner.


import java.util.*;
class Swap
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();
int temp = a;
a = b;
b = temp;
System.out.println("After swap: a = " + a + ", b = " + b);
}
}

13. What do you understand by ‘Run time error’? Explain with example.
→ Error that occurs during execution of a program.
Example:
int a = 5, b = 0;
System.out.println(a / b); // Division by zero → Runtime Error

14. What are the different types of errors that take place during execution of
a program?
 Runtime errors (e.g., divide by zero)
 Logical errors (e.g., wrong formula)

15. Distinguish between syntax error and logical error.


 Syntax Error → Violates Java grammar rules, caught at compile-time.
 Logical Error → Program runs but gives wrong result due to wrong logic.

You might also like