Sunday, 25 March 2012

::CoreJava-Exceptions-001::Which of the following statement is true regarding throws declaration in Exception handling?{

~All sub class types of exceptions that come under java.lang.RuntimeException, if not handled inside a method should be declared using throws keyword#No

~All sub class types of errors that come under java.lang.Error, if not handled inside a method should be declared using throws keyword#No

~All sub class types of java.lang.Throwable, if not handled inside a method should be declared using throws keyword#No

~All sub class types of exceptions that come under java.lang.Exception, if not handled inside a method should be declared using throws keyword#No

=None of the listed options#Correct

}



::CoreJava-Exceptions-002::Which of the following statement is true regarding the throws declarations for overriden methods?{

=The overriding method cannot declare additional exceptions which is not declared in its super class version.#Correct

~When a method in the super class is declared to throw a Checked Exception, the overriding method in the sub class should also declare the same.#No

~The overriding methods cannot declare to throw the Super Class types of those exceptions declared in the super class methods.#No

~the overriding method cannot re-declare the Unchecked exceptions, that are declared by super class method.#No

}



::CoreJava-Exceptions-003::Which of the following listed type cannot be caught and handled using catch block?{

~java.lang.Error#No

~java.lang.Exception#No

=None of the listed options#Correct

~java.lang.Throwable#No

}



::CoreJava-Exceptions-004::Which of the following options give the types that Throwable Constructor can accept?

1) String
2) StringBuffer
3) Throwable
4) Integer
5) Boolean{

~2,3#No

=1,3#Correct

~3,4#No

~4,5#No

~1,5#No

}



::CoreJava-Exceptions-005::Which of the following statements are true regarding try-catch-finally blocks?

1) Catch block is optional when a RuntimeException is thrown from the try block
2) Catch block is optional, if finally block is available, provided a Checked Exception is thrown from the try block
3) Finally block is optional, irrespective of catch block
4) Both catch block and finally block are optional, either Checked exception or unchecked exception is thrown from the try block
5) Both catch block and finally block are required, irrespective of the type of exceptions thrown from the try block{

~3,4#No

~1,2#No

~2,3#No

=1,3#Correct

~1,4#No

}



::CoreJava-Exceptions-006::Which of the following statements are true regarding try-catch-finally?

1) An exception which is not handled by a catch block can be handled by writing another try catch block inside finally block
2) An exception which is not handled by a catch block will be handled by subsequent catch blocks
3) A catch block can have another try block nested inside
4) Finally block cannot have a try block with multiple catch blocks
5) Both catch block and finally block can throw exceptions{

~2,3,4#No

~1,2,3#No

=2,3,5#Correct

~3,4,5#No

~1,4,5#No

}



::CoreJava-Exceptions-007::Which of the following are true regarding implementing user defined exception mechanisms?

1) It is not valid to derive a class from java.lang.Throwable
2) It is not valid to derive a class from java.lang.Error
3) It is valid to derive a class from java.lang.Exception
4) It is valid to derive a class from java.lang.RuntimeException
5) It is not valid to derive a class from java.io.IOException{

~1,2#No

~2,3#No

~4,5#No

~3,5#No

=3,4#Correct

}



::CoreJava-Exceptions-008::Which of the following are true regarding RuntimeException?

1) RuntimeException does not require a throws declaration
2) If RuntimeException is declared using throws clause, then the calling method should handle it using try-catch block
3) Any class that derives the RuntimeException will always be an unchecked exception
4) RuntimeException can be handled using a catch that handles Error
5) RuntimeException can be handled using a catch that handles Exception{

~3,4,5#No

~1,2,3#No

~2,3,4#No

=1,3,5#Correct

~1,3,4#No

}



::CoreJava-Exceptions-009::Which of the following statements about the printStackTrace() method are true?

1) Starts the error report from the calling method
2) Starts the error report from the method whether the exception occurred
3) Reports the line number in each method from where the exception is propagated to next level
4) Stack Trace can be shown only to the console
5) The printStackTrace() method is defined in java.lang.Exception class{

~1,2#No

~3,4#No

~4,5#No

~1,5#No

=2,3#Correct

}



