0% found this document useful (0 votes)
20 views

JAVA UNIT-4 2MARKS

The document provides an overview of Java packages, including their definition, advantages, and types. It explains concepts such as CLASSPATH, exception handling, and stream types, along with examples of user-defined packages and methods related to wrapper classes. Additionally, it covers exception handling keywords, differences between checked and unchecked exceptions, and the use of streams in Java.

Uploaded by

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

JAVA UNIT-4 2MARKS

The document provides an overview of Java packages, including their definition, advantages, and types. It explains concepts such as CLASSPATH, exception handling, and stream types, along with examples of user-defined packages and methods related to wrapper classes. Additionally, it covers exception handling keywords, differences between checked and unchecked exceptions, and the use of streams in Java.

Uploaded by

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

UNIT-4 (2 MARKS)

1. Define package with syntax and example?


A. A package is a group of classes,interfaces and sub-packages.It is defined by keyword
package.
Syntax:
package packagename;
Example:
package mypackage;
2. write any two advantages of java packages?
A.1. Java package is used to categorize the classes and interfaces.So that they can be easily
maintained.
2.Java package provides access protection and removes naming collision.this means that a
unique name had to be used for each class to avoid name collisito createon.
3. Define types of packages in java?
A. packages in java can be categorized into two types:
*Built-in packages
*User defined packages
There are many built-in packages such as lang,util,io,net,awk
1.java.lang
2.java.util
3.java.io
4.java.net
5.java.awk
User defined package:
These are the packages defined by the user.A package declaration resides at the top of
a java source file(java pro).
4. write a program to create a user defined package in java?
A. package
vehicle;
Interface Vehicle
{
public void run();
public void speed();
}
Car.java
Packages vehicles;
public class Car implements Vehicle
{
public void run()
{
System.out.println(“car is running”);
}
public void speed()
{
System.out.println(“Speed of car:50 Km/h”);
}
public static void main(String args[])
{
Car Car=new Car(); Car.run();
Car.speed();
System.out.println(“hello world”);
}
Output:
car is running
Speed of car:50 Km/h
hello world
5. Explain the term CLASSPATH?
Ans. The packages are nothing but the directories. For locating the specified package the java
run time system makes use of current working directory as its starting point. This
directory path is called CLASSPATH.

6. What are the ways of importing the java packages? OR Write the syntax for
importing packages in a Java source file and give an example.
A. The import statement can be written at the beginning of the Java program, using
the keyword import. For example -
import
java.io.File or
import java.io.*
Either a class name can be specified explicitly or a * can be used which indicated that the
Java compiler should import the entire package.
7. Define the term ClassMath?
A. Java Math class contains certain methods to perform different numeric operations such
as exponential , square root, cube root, trigonometric and logarithmic functions.
8. Define wrapper classs and its methods in java?
A. In Java, wrapper classes are used to convert primitive data types into objects and
vice versa. They have a collection of methods for manipulating and converting data.
Methods of wrapper classes:
1.equals()
2.byteValue()
3.compareTo()
4.doubleValue()
5.floatValue()
6.longValue()
7.intValue()
8.shortValue()
9.toletString(type number)
10.valueOf(type number)
9. Explain about auto-boxing with syntax and example?
A. The automatic conversion of primitive datatypes into its corresponding wrapper class
object is called auto-boxing.
Syntax:
WrapperClass obj=primitive value;

Example:
Integer b1=20;
10. Explain about auto un-boxing with syntax and example?
A. Unboxing is auto-unboxing is process of converting a wrapper class instance is a primitive
data type.
Syntax:
primitive type variable=obj;
Example:
Int a=b1;
11. What is an exception?
Exception is an event which disrupts the flow of a program. When an exception occurs, an
exception object is created, which contains the information regarding the error, type, and
state of the program when the error occurred.
12. Write the types of Exceptions in Java.
We have two types of exceptions in Java: Checked and Unchecked. Checked exceptions are
the exceptions checked at compile time by the compiler. Unchecked Exceptions are those
not checked at the compile time by the compiler.
13. What are the advantages of using exception handling in your program?
Java Exception handling maintains the flow of program execution. It handles the exceptions
14. List the Exception handling keywords in Java.
There is 5 Exception Handling keywords in Java enlisted:
 try
 catch
 finally
 throw
 throws
15. Write the difference between throw, throws, and throwable in Java.
throw is a keyword in java used to throw an exception manually. Exception should be of
type java.lang.Throwable class or its subclasses
throws is also a keyword in java used in the method declaration to indicate that this method
may throw exceptions.
Throwable forms a part of the super class that is for all types of errors and exceptions in
java. Let's take a look at the following example.

16. Write the difference between final, finally, and finalize in Java.
Final is an access modifier. finally, a block in Exception Handling and finalize forms a method
of the object class. Moreover, the final method is called when we execute it, finally executed
after the try-catch block is executed, and finalize executes just before the object is destroyed.
17. What is the use of try, catch keywords?
Ans. : try - A block of source code that is to be monitored for the exception.
catch - The catch block handles the specific type of exception along with the try block.
18. What is ArrayIndexOutOfBoundsException?
Ans. When we use an array index which is beyond the range of index then
ArrayIndexOutOfBoundsException occurs.
19. What is the need of multiple catch?
Ans.:• There may be the situations in which different exceptions may get raised by a single try
block statements and depending upon the type of exception thrown it must be caught.
• To handle such situation multiple catch blocks may exist for the

single try block statements.


20. What are the methods provided by the Throwable class in Java?
The methods provided by the Throwable class are as follows:
 getMessage()
 toString()
 printStackTrace()
 fillInStackTrace()
 getStackTrace()
 getClause()
21. What is the difference between checked and unchecked Exceptions?
A. Checked exceptions happen at compile time when the source code is transformed into
an executable code. Unchecked exceptions happen at runtime when the executable program
starts running.
22. What is stream?
Ans. Stream is basically a channel on which the data flow from sender to receiver.
23. What is input stream and output stream?
Ans. An input object that reads the stream of data from a file is called input stream and the
output object that writes the stream of data to a file is called output stream.
24. What is byte stream? Enlist its super classes.
Ans. The byte stream is used for inputting or outputting the bytes. The InputStream and
OutputStream are the superclass of byte stream.
25. What is character stream? Enlist its super classes.
Ans. The character stream is used for inputting or outputting the characters. The Reader and
Writer are the superclass of character stream.
26. List the byte stream classes.
Ans. The Byte Stream classes are -
1. FileInputStream

3. FilterInputStream
5. FileOutputStream
7. FilterOutputStream
2. PipedInputStream

4. ByteArrayInputStream
6. PipedOutputStream
8. ByteArrayOutputStream
27. Give an example on stream.
• There are two types of streams - Byte stream and Character stream.

• Stream classes for byte stream : FileInputStream, PipedInputStream,


BufferedInputStream, FileOutputStream, PipedOutputStream and so on.
• Stream classes for character stream: FileReader, PipeReader, BufferedReader, FileWriter,
PipeWriter, Buffered Writer and so on.

You might also like