Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CORE JAVA – EXERCISES - 2

1. What is the major mistake in the following code:

public class TryBlockDemo


{
public static void main( String[] args )
{
divide( 15, 5 );
}

private static void divide( int aDividend, int aDivisor )


{
int mQuotient = 0;

try
{
mQuotient = aDividend / aDivisor;
}

System.out.println( "The Quotient is: " + mQuotient );


}
}

2. What is the major mistake in the following code:

public class TryCatchBlockDemo


{
public static void main( String[] args )
{
perform( "Hello", "World" );
}

private static void perform( String aFirstString, String


aSecondString )
{
try
{
aFirstString = aFirstString + " " +
aSecondString.substring( 1, 3 );
aFirstString = aFirstString + " ";
}
aFirstString = aFirstString + "Bye";

catch( Exception aWorkException )


{
aWorkException.printStackTrace( );
return;
}

System.out.println( "The Final String is: " + aFirstString


);
return;
}
}
3. Consider the following code:

import java.io.*;

class ExceptionCatchingDemo2
{
public static void main( String[] args )
{
fileOperations( );
}

private static void fileOperations( )


{
try
{
………
………
throw new FileNotFoundException( );
………
………
}
catch( FileNotFoundException aWorkFileNotFoundException )
{
System.out.println( "File not found exception caught"
);
aWorkFileNotFoundException.printStackTrace( );
}
catch( IOException aWorkIOException )
{
System.out.println( "IO exception caught" );
aWorkIOException.printStackTrace( );
}
catch( ArrayIndexOutOfBoundsException
aWorkArrayIndexOutOfBoundsException )
{
System.out.println( "Array Index out of bounds
exception caught" );
aWorkArrayIndexOutOfBoundsException.printStackTrace(
);
}

System.out.println( "File Operation completed" );


System.out.println( "Returning to caller ... " );
return;
}
}

i) Which of the catch blocks above will catch the thrown exception?
ii) Where will the execution proceed from, after that catch block has executed?
4. Consider the following code:

import java.io.*;

class ExceptionCatchingDemo
{
public static void main( String[] args )
{
fileOperations( );
}

private static void fileOperations( )


{
try
{
………
………
throw new IOException( );
………
………
}
catch( ArrayIndexOutOfBoundsException
aWorkArrayIndexOutOfBoundsException )
{
aWorkArrayIndexOutOfBoundsException.printStackTrace(
);
}
catch( Exception aWorkException )
{
aWorkException.printStackTrace( );
}
}
}

Will the exception that is being thrown above get caught in any of the catch blocks? If so,
which one and why?

5. Consider the following code:

import java.io.*;

class ExceptionCatchingDemo3
{
public static void main( String[] args )
{
fileOperations( );
}

private static void fileOperations( )


{
try
{
………
………
throw new IOException( );
………
………
}
catch( ArrayIndexOutOfBoundsException
aWorkArrayIndexOutOfBoundsException )
{
System.out.println( "Array Index out of bounds
exception caught" );
aWorkArrayIndexOutOfBoundsException.printStackTrace(
);
}
catch( Exception aWorkException )
{
System.out.println( "All exceptions caught" );
aWorkException.printStackTrace( );
}
catch( IOException aWorkIOException )
{
System.out.println( "IO exception caught" );
aWorkIOException.printStackTrace( );
}

System.out.println( "File Operation completed" );


System.out.println( "Returning to caller ... " );
return;
}
}

What is the major error in the code above and why? Is it a run-time error? What is to be
done to rectify the error?
6. Consider the code below:

import java.io.*;

class ExceptionPropagationDemo
{
public static void main( String[] args )
{
try
{
System.out.println( "Entered main ... " );
………
m1( );
………
………
}
catch( Exception aWorkException )
{
System.out.println( "Caught Exception in main" );
aWorkException.printStackTrace( );
}
}

private static void m1( )


{
try
{
System.out.println( "Entered m1 ... " );
………
………
m2( );
………
}
catch( IOException aWorkIOException )
{
System.out.println( "Caught IO Exception in m1" );
aWorkIOException.printStackTrace( );
}
}

private static void m2( ) throws IOException


{
// This method contains no try-catch block
System.out.println( "Entered m2 ... " );

……… // method code


………
………
}
}
Assume that a NullPointerException occurs in the method m2( ).

i) Describe with reasons how the exception would propagate up the method
call sequence, and which catch block (if any) would finally catch it.

ii) If we remove the try-catch structure from the main( ) function, what would
happen to the exception?

7. Find the main mistake in the following code. Also suggest two possible ways to
rectify it.

import java.io.*;

class ExceptionCatchingDemo4
{
public static void main( String[] args )
{
try
{
………
fileOperations( );
………
………
}
catch( Exception aWorkException )
{
System.out.println( "Caught Exception in main" );
aWorkException.printStackTrace( );
}
}

private static void fileOperations( )


{
// This method contains no try-catch block
………
………
throw new IOException( );
………
………
}
}
In Questions 1-4 below, choose the most appropriate answer:

8. In order to read individual text characters from a text file, we use:

i) DataInputStream
ii) FileInputStream
iii) FileReader
iv) InputStreamReader

9. To write primitive Java data types to a binary file, we should use:

i) FileOutputStream
ii) FileOutputStream wrapped over a DataOutputStream
iii) DataOutputStream
iv) DataOutputStream wrapped over a FileOutputStream

10. To write individual text characters to a text file, we use:

i) Writer
ii) FileWriter
iii) FileOutputStream
ii) OutputStreamWriter

11. To read primitive Java data types from a binary file, we should use:

i) FileInputStream wrapped over a DataInputStream


ii) DataInputStream
iii) DataInputStream wrapped over a FileInputStream
iv) FileInputStream

12. Consider the following class:

class Entity
{
private int[] oEntityId = null;
// other field declarations ………

public void setEntityId( int aEntityId, int aEntityIndex )


{
oEntityId[aEntityIndex] = aEntityId;
}
// other method declarations ………
}

This class models a table called “ENTITY” in the database of the system. We want to use
an object of this class to cache the data of the ENTITY table in memory temporarily, so
that the front-end can access the data from the memory. However, in case of huge
amounts of data in this object, the application server will want to automatically save the
object to a disk file temporarily, in order to avoid memory overflow.

Can the application server save objects of the Entity class to disk? If not, what change is
to be made in the class definition above?

You might also like