Java SE 11: Exception Handling

--

Some basic interview question for Exception handling.

Java SE 11: Exception Handling

Objective type Question

1. Select the condition that describes a Throwable class of type Error.

a) An exceptional condition that are external to the application

b) A programming error caused by invalid syntax

c) An error for an invalid array index

2. What happens to an exception when it is propagated all the way up the call stack without being handled?

a) The error is ignored and the program terminates

b) The jvm calls System.exit with the error status

c) The jvm outputs the exception and a stack trace for the exception

3. Select the methods used by Java to handle unexpected events.

a) System.exit is called

b) An exception is thrown to the calling method

c) An abnormal terminal occurs

4. What is a checked exception?

a) An exception that the caller is forced to catch or rethrow

b) An exception that cannot be recovered from

c) An exception that is always thrown

5. What are the poor exception handling practices in the following try/catch block code snippet?

try 
{
createFile(“/path/to/file”);
}
catch(Exception e) {
System.out.println(“Error creating file.”);
}

a) The catch clause catching an Exception type rather the specific type

b) The catch clause does not throw the Exception

c) The createFile method should not be in the try block

6. Which exceptions are shown in the throws section of a method’s documentation in the Java API?

a) Unchecked exceptions

b) All exception that could result from the method

c) Exceptions that the method can throw

7. What is the output from the following try/catch block?

try { 
int[] myIntArray = new int[5];
myIntArray[5] = 25;
} catch (IOException ioe) {
System.out.println(“one”);
} catch (IllegalArgumentException iae) {
System.out.println(“two”);
} catch (OutOfMemoryError oom) {
System.out.println(“three”);
} catch (Exception e) {
System.out.println(“four”);
}

a) two

b) four

c) one

8. What options does NetBeans provide for handling checked exceptions?

a) Add throws to the method signature.

b) Add a throw new Exception statement

c) Add an if statement to prevent the exception

9. If a checked exception is not caught, how can it be sent higher up the call stack?

a) By declaring the calling method with throws exception as well

b) By using a try block without a corresponding check block

c) By prefixing the method call with a throw statement

10. What code is intended to be wrapped in a try block?

a) Code that catches a specific exception

b) All Java code should be wrapped in try blocks

c) Code that might throw an exception

11. What is the unwinding of the sequence of method calls typically referred to as?

a) The segmentation fault

b) The stack trace

c) The error queue

12. When does the finally block get performed when included with a try/catch block?

a) Always after the try and catch blocks, if any, have been executed

b) Immediately after the try block

c) Only when an exception goes uncaught by the catch blocks

Answer:-

  1. a
  2. c
  3. b
  4. a
  5. a
  6. c
  7. b
  8. a
  9. a
  10. c
  11. b
  12. a

--

--

Pragati Singh ⭐️⭐️⭐️⭐️⭐️
erpragatisingh

Android Developer Advocate & Architect ✔Technology Leader ✔Life Coach #techentrepreneur #polyglot programer #BuildBetterApp