Tuesday 24 July 2012

Exception Handling -Objective Questions -Part 3

1)

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();}
    }}

and given that all methods of class FileOutputStream, including close(), throw an
IOException, which of these is true?

A. This program will compile successfully.
B. This program fails to compile due to an error at try block
C. This program fails to compile due to an error at catch block
D. This program fails to compile due to an error at finally block

Answer: D

Any method (in this case, the main() method) that throws a checked exception (in this case, out.close()  ) must be called within a try clause, or the method must declare that it throws the exception. Either main() must declare that it throws an exception, or the call to out.close()  in the finally block must fall inside a (in this case nested) try-catch block.

2)

class A {
  public static void main (String[] args) {
    Object error = new Error();
    Object runtimeException = new RuntimeException();
    System.out.print((error instanceof Exception) + ",");
    System.out.print(runtimeException instanceof Exception);
}}

What is the result of attempting to compile and run the program?

A. Prints: false,true
B. Prints: false,false
C. Prints: true,false
D. compile-time error

Answer: A

Error is a direct subclass of Throwable. RuntimeException is a direct subclass of Exception.

3)

class RedException extends Exception {}
class BlueException extends Exception {}
class White {
  void m1() throws RedException {throw new RedException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c,d; a = b = c = d = 0;
    try {white.m1(); a++;}catch (RedException e) {b++;}catch (BlueException e) {c++;}
      finally {d++;}System.out.print(a+","+b+","+c+","+d);
}}

What is the result of attempting to compile and run the program?

A. Prints 0,1,0,1
B. Prints 1,1,0,1
C. compile-time error
D. run-time error

Answer: C

A compile-time error is generated, because the second catch clause attempts to catch an exception that is never
thrown in the try block.

4)

Which statement is true?

A. A try statement must have at least one corresponding catch block.
B. Multiple catch statements can catch the same class of exception more than once.
C. An Error that might be thrown in a method must be declared as thrown by that method, or be
   handled within that method.
D. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally
   block will always start to execute.

Answer: D

If you put a finally block after a try and its associated catch  blocks, then once execution enters the try
block, the code in that finally block will definitely be executed except in the following circumstances:

   1. An exception arising in the finally block itself.
   2. The death of the thread.
   3. The use of System.exit()
   4. Turning off the power to the CPU.

5)

class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
        try {
     throw new Level2Exception();      
    }catch (Level3Exception e) {
    System.out.println("Level3");
    }catch (Level1Exception e) {
    System.out.println("Level1");
     }catch (Exception e) {
    System.out.println("Exception");    
     }}}

A. Prints Level1
B. Prints Level3
C. Prints Exception
D. compile-time error

Answer: A

Level1Exception is the super class of Level2Exception so it can handle generated Level2Exception.Hence the result Level1

6)

class Color extends Exception{}
class Blue extends Color{}
interface A{void dis()throws Blue; }
public class Main implements A{
public void dis() throws Color{
throw new Color();
}
   public static void main (String[] args) {
  try{
  new Main().dis();
  }catch(Color e){
  System.out.println("color");
  }} }

A. Prints color
B. Prints Nothing
C. compile-time error
D. run-time error

Answer: C

Here dis() 's throws clause in interface is narrowed(ie Blue is sub of Color) than its throws clause in Main.Hence the result is compile time error.You can either declare no throws for dis() in Main or same throws as in interface or wider than that.

7)

class A {
  public static void main (String[] args) {
    Error error = new Error();
    Exception exception = new Exception();
    System.out.print((exception instanceof Throwable) + ",");
    System.out.print(error instanceof Throwable);
}}

What is the result of attempting to compile and run the program?

A. Prints: false,false
B. Prints: false,true
C. Prints: true,true
D. compile-time error

Answer: C

Both Error and Exception are subclasses of Throwable.

8)

class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
        try {
     throw new Level1Exception();      
    }catch (Level3Exception e) {
    System.out.println("Level3");
    }catch (Level2Exception e) {
    System.out.println("Level2");
     }catch (Exception e) {
    System.out.println("Exception");    
     }}}

A. Prints Level3
B. Prints Level2
C. Prints Exception
D. compile-time error

Answer: C

Level2Exception and Level3Exception are subclasses of Level1Exception.They cant handle the Level1Exception.But the Exception class can handle all types of exceptions except Errors.Hence the result 'Exception'.

9)

class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
        try {
     throw new Level1Exception();      
    }catch (Level1Exception e) {S
    System.out.println("Level1");
    }catch (Level2Exception e) {
    System.out.println("Level2");
     }catch (Level3Exception e) {
    System.out.println("Level3");    
     }}}

A. Prints Level1
B. Prints Level2
C. Prints Level3
D. compile-time error

Answer: D

Unreachable catch block for Level2Exception and Level3Exception. It is already handled by the catch block for Level1Exception

10)

class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
  abstract void m1() throws ColorException;
}
class White extends Color {
  void m1() throws WhiteException {throw new WhiteException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c; a = b = c = 0;
    try {white.m1(); a++;}
      catch (WhiteException e) {b++;}
      finally {c++;}
    System.out.print(a+","+b+","+c);
}}

A. Prints 0,1,1
B. Prints 1,1,1
C. compile-time error
D. run-time error

Answer: A

The try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented and then finally block execute.

No comments:

Post a Comment