::CoreJava-Exceptions-010::Consider the following code:

class LastException extends Exception { }
class PreviousException extends LastException { }
class TopException extends PreviousException { }

public class ExceptionalWorld {
   public static void main(String args[]) {
      try {
         exceptionThrower();
      }
      catch(LastException le) {System.out.println("Last Exception");}
      catch(PreviousException pe) {System.out.println("Previous Exception");}
   }
 
   static void exceptionThrower() throws TopException {
      throw new TopException();
   }
}

Which of the following option gives the output for the above code?{

~Run time Error#No

~Prints "Previous Exception"#No

~Prints "Last Exception"#No

=Compile Time Error#Correct

}



::CoreJava-Exceptions-011::Consider the following code:

class MyException extends Throwable { }

public class TestThrowable {
   public static void main(String args[]) {
      try {
         test();
      } catch(Throwable ie) {
         System.out.println("Exception");
      }
   }
 
   static void test() throws Throwable {
      throw new MyException();
   }
}

Which of the following option gives the output for the above code?{

~Compiler time error
Userdefined exceptions should extend Exception#No

~Compile time error
Cannot use Throwable to catch the exception#No

~Run time error
test() method does not throw a Throwable instance#No

=Prints
Exception#Correct

}



::CoreJava-Exceptions-012::Consider the following code:

class MyError extends Error { }

public class TestError {
   public static void main(String args[]) {
      try {
         test();
      } catch(Error ie) {
         System.out.println("Error caught");
      }
   }

   static void test() throws Error {
      throw new MyError();
   }
}

Which of the following option gives the output for the above code?{

~Compile time error
Error class cannot be extended#No

~Compile time error
Cannot catch Error type objects#No

~Run time error
test() method does not throw an Error type instance#No

=Prints
Error caught#Correct

}



::CoreJava-Exceptions-013::Consider the following code:

public class TestPropagator {
   public static void main(String args[]) {
      try {
         propagator1();
      } catch(IndexOutOfBoundsException i) {
         System.out.println(i.getMessage());
      }
   }
   static void propagator1() { propagator2(); }
   static void propagator2() { propagator3(); }
   static void propagator3() {
      throw new StringIndexOutOfBoundsException("StringIndexOutOfBoundsException");
   }
}

Which of the following option gives the output for the above code?{

~IndexOutOfBoundsException#No

~NullPointerException#No

~Compile time errors#No

=StringIndexOutOfBoundsException#Correct

}



::CoreJava-Exceptions-014::Consider the following code:

1   public class FinallyCatch {
2      public static void main(String args[]) { 
3         try {
4            throw new java.io.IOException();
5         }
6      }
7   }

Which of the following is true regarding the above code?{

~Shows unhandled exception type IOException at line number 4#No

~Shows unhandled exception type IOException at line number 5#No

~Demands a finally block at line number 4#No

=Demands a finally block at line number 5#Correct

}



::CoreJava-Exceptions-015::Consider the following code:

public class FinallyFinallyFinally {
   public static void main(String args[]) { 
      try {
         try {
            throw new java.io.IOException();
         }
         finally { System.out.println("Finally Inner");}
      } catch(Exception e){ System.out.println("Exception Outer"); }
         finally { System.out.println("Finally Outer");}
   }
}

Which of the following option gives the output for the above code?{

~Finally Outer
Exception Outer#No

~Exception Outer
Finally Inner
Finally Outer#No

~Exception Outer
Finally Outer#No

~Finally Inner
Finally Outer
Exception Outer#No

=Finally Inner
Exception outer
Finally Outer#Correct

}



::CoreJava-Exceptions-016::Consider the following code:

public class ThrowableError {
   public static void main(String args[]) {
      try {
         throw new Throwable();
      }catch(Error e) {
         System.out.println("Problem found");
      }
   }
}

