Alien head

Basics of Exception Handling in Java

Description: This quiz has basics of Exception Handling questions
Number of Questions: 15
Created by:
Tags: Exception Handling Java

What would be the output of the following program?

Class A {
 public static void main(String args[]) {
  int a = 0;
  int b = 10 / a;
 }
}
  1. 0

  2. 10

  3. Infinite

  4. RuntimeException

  5. Compile Time Exception

Answer: 4
Explanation:

This is the correct answer. This piece of code will result in a RuntimeException. ArithmeticException is the exception that will be thrown.

Which of the following statements is false regarding 'finally' keyword in Java?

  1. Finally block does not get executed in case an exception does not occur.

  2. There can be multiple finally blocks in a Java method.

  3. Finally block is optional.

  4. A try statement without a catch block must be followed by a finally block.

  5. The finally block executes even if no catch block executes.

Answer: 1
Explanation:

This is the correct answer. The given statement is false. The finally block executes even if an exception does not occur.

Which of the following is not an unchecked exception?

  1. ArithmeticException

  2. NullPointerException

  3. NumberFormatException

  4. ArrayIndexOutOfBoundsException

  5. IOException

Answer: 5
Explanation:

This is a checked exception. Such exceptions are checked at the compile time.

Which of the following keywords is not associated with Java exception handling?

  1. try

  2. catch

  3. throw

  4. finally

  5. final

Answer: 5
Explanation:

This is a correct answer. This keyword is not associated with Java exception handling. This keyword when used with different class members behaves differently. For example, when used with a variable it makes the variable a constant.

Which of the following is responsible for the occurence of exceptions?

  1. Syntactical errors

  2. Symentical errors

  3. Lexical errors

  4. Pass 1 error of the compiler

  5. Program load time error

Answer: 2
Explanation:

These errors are runtime errors due to wrong calculations or wrongly understood expressions. Such errors are identified as exceptions during runtime.

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

Answer: 4
Explanation:

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

Which of the following methods is used in Java to throw an exception explicitly in a program?

  1. Throws

  2. Throw

  3. Finally

  4. None of these

Answer: 2
Explanation:

This method is used in Java to throw an exception explicitly in a program.

Which of the following methods of Thread class waits for a thread to terminate?

  1. isAlive()

  2. join()

  3. run()

  4. sleep()

  5. start()

Answer: 2
Explanation:

This method of Thread class waits for a thread to terminate.

Which of the following Exception classes is used to deal with an exception of an attempt to violate the security?

  1. NullPointerException

  2. ClassNotFoundException

  3. IllegalAccessException

  4. InterruptedException

  5. SecurityException

Answer: 5
Explanation:

This Exception class is used to deal with an exception of an attempt to violate security.

Which of the following blocks in Java is executed regardless of whether an exception is thrown or not?

  1. try block

  2. catch block

  3. finally block

  4. final

  5. private

Answer: 3
Explanation:

This block in Java is executed regardless of whether an exception is thrown or not.

Which of the following methods of Throwable class sends the stack trace to the specified stream?

  1. printStackTrace()

  2. fillInStackTrace()

  3. printStackTrace(PrintStream stream)

  4. toString()

  5. getLocalizedMessage()

Answer: 3
Explanation:

This method of Throwable class sends the stack trace to the specified stream.

Name the exception thrown by read method of the InputStream class

  1. IOException

  2. ReadException

  3. FileNotFoundException

  4. Exception

  5. All of the above

Answer: 1
Explanation:

This exception can be raised by the read method.

Which of the following is not a Java's checked exception?

  1. ClassNotFoundException

  2. ClassNotSupportedException

  3. InstantiationException

  4. UnsupportedOperationException

  5. NoSuchMethodException

Answer: 4
Explanation:

This is the correct answer. This is an unchecked exception. This occurs when an unsupported action gets encountered.

Which of the following classes is at the top of Exception class hierarchy?

  1. Exception

  2. Error

  3. Throwable

  4. RuntimeException

  5. IOException

Answer: 3
Explanation:

This is the correct answer. Throwable is at the top of Exception class hierarchy.

Which of the following Exception classes is used when a requested operation is not compatible with the current thread state?

  1. ArithmeticException

  2. IllegalStateException

  3. IllegalThreadStateException

  4. ArrayStoreException

  5. NegativeArraySizeException

Answer: 3
Explanation:

This Exception class is used when a requested operation is not compatible with the current thread state.

+ View questions