1

Test on Java Exception Handling

Description: Try this test on Exception Handling in java
Number of Questions: 15
Created by:
Tags: Exception Handling Programming Java
Attempted 0/15 Correct 0 Score 0

Which of the following methods of Iterator class throws NoSuchElementException, if the next element does not exist?

  1. hasNext()

  2. next()

  3. remove()

  4. add()

  5. nextIndex()


Correct Option: B
Explanation:

This method of Iterator class throws NoSuchElementException, if there is not a next element.

Which of the following exceptions is thrown when one thread has been interrupted by another thread?

  1. ClassNotFoundException

  2. IllegalAccessException

  3. InstantiationException

  4. InterruptedException

  5. NoSuchFieldException


Correct Option: D
Explanation:

This exception is thrown when one thread has been interrupted by another thread.

Which of the following Exception classes in Java is used to deal with an exception, where an assignment to an array element is of incompatible type?

  1. ArithmeticException

  2. ArrayIndexOutOfBoundsException

  3. IllegalArgumentException

  4. ArrayStoreException

  5. IllegalStateException


Correct Option: D
Explanation:

This Exception class in Java is used to deal with the exception, where an assignment to an array element is of incompatible type.

A programmer has created his own exception for balance in account <1000. The exception is created properly, and the other parts of the programs are correctly defined. Though the program is running but error message has not been displayed. Why did this happen?

  1. Because of the Throw portion of exception.

  2. Because of the Catch portion of exception.

  3. Because of the main() portion.

  4. Because of the class portion.

  5. None of the above


Correct Option: B
Explanation:

Though exception is opened and it is generating the exception also but since catch is missing, so the error is not acknowledged and displayed.

Which of the following exceptions is generated when a user tries to create an environment or application in an incorrect state?

  1. IllegalMonitorStateException

  2. IllegalArgumentException

  3. IllegalStateException

  4. None of these


Correct Option: C
Explanation:

This exception is generated when a user tries to create an environment or application in an incorrect state.

Which of the following exception classes is used in Java to deal with a requested method that does not exist in the program?

  1. NoSuchFieldException

  2. InstantiationException

  3. ClassNotFoundException

  4. NoSuchMethodException


Correct Option: D
Explanation:

This exception class is used in Java to deal with a requested method that does not exist in the program.

What will be the output of the program?

try 
{
    Float f1 = new Float(3.0);
    int x = f1.intValue();
    byte b = f1.byteValue();
    double d = f1.doubleValue();
    System.out.println(x + b + d);
}
catch (NumberFormatException e) 
{
    System.out.println(bad number);
}
  1. 9.0

  2. bad number

  3. compilation fails on line 13

  4. compilation fails on line 14


Correct Option: A
Explanation:

The xxxValue() methods convert any numeric wrapper object's value to any primitive type. When narrowing is necessary, significant bits are dropped and the results are difficult to calculate.

Which of the following statements is true?

  1. It is sometimes good practice to throw an AssertionError explicitly.

  2. Private getter() and setter() methods should not use assertions to verify arguments.

  3. If an AssertionError is thrown in a try-catch block, the finally block will be bypassed.

  4. It is proper to handle assertion statement failures using a catch (AssertionException ae) block.


Correct Option: A
Explanation:

Option 1 is correct because it is sometimes advisable to thrown an assertion error even if assertions have been disabled. Option 2 is incorrect because it is considered appropriate to check argument values in private methods using assertions. Option 3 is incorrect; finally is never bypassed. Option 4 is incorrect because AssertionErrors should never be handled.

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

Given that all methods of class FileOutputStream, including close(), throw an IOException, which of the following is true?

  1. This program will compile successfully.

  2. This program fails to compile due to an error at line 4.

  3. This program fails to compile due to an error at line 6.

  4. This program fails to compile due to an error at line 13.


Correct Option: D
Explanation:

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.

What will be the output of the following 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);  
        } 
    } 
}
  1. Ex0 caught

  2. exception caught

  3. Compilation fails because of an error it line 2

  4. Compilation fails because of an error in line 9


Correct Option: A
Explanation:

An exception Exc1 is thrown and is caught by the catch statement in line 11. 

What will be the output of the program?

public class ExamQuestion6 
{
    static int x; 
    boolean catch()
    {
        x++; 
        return true; 
    } 
    public static void main(String[] args)
    {
        x=0; 
        if ((catch() | catch()) || catch()) 
            x++; 
        System.out.println(x); 
    } 
}
  1. 1

  2. 2

  3. 3

  4. Compilation Fails


Correct Option: D
Explanation:

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

When attempting to compile and run the following code, the result will be

public class MyClass extends Thread{
       public MyClass(String s){ msg=s; }
       String msg;
       public void run(){
             System.out.println(msg);
       }
       public static void main(String[] args) {
             new MyClass(Hello);
             new MyClass(World);
       }
}
  1. The program will fail to compile.

  2. The program will compile without any errors and will print 'Hello' and 'World' everytime the program is run.

  3. The program will compile without errors and will print 'Hello' and 'World' but the order is unpredictable.

  4. The program will compile without any errors and will terminate without any output when run.


Correct Option: D
Explanation:

The program will compile without errors and will simply terminate without any output. Two threads are created but they will never be started. The start() method must be called on the thread objects to make the threads execute the run() method asynchronously.

What will be the output of the program?

public class X {

 public static void main(String[] args) {
  try {
   badMethod(); /* Line 5 */
   System.out.print("A");
  } catch (Exception ex) // Line 7
  {
   System.out.print("B"); // Line 9 
  } finally // Line 10 
  {
   System.out.print("C"); // Line 12
  }

  System.out.print("D"); // Line 15
 }
 public static void badMethod() {
  throw new RuntimeException();
 }
}
  1. AB

  2. BC

  3. ABC

  4. BCD


Correct Option: D
Explanation:

(1) A RuntimeException is thrown, this is a subclass of exception. (2) The exception causes the try to complete abruptly (line 5) therefore line 6 is never executed. (3) The exception is caught (line 7) and "B" is output (line 9) (4) The finally block (line 10) is always executed and "C" is output (line 12). (5) The exception was caught, so the program continues with line 15 and outputs "D".

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


Correct Option: C
Explanation:

Error is thrown but not recognised in line(18) 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).

In the following Java program, it was found that these statements have generated an exception of Array Index out of bound Exception.

int b[ ] = {10,20,30};
b[50] = 100 ;

Choose the correct catch statement for the above code.

  1. catch (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
    }
    
  2. catch (ArrayIndexOutOfBoundsException aie) {
     System.out.println("array size is less than what you are trying to access")
    }
    
  3. catch (aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
     }
    
  4. throw (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access ')
     }
    
  5. throw (aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access)
    }
    

Correct Option: A
Explanation:

Catch routines are defined in this form. 

- Hide questions