Which of the following option gives the output for the above code?{

~Run time error#No

~Prints "Problem found"#No

~Runs with No output#No

=Compile time error#Correct

}



::CoreJava-Exceptions-017::Consider the following code:

public class ErrorWorld {
   public static void main(String args[]) {
      try {
         errorThrower();
      }catch(Error e) {
         System.out.println("Error caught");
      }
   }

   public static void errorThrower() throws Error {
      throw new Error();
   }
}

Which of the following will be the output for the above code?{

~Executes without any output#No

~Run time Error#No

~Compile Time Error#No

=Prints "Error caught"#Correct

}



::CoreJava-Exceptions-018::Consider the following code:

public class ProblemsWorld {
   public static void main(String args[]) {
      try {
         xpect();
      } catch(IOException e) {
         System.out.println("xpected caught");
      }
   }

   public static void xpect() throws IOException {
      throw new FileNotFoundException();
   }
}

Which of the following statement is true regarding the above code?{

~Compile time error; the declaration does not match the throw statement#No

=Compiles and Runs successfully and prints "xpected caught"#Correct

~Compiles successfully. But throws runtime error while executing#No

~Compile time error; built-in exceptions like FileNotFoundException cannot be instantiated programmatically#No

}



::CoreJava-Exceptions-019::Consider the following code:

class PlanetException extends Exception { }
class EarthException extends PlanetException { }

class Planet {
   void revolve() throws PlanetException {
      System.out.println("Planet revolves");
   }
}

class Earth extends Planet {
   void revolve() /* CODE 1 */ {
      System.out.println("Earth revolves");
   }
}

public class WorldOfExceptions {
   public static void main(String args[]) {
      Planet planet = new Earth();
      try {
         planet.revolve();
      }catch(/* CODE 2 */ e) {
         System.out.println("Problem found");
      }
   }
}

Which of the following listed code snippets on replacing the comments /* CODE 1 */ and /* CODE 2*/ in the above program makes the program to compile and execute properly?

1)  /* CODE 1*/ with EarthException
     /* CODE 2*/ with EarthException
2)  /* CODE 1*/ with PlanetException
     /* CODE 2*/ with PlanetException
3)  /* CODE 1*/ with EarthException
     /* CODE 2*/ with PlanetException 
4)  /* CODE 1*/ with PlanetException
     /* CODE 2*/ with EarthException
5)  /* CODE 1*/ with Exception
     /* CODE 2*/ with Exception{

~3,4#No

~1,2#No

=2,3#Correct

~4,5#No

~1,5#No

}



::CoreJava-Exceptions-020::Consider the following code:
import java.io.IOException;
import java.sql.SQLException;

interface Interfaze1 {
   void behaviour() throws IOException;
}

interface Interfaze2 {
   void behaviour() throws SQLException;
}

class Implementor implements Interfaze1, Interfaze2 {
/* CODE */
}

public class TestImplementor {
   public static void main(String args[]) {
      try {
         Implementor impl = new Implementor();
         impl.behaviour();
      }catch(Exception e) {
         System.out.println("Exception caught");
      }
   }
}

Which the following listed option gives the valid code snippet, that can be replaced for the comment /* CODE */ in the above code in order to compile and execute the program successfully?{

~public void behaviour() throws SQLException {
   System.out.println("Behaviour Implemented");
}#No

=public void behaviour() {
   System.out.println("Behaviour Implemented");
}#Correct

~public void behaviour() throws IOException, SQLException {
   System.out.println("Behaviour Implemented");
}#No

~public void behaviour() throws Exception {
   System.out.println("Behaviour Implemented");
}#No

~public void behaviour() throws IOException {
   System.out.println("Behaviour Implemented");
}#No

}



::CoreJava-Exceptions-021::Consider the following code:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

class SuperClass {
   void method() throws IOException {
      System.out.println("Super Class");
   }
}

class SubClass extends SuperClass {
/* CODE */
}

