Sail E0 Webinar

MCQs

Total Questions : 61 | Page 6 of 7 pages
Question 51.


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();
}
}
  1.    BD
  2.    BCD
  3.    BDE
  4.    BCDE
 Discuss Question
Answer: Option C. -> BDE

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.


Question 52.

Which of these exceptions handles the divide by zero error?


  1.    ArithmeticException
  2.    MathException
  3.    IllegalAccessException
  4.    IllegarException
 Discuss Question
Answer: Option A. -> ArithmeticException

None.


Question 53.

Which of these packages contain all the Java’s built in exceptions?


  1.    java.io
  2.    java.util
  3.    java.lang
  4.    java.net
 Discuss Question
Answer: Option C. -> java.lang

 None.


Question 54.


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 */
}
}
  1.    ABCD
  2.    Compilation fails.
  3.    C is printed before exiting with an error message.
  4.    BC is printed before exiting with an error message.
 Discuss Question
Answer: Option C. -> C is printed before exiting with an error message.

Error is thrown but not recognised line(22) because the only catch attempts to catch an 

Exception and Exception is not a superclass of Error. Therefore only the code in the finally 

statement can be run before exiting with a runtime error (Exception in thread "main" java.

lang.Error).


Question 55.

Which of these class is related to all the exceptions that cannot be caught?


  1.    Error
  2.    Exception
  3.    RuntimeExecption
  4.    All of the mentioned
 Discuss Question
Answer: Option A. -> Error

Error class  is related to  java run time error that can't be caught usually, 

RuntimeExecption is subclass of Exception class which contains all the

exceptions that can be caught.



Question 56.


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");
  1.    finished
  2.    Exception
  3.    Compilation fails.
  4.    Arithmetic Exception
 Discuss Question
Answer: Option C. -> Compilation fails.

Compilation fails because ArithmeticException has already been caught. ArithmeticException 

is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has 

already been caught by the Exception class.

If ArithmeticException appears before Exception, then the file will compile. When catching 

exceptions the more specific exceptions must be listed before the more general (the subclasses 

must be caught before the superclasses).


Question 57.

Which of these class is related to all the exceptions that can be caught by using catch?


  1.    Error
  2.    Exception
  3.    RuntimeExecption
  4.    All of the mentioned
 Discuss Question
Answer: Option B. -> Exception

Error class is related to java run time error that  can't be  caught usually, 

RuntimeExecption is subclass of Exception class which contains all the

exceptions that can be caught.



Question 58.

Which of these clause will be executed even if no exceptions are found?


  1.    throws
  2.    finally
  3.    throw
  4.    catch
 Discuss Question
Answer: Option B. -> finally

finally keyword is used to define a set of instructions that will be executed irrespective 

of the exception found or not.


Question 59.

A single try block must be followed by which of these?


  1.    finally
  2.    catch
  3.    finally & catch
  4.    None of the mentioned
 Discuss Question
Answer: Option C. -> finally & catch

try block can be followed by any of finally or catch block, try block checks for exceptions 

Question 60.


What will be the output of the program?


public class Foo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
  1.    Finally
  2.    Compilation fails.
  3.    The code runs with no output.
  4.    An exception is thrown at runtime.
 Discuss Question
Answer: Option A. -> Finally

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.

I suppose the last three could be classified as VM shutdown.


Latest Videos

Latest Test Papers