Java Programming

Java Programming

Exceptions has 16 Questions
What will be the output of the program?
public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Finally" ); 
        } 
    } 
}

Finally
Compilation fails.
The code runs with no output.
An exception is thrown at runtime.

and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
import java.io.*;
public class MyProgram 
{
    public static void main(String args[])
    {
        FileOutputStream out = null;
        try 
        {
            out = new FileOutputStream("test.txt");
            out.write(122);
        }
        catch(IOException io) 
        {
            System.out.println("IO Error.");
        }
        finally 
        {
            out.close();
        }
    }
}


This program will compile successfully.
This program fails to compile due to an error at line 4.
This program fails to compile due to an error at line 6.
This program fails to compile due to an error at line 13.
What will be the output of the program?
try 
{ 
    int x = 0; 
    int y = 5 / x; 
} 
catch (Exception e) 
{
    System.out.println("Exception"); 
} 
catch (ArithmeticException ae) 
{
    System.out.println(" Arithmetic Exception"); 
} 
System.out.println("finished");

finished
Exception
Compilation fails.
Arithmetic Exception

which answer most closely indicates the behavior of the program?
public class MyProgram 
{
    public static void throwit() 
    {
        throw new RuntimeException();
    }
    public static void main(String args[])
    {
        try 
        {
            System.out.println("Hello world ");
            throwit();
            System.out.println("Done with try block ");
        }
        finally 
        {
            System.out.println("Finally executing ");
        }
    }
}


The program will not compile.
The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B");  
        } 
        finally 
        {
            System.out.print("C"); 
        } 
        System.out.print("D"); 
    }  
    public static void badMethod() 
    {
        throw new Error(); /* Line 22 */
    } 
}

ABCD
Compilation fails.
C is printed before exiting with an error message.
BC is printed before exiting with an error message.

At Point X on line 5, which code is necessary to make the code compile?
public class ExceptionTest 
{ 
    class TestException extends Exception {} 
    public void runTest() throws TestException {} 
    public void test() /* Point X */ 
    { 
        runTest(); 
    } 
}


No code is necessary.
throws Exception
catch ( Exception e )
throws RuntimeException
What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A");  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print("B"); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print("C"); 
        } 
        finally 
        {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}

BD
BCD
BDE
BCDE

and given that EOFException and FileNotFoundException are both subclasses of IOException, and further assuming this block of code is placed into a class, which statement is most true concerning this code?
System.out.print("Start ");
try 
{
    System.out.print("Hello world");
    throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e) 
{
    System.out.print("End of file exception");
}
catch(FileNotFoundException e) 
{
    System.out.print("File not found");
}


The code will not compile.
Code output: Start Hello world File Not Found.
Code output: Start Hello world End of file exception.
Code output: Start Hello world Catch Here File not found.
What will be the output of the program?
public class RTExcept 
{
    public static void throwit () 
    {
        System.out.print("throwit ");
        throw new RuntimeException();
    }
    public static void main(String [] args) 
    {
        try 
        {
            System.out.print("hello ");
            throwit();
        }
        catch (Exception re ) 
        {
            System.out.print("caught ");
        }
        finally 
        {
            System.out.print("finally ");
        }
        System.out.println("after ");
    }
}

hello throwit caught
Compilation fails
hello throwit RuntimeException caught after
hello throwit caught finally after
Which statement is true?

catch(X x) can catch subclasses of X where X is a subclass of Exception.
The Error class is a RuntimeException.
Any statement that can throw an Error must be enclosed in a try block.
Any statement that can throw an Exception must be enclosed in a try block.
What will be the output of the program?
public class Test 
{  
    public static void aMethod() throws Exception 
    {
        try /* Line 5 */
        {
            throw new Exception(); /* Line 7 */
        } 
        finally /* Line 9 */
        {
            System.out.println("finally"); /* Line 11 */
        } 
    } 
    public static void main(String args[]) 
    {
        try 
        {
            aMethod();  
        } 
        catch (Exception e) /* Line 20 */
        {
            System.out.println("exception"); 
        } 
        System.out.println("finished"); /* Line 24 */
    } 
}

finally
exception finished
finally exception finished
Compilation fails
Which four can be thrown using the throw statement?
  1. Error
  2. Event
  3. Object
  4. Throwable
  5. Exception
  6. RuntimeException

1, 2, 3 and 4
2, 3, 4 and 5
1, 4, 5 and 6
2, 4, 5 and 6
What will be the output of the program?
public class X 
{ 
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B"); 
        }  
        finally 
        {
            System.out.print("C"); 
        }  
        System.out.print("D"); 
    }  
    public static void badMethod() {} 
} 

AC
BC
ACD
ABCD
What will be the output of the program?
public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod(); /* Line 7 */
            System.out.print("A"); 
        } 
        catch (Exception ex) /* Line 10 */
        {
            System.out.print("B"); /* Line 12 */
        } 
        finally /* Line 14 */
        {
            System.out.print("C"); /* Line 16 */
        }  
        System.out.print("D"); /* Line 18 */
    } 
    public static void badMethod() 
    {
        throw new RuntimeException(); 
    } 
}

AB
BC
ABC
BCD
What will be the output of the program?
public class MyProgram 
{
    public static void main(String args[])
    {
        try 
        {
            System.out.print("Hello world ");
        }
        finally 
        {
            System.out.println("Finally executing ");
        }
    }
}

Nothing. The program will not compile because no exceptions are specified.
Nothing. The program will not compile because no catch clauses are specified.
Hello world.
Hello world Finally executing
What will be the output of the program?
class Exc0 extends Exception { } 
class Exc1 extends Exc0 { } /* Line 2 */
public class Test 
{  
    public static void main(String args[]) 
    { 
        try 
        {  
            throw new Exc1(); /* Line 9 */
        } 
        catch (Exc0 e0) /* Line 11 */
        {
            System.out.println("Ex0 caught"); 
        } 
        catch (Exception e) 
        {
            System.out.println("exception caught");  
        } 
    } 
}

Ex0 caught
exception caught
Compilation fails because of an error at line 2.
Compilation fails because of an error at line 9.