public class TestSuper {
   public static void main(String args[]) {
      try {
         SuperClass s = new SubClass();
         s.method();
      }catch(Exception e) {
         System.out.println("Exception caught");
      }
   }
}

Which the following listed options give the valid code snippet, that can be replaced for the comment /* CODE */ in the above code in order to compile and execute the program successfully?

1) void method() throws IOException {
   System.out.println("Sub Class");
   throw new FileNotFoundException();
   }
2) void method() throws FileNotFoundException {
   System.out.println("Sub Class");
   throw new FileNotFoundException();
   }
3) void method() {
   System.out.println("Sub Class");
   }
4) void method() throws Exception {
   System.out.println("Sub Class");
   throw new IOException();
   }
5) void method() throws Exception {
   System.out.println("Sub Class");
   throw new FileNotFoundException();
   }{

~3,4,5#No

~2,3,4#No

=1,2,3#Correct

~1,4,5#No

~1,2,5#No

}



::CoreJava-Exceptions-022::Consider the following code:

public class TestFinallyTry {
   public static void main(String args[]) {
      try {
         throw new NullPointerException();
      } catch(IndexOutOfBoundsException e) {
         System.out.println("Exception caught");
      } finally {
         try {
            System.out.println("Test");
         } catch(NullPointerException e) {
            System.out.println("NullPointerException caught");
         }
      }
   }
}

Which of the following option gives the output for the above code?{

~Prints:
Test
NullPointerException caught#No

=Prints:
Test
then throws NullPointerException on the console#Correct

~Prints:
Exception caught
Test#No

~Prints:
Exception caught
NullPointerException caught#No

}



::CoreJava-Exceptions-023::Consider the following code:

public class CheckedUnchecked {
   public static void main(String args[]) {
      try {
         System.out.println("try");
      } catch(/* CODE */ e) {
         System.out.println("Exception");
      }
   }
}

Which of the following code snippets when replaced for the comment /* CODE */ in the above program will compile and execute properly?

1) java.lang.Error
2) java.io.IOException
3) java.lang.Throwable
4) java.lang.Exception
5) java.sql.SQLException{

~2,3,4#No

~1,2,3#No

=1,3,4#Correct

~3,4,5#No

~1,4,5#No

}



::CoreJava-Exceptions-024::Consider the following code:

class UniverseException extends Exception { }
class PlanetXException extends UniverseException { }
class PlanetYException extends UniverseException { }

public class ExceptionWorld {
   public static void main(String args[]) {
      try {
         planetXExceptionThrower();
         planetYExceptionThrower();  
      }
      catch(PlanetXException px) { System.out.println("Problem in PlanetX"); }
      catch(PlanetYException py) { System.out.println("Problem in PlanetY"); }
      catch(UniverseException ue) { System.out.println("Problem in Universe"); }
   }

   static void planetXExceptionThrower() throws UniverseException {
      throw new PlanetXException();
   }
   static void planetYExceptionThrower() throws UniverseException {
      throw new PlanetYException();
   }
}

Which of the following option gives the output for the above code?{

~Prints:
Problem in Universe#No

~Prints:
Problem in Universe
Problem in Universe#No

~Prints:
Problem in PlanetX
Problem in PlanetY#No

=Prints:
Problem in PlanetX#Correct

}



::CoreJava-Exceptions-025::Consider the following code:

class AllException extends Exception { }
class SpecificException1 extends AllException { }
class SpecificException2 extends AllException { }

public class SpecificExceptionWorld {
   public static void main(String args[]) {
      try {
         specifcXception1Thrower();
         specifcXception2Thrower();  
      }
      catch(SpecificException1 sp1) { System.out.println("Specific Problem 1"); }
      catch(SpecificException2 sp2) { System.out.println("Specific Problem 2"); } 
   }

   static void specifcXception1Thrower() throws AllException {
      throw new SpecificException1();
   }

   static void specifcXception2Thrower() throws AllException  {
      throw new SpecificException2();
   }
}

