Sail E0 Webinar

MCQs

Total Questions : 61 | Page 5 of 7 pages
Question 41.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
int a, b;
b = 0;
a = 5 / b;
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
  1.    A
  2.    B
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option B. -> B

None.
Output:
$ javac exception_handling.java
$ java exception_handling
B


Question 42.


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() {}
}
  1.    AC
  2.    BC
  3.    ACD
  4.    ABCD
 Discuss Question
Answer: Option C. -> ACD

There is no exception thrown, so all the code with the exception of the catch statement block is run.


Question 43.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
throw new NullPointerException ("Hello");
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
  1.    A
  2.    B
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option D. -> Runtime Error

 try block is throwing NullPointerException but the catch block is used to counter 

Arithmetic Exception. Hence NullPointerException occurs since no catch is there

which can handle it, runtime error occurs.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.NullPointerException: Hello



Question 44.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
System.out.print("Hello" + " " + 1 / 0);
}
finally {
System.out.print("World");
}
}
}
  1.    Hello
  2.    World
  3.    Compilation Error
  4.    First Exception then World
 Discuss Question
Answer: Option D. -> First Exception then World

None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" java.lang.ArithmeticException: / by zero
World



Question 45.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
}
catch (ArithmeticException e) {
System.out.println("1");
}
}
}


Note : Execution command line : $ java exception_handling


  1.    0
  2.    1
  3.    Compilation Error
  4.    Runtime Error
 Discuss Question
Answer: Option B. -> 1

None.
Output:
$ javac exception_handling.java
$ java exception_handling
1

Question 46.


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.print("finally "); /* Line 11 */
}
}
public static void main(String args[])
{
try
{
aMethod();
}
catch (Exception e) /* Line 20 */
{
System.out.print("exception ");
}
System.out.print("finished"); /* Line 24 */
}
}
  1.    finally
  2.    exception finished
  3.    finally exception finished
  4.    Compilation fails
 Discuss Question
Answer: Option C. -> finally exception finished

This is what happens:

(1) The execution of the try block (line 5) completes abruptly because of the throw statement 

(line 7).

(2) The exception cannot be assigned to the parameter of any catch clause of the try statement

 therefore the finally block is executed (line 9) and "finally" is output (line 11).

(3) The finally block completes normally, and then the try statement completes abruptly because of the throwstatement (line 7).

(4) The exception is propagated up the call stack and is caught by the catch in the main method (line 20). 

This prints "exception".

(5) Lastly program execution continues, because the exception has been caught, and "finished" is output 

(line 24).


Question 47.

Which of these keywords is used to manually throw an exception?


  1.    try
  2.    finally
  3.    throw
  4.    catch
 Discuss Question
Answer: Option C. -> throw

None.



Question 48.


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 ");
}
}
  1.    hello throwit caught
  2.    Compilation fails
  3.    hello throwit RuntimeException caught after
  4.    hello throwit caught finally after
 Discuss Question
Answer: Option D. -> hello throwit caught finally after

The main() method properly catches and handles the RuntimeException in the catch block, 

finally runs (as it always does), and then the code returns to normal.

A, B and C are incorrect based on the program logic described above. Remember that properly 

handled exceptions do not cause the program to stop executing.


Question 49.

Which of these exceptions will occur if we try to access the index of an array 

beyond its length?


  1.    ArithmeticException
  2.    ArrayException
  3.    ArrayIndexException
  4.    ArrayIndexOutOfBoundsException
 Discuss Question
Answer: Option D. -> ArrayIndexOutOfBoundsException

ArrayIndexOutOfBoundsException is a built in exception that is caused when we try

 to access an index location which is beyond the length of an array.


Question 50.

Which of these handles the exception when no catch is used?


  1.    Default handler
  2.    finally
  3.    throw handler
  4.    Java run time system
 Discuss Question
Answer: Option A. -> Default handler

None.


Latest Videos

Latest Test Papers