Java - Core Java Exercises
Java - Core Java Exercises
try
{
mQuotient = aDividend / aDivisor;
}
import java.io.*;
class ExceptionCatchingDemo2
{
public static void main( String[] args )
{
fileOperations( );
}
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( );
}
Will the exception that is being thrown above get caught in any of the catch blocks? If so,
which one and why?
import java.io.*;
class ExceptionCatchingDemo3
{
public static void main( String[] args )
{
fileOperations( );
}
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( );
}
}
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( );
}
}
i) DataInputStream
ii) FileInputStream
iii) FileReader
iv) InputStreamReader
i) FileOutputStream
ii) FileOutputStream wrapped over a DataOutputStream
iii) DataOutputStream
iv) DataOutputStream wrapped over a FileOutputStream
i) Writer
ii) FileWriter
iii) FileOutputStream
ii) OutputStreamWriter
11. To read primitive Java data types from a binary file, we should use:
class Entity
{
private int[] oEntityId = null;
// other field 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?