Which of the following option gives the output for the above code?{

~Prints:
Specifc Problem 1
Specifc Problem 2#No

~Prints:
Specifc Problem 1#No

~Prints:
Specifc Problem 2#No

=Compilation Error#Correct

~Runtime Error#No

}



::CoreJava-Exceptions-026::Consider the following code:

1   abstract class SuperException extends Throwable { }
2   class SpecialException extends SuperException { }
3
4   public class TestAbstractThrowable {
5      public static void main(String args[]) {
6         try {
7            testAbstract();
8         } catch(SuperException ae) {
9            System.out.println("SuperException caught");
10        }
11     }
12
13     static void testAbstract() {
14        throw new SpecialException();
15     }
16  }

Which of the following statements are true regarding the above code?

1) Shows Unreachable catch block for SuperException, at line number 8
2) Shows Unhandled exception type SpecialException, at line number 13
3) No error in the code
    Prints:
    SuperException caught
4) Shows Unreachable catch block for SuperException, at line number 7
5) Shows Unhandled exception type SpecialException, at line number 14{

~1,2#No

=1,5#Correct

~2,3#No

~3,4#No

~3,5#No

}



::CoreJava-Exceptions-027::Consider the following code:

1   abstract class TopException extends RuntimeException { }
2   public class TestAnonymousException {
3      public static void main(String args[]) {
4         try {
5            testAbstract();
6         } catch(Exception ae) {
7            System.out.println("TopException caught");
8         }
9      }
10
11     static void testAbstract() throws Exception {
12        throw new TopException() { };
13     }
14  }

Which of the following option gives the output for the above code?{

~Shows, Unreachable catch block for Exception, at line number 6#No

~Shows, cannot create anonymous user defined exception class, at line number 12#No

~Shows, RuntimeException at line number 5#No

=No error in the code
Prints:
TopException caught#Correct

}



::CoreJava-Exceptions-028::Consider the following code:

public class TestRecException {
   public static void main(String args[]) { 
      testRecException(3); 
   }
 
   static void testRecException(int n) throws RuntimeException {
      try {
         if(n == 3)
            throw new NullPointerException("NullPointerException");
         else if(n == 2)
            throw new IllegalArgumentException("IllegalArgumentException");
         else if(n == 1)
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
         testRecException(n - 1);
      } catch(RuntimeException re) {
         System.out.println(re.getMessage());
      } finally {
         System.out.println("finally");
      } 
   }
}

Which of the following option gives the output for the above code?{

~finally
NullPointerException
finally
IllegalArgumentException
finally
IndexOutOfBoundsException#No

=NullPointerException
finally#Correct

~IndexOutOfBoundsException
finally
IllegalArgumentException
finally
NullPointerException
finally#No

~NullPointerException
finally
IllegalArgumentException
finally
IndexOutOfBoundsException
finally#No

}



::CoreJava-Exceptions-029::Consider the following code:

public class TestRecursiveRuntime {
   public static void main(String args[]) { 
      testRecRuntime(3); 
   }

   static void testRecRuntime(int n) throws RuntimeException {
      try {
         if(n > 0)
            testRecRuntime(n - 1);
         if(n == 0)
            throw new RuntimeException("RuntimeException");
         else if(n == 1)
            throw new NullPointerException("NullPointerException");
         else if(n == 2)
            throw new IndexOutOfBoundsException("IndexOutOfBoundsException");
         else if(n == 3)
            throw new IllegalArgumentException("IllegalArgumentException");  
      } catch(IllegalArgumentException ie) {
         System.out.println("IllegalArgumentException");
      } catch(IndexOutOfBoundsException ie) {
         System.out.println("IndexOutOfBoundsException");
      } catch(NullPointerException ne) {
         System.out.println("NullPointerException");
      } catch(RuntimeException re) {
         System.out.println("RuntimeException");
      }
   }
}

