Java Unit 4
Java Unit 4
Java Unit 4
2Marks
2)What is Stream?(nov-13,apr-14,nov-15,apr-15)
A stream is a path of communication between the source and destination. It represent the
ordered sequence of data. A stream is linked to physical device by the java I/O stream. The data
flows There are two types of stream,
1.Input stream-reads.
2.output stream-writes.
3)What is an exception?(apr-12,nov-14,apr-15)
An exception is an abnormal condition, which occurs during the execution of a program
Exceptions are error events like division by zero, opening of a file that does not exist.
5)What is ByteStream?(nov-14)
Byte stream classes that provide support for creating and manipulating streams and files
for reading(input stream) and writing(output stream) bytes. It is a unidirectional(ie)that can
transmit bytes in only one direction.
6)Define applet?(apr-12)
An applet is a dynamic and interactive program that runs inside a web page displayed by
a Java capable browser. This can also be executed using the appletviewer.
10)What is a file?(apr-13)
File class that provides support for creating files and directories. It's a collection of
record. some of the operations of files are creating, opening, closing, deleting.
Data Input
Hierarchy of inputstreamclasses
OutputStream classes:
OutputStream class is an abstract class.It is the super class of all classes representing an
output stream of bytes. we cannot instantiate it.The methods that are designed to perform tasks
are;
1)writing bytes.
2)closing streams.
3)flushing streams.
Data Output
OutputStreamclasses:
OutputStream class is an abstract class.It is the super class of all classes representing an
output stream of bytes. we cannot instantiate it.The methods that are designed to perform tasks
are; 1)writing bytes.
2)closing streams.
3)flushing streams.
Commonly used methods of OutputStream class:
1) write()-writes a byte to the output stream.
2) write(byte b[ ])-write all bytes in the array b to output stream.
3)close()-closes the output stream.
4)flush()-flushes the output stream.
3)Explain the keywords that are used to manage the exception in java?(apr-16)
An exception is an abnormal condition, which occurs during the execution of a program
Exceptions are error events like division by zero, opening of a file that does not exist. Java
provides specific keywords for exception handling purposes they are,
1)Try-Catch
2)Multiple Catch Statements.
3)Finally
4)Throwing Our Own Exceptions.
1.Try-Catch – The try block contains a block of program statements within which an exception
might occur. The corresponding catch block executes if an exception of a particular type
occurs within the try block. try is the start of the block and catch is at the end of try block to
handle the exceptions. We can have multiple catch blocks with a try and try-catch block can be
nested also. catch block requires a parameter that should be of type Exception.
Syntax: try
{
//statements that may cause an exception
}
catch (exception-type e)
{
//error handling code
}
2.Finally – finally block is optional and can be used only with try-catch block. Since exception
halts the process of execution, we might have some resources open that will not get closed, so we
can use finally block. finally block gets executed always, whether exception occurred or not.
Syntax: try
{
//statements that may cause an exception
}
catch(Exception e)
{
//statements
}
finally
{
//statements to be executed
}
3.Multiple Catch Statements: When an exception in a try block is generated,java treats the
multiple catch statements like switch case statement.The first satements whose parameter
matches with exception object will be executed and the remaning statementswill be skipped.
syntax: try
{
statement;
}
catch(Exception-Type-1 e)
{
statement;
}
.
.
catch(Exception-Type-N e)
{
statement;
}
4)Throwing Our Own Exceptions.
There may be times to throw our own exception.This can be done by the keyword called
throw.It is used inside method body to invoke an exception.
syntax: throw new Throwabl'e subclass;
Throws – Throws are used to throws an exception in a method and not for handling it. this
throws keyword is used.It is specifed after the method declaration
Example:
import java.lang.Excception;
class myException extends Exception
{
MyException(String message)
{
super(messgae);
}
}
class TestMyException
{
public static void main(String args[])
{
int x=2,y=100;
try
{
float z= (float) x/(float)y;
if(z<0.01)
{
throw new MyException("number is small");
}
}
catch(MyException e)
{
system.out.println("caught");
system.out.println(e.getMessage());
}
finally
{
system.out.println("i am");
}
}
}
output: caught
number is small
i am
4) Explain any five methods in the Graphics class?(nov-15,nov-14)
The Graphics class is the abstract super class for all graphics contexts which allow an
application to draw onto components that can be realized on various devices. some of the
methods are,
1. public abstract void drawString(String str, int x, int y): is used to draw the
specified string.
2. public void drawRect(int x, int y, int width, int height): draws a rectangle with the
specified width and height.
3. public abstract void fillRect(int x, int y, int width, int height): is used to fill
rectangle with the default color and specified width and height.
4. public abstract void drawOval(int x, int y, int width, int height): is used to draw
oval with the specified width and height.
5. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval
with the default color and specified width and height.
Example:
class exam
{
public static void main(string args[])
{
int a=10,b=0,c;
c=a/b; //divide by zero(run time error)
system.out.println("the value of c:"+c) //semi-colon missing(compile time error)
}
6)Give a brief account on character stream classes?(apr-16)
Character stream is also defined by using two abstract class at the top of hierarchy .They
are used to Reade and Write 16-bit unicode characters.
1)checked exceptions: These exceptions are explicity handled in the code itself with the help of
try-catch blocks. checked exceptions are extented from the java.lang.Exception class.
Java provides specific keywords for exception handling purposes they are,
1)Try-Catch
2)Multiple Catch Statements.
3)Finally
4)Throwing Our Own Exceptions.
try Block
Statement that causes
an exception Exception object creator
Throws
exception
object
1.Try-Catch – The try block contains a block of program statements within which an exception
might occur. The corresponding catch block executes if an exception of a particular type
occurs within the try block. try is the start of the block and catch is at the end of try block to
handle the exceptions. We can have multiple catch blocks with a try and try-catch block can be
nested also. catch block requires a parameter that should be of type Exception.
Syntax: try
{
//statements that may cause an exception
}
catch(exception-typee)
{
//error handling code
}
2.Finally – finally block is optional and can be used only with try-catch block. Since exception
halts the process of execution, we might have some resources open that will not get closed, so we
can use finally block. finally block gets executed always, whether exception occurred or not.
Syntax: try
{
//statements that may cause an exception
}
catch(Exception e)
{
//statements
}
finally
{
//statements to be executed
}
3.Multiple Catch Statements: When an exception in a try block is generated,java treats the
multiple catch statements like switch case statement.The first satements whose parameter
matches with exception object will be executed and the remaning statementswill be skipped.
syntax: try
{
statement;
}
catch(Exception-Type-1 e)
{
statement;
}
.
.
.
catch(Exception-Type-N e)
{
statement;
}
Throws – Throws are used to throws an exception in a method and not for handling it. this
throws keyword is used.It is specifed after the method declaration
Example:
Class xthrows
{
static void m( ) throws ArithmeticException
{
int x=2,y=0,z;
z=x/y;
}
public static void main(string args[])
{
try
{
m( );
}
catch(ArithmeticException e)
{
system.out.prinntln("caught exception"+e);
}
}
}
output: caught exception java.lang. ArithmeticException:/by zero.