Tuesday 24 July 2012

Exception Handling -Objective Questions -Part 2

1)

class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {C() {}} // 3

Which of the following statements are true?

A. Compile-time error at 1.
B. Compile-time error at 2.
C. Compile-time error at 3.
D. None of the above

Answer: C

The constructors for class B and class C both invoke the constructor for A. The constructor for class A
declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A, it is  necessary to declare Exception in the throws clauses of B and C. A compile-time error is generated at marker 3, because the constructor does not declare Exception in the throws clause.

2)

public class Main{
   public static void main (String[] args) {
    try{
    System.out.print("before");
    throw new Exception();
    System.out.print("after");
    }catch(Exception e){
    System.out.println("exception");
    }}
   }

A. Prints before exception
B. Prints before exception after
C. compile-time error
D. run-time error

Answer: C

A throw statement appearing in a try block causes control to pass out of the block. Consequently, statements can not be reached if they appear in the block after the throw statement.Hence the result is compile-time error.

3)

What will be the output of the program?

public class Foo
{
    public static void main(String[] args)
    {
        try
        {
            return;
        }
        finally
        {
            System.out.println( "Finally" );
        }
    }
}

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

Answer: A

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.

4)

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 */
    } }

A. finally
B. exception finished
C. finally exception finished
D. Compilation fails

Answer: C

At line 9 an exception is generated ,there is no catch block to handle it.so that the finally block executes.The exception is propagated up the call stack and is caught by the catch in the main method (line 20). This prints "exception".Lastly program execution continues, because the exception has been caught, and "finished" is output (line 24).

5)

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

A. BD
B. BCD
C. BDE
D. BCDE

Answer: C

A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught.

6)

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

A. Ex0 caught
B. exception caught
C. Compilation fails because of an error at line 2.
D. Compilation fails because of an error at line 9.

Answer: A

An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is executed in this block. There is no finally block of code to execute.

7)

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

Which statement is most true concerning this code?

A. The code will not compile.
B. Code output: Start Hello world File Not Found.
C. Code output: Start Hello world End of file exception.
D. Code output: Start Hello world Catch Here File not found.

Answer: A

The only legal statements after try blocks are either catch or finally statements.

8)

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

which answer most closely indicates the behavior of the program?

A. The program will not compile.
B. 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
C. The program will print Hello world, then will print that a RuntimeException has occurred,
   and then will print Finally executing.
D. The program will print Hello world, then will print Finally executing, then will print that
   a RuntimeException has occurred.

Answer: D

Once the program throws a RuntimeException (in the throwit() method) that is not caught, the finally block will be executed and the program will be terminated. If a method does not handle an exception, the finally block is executed before the exception is propagated.

9)

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

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

Answer: C

Implemented class can use throws clause for a function only if the interface declared throws for the
corresponding function.Here dis() in A not declared any throws clause.Hence we can't declare any throws clause for the function dis() in Main class.It is a rule that if the interface declared a throws for a function then you can either declare throws for the same or narrowed exception or no throws clause in the implemented class.

10)

public class Main{
public void dis(){
try{
System.out.println("try");
}catch(IOException e){
System.out.println("IO");
}
}
   public static void main (String[] args) {
   new Main().dis();
  } }

A. Prints try
B. Prints Nothing
C. Prints IO
D. compile-time error

Answer: D

Unreachable catch block for IOException. This exception is never thrown from the try statement body.

No comments:

Post a Comment