Which of the following option gives the output for the above code?{

~IllegalArgumentException
IndexOutOfBoundsException
NullPointerException
RuntimeException#No

~RuntimeException
RuntimeException
RuntimeException
RuntimeException#No

~RuntimeException#No

=RuntimeException
NullPointerException
IndexOutOfBoundsException
IllegalArgumentException#Correct

}



::CoreJava-Exceptions-030::Consider the following code:

public class TestRecRuntimeException {
   public static void main(String args[]) { 
      testRecRuntime(3); 
   }
 
   static void testRecException(int n) throws RuntimeException {
      try {
         if(n > 0)
            testRecException(n - 1);
         throw new RuntimeException("RuntimeException");
      } catch(RuntimeException re) {
         System.out.println("RuntimeException");
      }
   }
}

Which of the following option gives the output for the above code?{

~RuntimeException
RuntimeException#No

~RuntimeException
RuntimeException
RuntimeException#No

=RuntimeException
RuntimeException
RuntimeException
RuntimeException#Correct

~RuntimeException#No

}



::CoreJava-Exceptions-031::Consider the following code:

public class HierarchyCommunicator {
   public static void main(String args[]) {
      try {
         communicator1();
      } catch(Throwable e) {
         System.out.println("ArrayIndexOutOfBounds caught");
      }
   }
   static void communicator1() throws /* CODE 1 */ { communicator2(); }
   static void communicator2() throws /* CODE 2 */ { communicator3(); }
   static void communicator3() throws /* CODE 3 */ {
      throw new ArrayIndexOutOfBoundsException();
   }
}

Which of the following code snippets when replaced to those comments /* CODE 1 */ /* CODE 2 */ and /* CODE 3 */ in the above code will make the program to generate the output "ArrayIndexOutOfBounds caught"?

1) CODE 1 - Exception
    CODE 2 - RuntimeException
    CODE 3 - IndexOutOfBoundsException
2) CODE 1 - Exception
    CODE 2 - IndexOutOfBoundsException
    CODE 3 - RuntimeException
3) CODE 1 - IndexOutOfBoundsException
    CODE 2 - RuntimeException
    CODE 3 - Exception
4) CODE 1 - RuntimeException
    CODE 2 - IndexOutOfBoundsException
    CODE 3 - Exception
5) CODE 1 - RuntimeException
    CODE 2 - Exception
    CODE 3 - IndexOutOfBoundsException{

~2,3#No

=1,2#Correct

~3,4#No

~4,5#No

~1,5#No

}



::CoreJava-Exceptions-032::Consider the following code:

import java.io.IOException;
import java.io.FileNotFoundException;

public class TestThrowInFinally {
   public static void main(String args[]) {
      try {
         method1();
      } catch(Exception e) {
         System.out.println(e.getMessage());
      }
   }
   static void method1() throws IOException {
      try {
         method2();
      } finally {
         System.out.println("finally method1");
         throw new IOException("IOException");
      }
   }
   static void method2() throws FileNotFoundException {
      throw new FileNotFoundException("FileNotFoundException"); 
   }
}

Which of the following option gives the output for the above code?{

~FileNotFoundException
finally method1#No

~finally method1
FileNotFoundException#No

~finally method1#No

=finally method1
IOException#Correct

}



::CoreJava-Exceptions-033::Consider the following code:

01  public class TestReturnInFinally {
02     public static void main(String args[]) {
03        System.out.println(testMethod());
04     }
05     static void testMethod() {
06        try {
07           try {
08              System.out.println("Returning from inner try");
09              return 100;
10           } finally {
11              System.out.println("Returning from inner finally");
12              return 200;
13           }
14        } finally {
15           System.out.println("Returning from outer finally");
16           return 300;
17        }
18     }
19  }

Which of the following option gives the output for the above code?{

~Returning from inner try
100#No

~Returning from inner try
Returning from inner finally
200#No

=Returning from inner try
Returning from inner finally
Returning from outer finally
300#Correct

~Shows compile time error 'Unreachable code at line numbers 11 and 15'#No

~Shows compile time error 'Unreachable code at line number 15'#No

}



No comments:

Post a Comment