JAVA UNIT-4 2MARKS
JAVA UNIT-4 2MARKS
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
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.