Exceptions QuizJ8 Home « Exceptions Quiz

The questions in this Java quiz are on the topics covered in the java - Exceptions section of the site. The table below lists the lessons, a description of the lesson content and the quiz question number range.

Lesson Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that lesson.

Flow Control Lessons Description Question Range
Lesson 1 - Exception OverviewWe begin our studies of Java exceptions by looking at the Java exception hierarchy and the various classes within it.1 - 4
Lesson 2 - Handling ExceptionsNow we know what the Java exception hierarchy looks like and the classes involved its time to start handling exceptions which may occur within our code.5 - 11
Lesson 3 - Declaring ExceptionsIn this lesson we learn how to use the throw keyword which allows to throw exceptions from within our methods. We also investigate how to declare exceptions using the throws keyword.12 - 18
Lesson 4 - Creating Our Own ExceptionsIn this lesson we learn how to create our own exceptions and use them in our code.
Lesson 5 - Using AssertionsFor this lesson we learn about the Assertion mechanism and how to write and then enable any assertion code within our programs.19 - 25

Java Quiz

The quiz below tests your knowledge of the material learnt in the java - Exceptions section of the site.

Question 1 : Which class is at the top of the inheritance hierarchy
- The <code>Throwable</code> class is at the top of the inheritance hierarchy.
Question 2 : What type of exceptions do we have to declare in our code?
- We have to declare Checked exceptions in our code or the compiler complains.
Question 3 : What type of exception classes can we as programmers do nothing about?
- The <code>Error</code> class and its subclasses handle error situations from within the JVM itself and so are outside our control as programmers.
Question 4 : What do we have to do with checked exceptions?
- We have to declare checked exceptions.
Question 5 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try {
int a = 5 / 0;
}
System.out.println("We can just continue on now.");
}
}
- We will get a compiler error as we cannot have a <code>try</code> without a <code>catch</code> or a <code>finally</code>.
Question 6 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; System.out.print("A."); }
catch (Exception ex) { System.out.print("B."); }
System.out.print("C.");
}
}
- As soon as the exception occurs in the <code>try</code> block it is caught in the <code>catch</code> block which prints B. then the program continues and prints out C. so the answer is B.C.
Question 7 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; }
System.out.print("A.");
catch (Exception ex) { System.out.print("B."); }
System.out.print("C.");
}
}
- A <code>catch</code> block must immediately follow a <code>try</code> block and there is a statement between them so we get a compiler error.
Question 8 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; }
finally { System.out.print("A."); }
System.out.print("B.");
}
}
- The <code>finally</code> block runs and prints A. and then the program crashes so we get A. followed by the runtime error output
Question 9 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; }
finally { System.out.print("A."); }
catch (Exception ex) { System.out.print("B."); }
System.out.print("C.");
}
}
- The <code>finally</code> block must come immediately after any <code>catch</code> blocks and not before.
Question 10 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; }
catch (Exception ex) { System.out.print("B."); }
System.out.print("C.");
finally { System.out.print("A."); }
}
}
- There can be no other code between the <code>try</code>, <code>catch</code> and <code>finally</code> blocks so we get a compiler error.
Question 11 : What would be output from the following code snippet?

public class DivideByZero {
public static void main(String[] args) {
try { int a = 5 / 0; }
catch (Exception ex) { System.out.print("B."); }
catch (ArithmeticException ex) { System.out.print("C."); }
finally { System.out.print("A."); }
System.out.print("D.");
}
}
- We get a compiler error. When using multiple <code>catch</code> blocks, they need to be ordered from the most specific to the most generic.
Question 12 : How do we declare exceptions in our methods?
- We declare exceptions in our methods using the <code>throws</code> keyword.
Question 13 : What exceptional conditions can we throw from within our code?
- We can <code>throw</code> any exceptional conditions from within our code, so all of the above.
Question 14 : What does the compiler demand we do with checked exceptions?
- The compiler demands that we handle or declare <i>checked exceptions</em>.
Question 15 : What happens when an exception occurs in a method that declares but doesn't handle the exception?
- When an exception occurs in a method that declares but doesn't handle the exception, the exception propogates up the stack
Question 16 : An overriding method can throw any Error or RuntimeException exceptions, whether these are declared in the overridden method or not.
- The above statement is <code>true</code>.
Question 17 : An overriding method can throw any new checked exceptions or any checked exceptions that are higher up the inheritance tree than those declared in the overridden method.
- An <i>overriding</em> method MUST NOT throw any new <i>checked exceptions</em> or any <i>checked exceptions</em> that are higher up the inheritance tree than those declared in the <i>overridden</em> method, so the above statement is <code>false</code>.
Question 18 : An overriding method can throw any checked exceptionss that are lower down the inheritance tree than those declared in the overridden method but this isn't mandatory.
- The above statement is <code>true</code>.
Question 19 : Assertions should be used to validate command-line arguments.
- We certainly can use assertions to validate command-line arguments. The problem with this approach is that would have to run the code everytime using the -ea option to ensure assertions run the validation.
Question 20 : What should we use to validate arguments to a public method.
- We have no control over a public interface and so we should NOT use assertions. We should use exceptions for validating arguments to a public method.
Question 21 : Assertions should never be used that cause side-effects?
- This is <code>true</code> as assertions should never be used that cause side-effects. Imagine changing a value when assertions are turned on, we are reliant on code being run using the <code>-ea</code> option to get the values changed.
Question 22 : In what situation would we use assert false;?
- We would use <code>assert false;</code> for cases that are never supposed to happen, such as a <code>switch default</code> that should never be reached.
Question 23 : Is the following code snippet an appropriate use of assertions?

public class A {
public void methodA(int a) {
assert (a > 0)
// more code
}
}
- The above code snippet is not an appropriate use of assertions as we have no control over usage of a public interface. Use exceptions to validate input to a <code>public</code> method.
Question 24 : Is the following code snippet an appropriate use of assertions?

public class A {
private void methodA(int a) {
assert (a > 0)
// more code
}
}
- The above code snippet is an appropriate use of assertions as we should have complete control over usage of a <code>private</code> method.
Question 25 : Assertions have no 'hit on performance?
- Assertion code leaves no footprint and is just ignored when a class is run without the <code>-ea</code> option, so there is no 'hit' on performance.
Quiz Progress Bar Please select an answer

What's Next?

In the next quiz we test knowledge of the java API.