Test on Java Exception Handling

Try this test on Exception Handling in java

14 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

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()
Question 2 Multiple Choice (Single Answer)

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
Question 3 Multiple Choice (Single Answer)

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
Question 4 Multiple Choice (Single Answer)

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
Question 5 Multiple Choice (Single Answer)

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
Question 6 Multiple Choice (Single Answer)

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
Question 7 Multiple Choice (Single Answer)

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
Question 8 Multiple Choice (Single Answer)

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.
Question 9 Multiple Choice (Single Answer)
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.
Question 10 Multiple Choice (Single Answer)

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
Question 11 Multiple Choice (Single Answer)

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
Question 12 Multiple Choice (Single Answer)

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
Question 13 Multiple Choice (Single Answer)

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.
Question 14 Multiple Choice (Single Answer)

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. ```java
    catch (ArrayIndexOutOfBoundsException aie) {
    aie.printStackTrace();
    System.out.println("array size is less than what you are trying to access")
    }
  2. java catch (ArrayIndexOutOfBoundsException aie) { System.out.println("array size is less than what you are trying to access") }
  3. ```java
    catch (aie) {
    aie.printStackTrace();
    System.out.println("array size is less than what you are trying to access")
    }
  4. ```java
    throw (ArrayIndexOutOfBoundsException aie) {
    aie.printStackTrace();
    System.out.println(array size is less than what you are trying to access ')
    }
  5. ```java
    throw (aie) {
    aie.printStackTrace();
    System.out.println(array size is less than what you are trying to access)
    }