Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 9

Chapter 9

Exception Handling

This activity contains 15 questions.

Question 1.
Each catch clause that follows a try block is called:


Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.

What is printed?
class MyException extends Exception {

	MyException() throws MyException {
		System.out.println("one");
		throw new MyException("three");
	}

	MyException(String s) {
		System.out.println("two: " + s);
	}

}

public class Except {

	Except() {
		try {
			throw new MyException();
		} catch (MyException e) {
			System.out.println("MyException:
                               " + e);
		} catch (Exception e) {
			System.out.println("Exception: " 
                               + e);
		} finally {
			System.out.println("finally");
		}
	}

	public static void main(String[] args) 
                        throws MyException {
		new Except();
		System.out.println("done");
	}

}

Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.

What is printed?
class MyException extends Exception {

	MyException() {
		super();
		System.out.println("MyException");
	}

	MyException(String s) {
		super(s);
		System.out.println("MyException: " 
                           + s);
		System.exit(0);
	}

}

public class Except {

	public static void main(String[] args) {
		new MyException("Testing");
		new MyException();
	}

}

Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.

What is printed?
class MyException extends Exception {

	MyException() {
		this("Testing");
		System.out.println("one");
	}

	MyException(String s) {
		super(s);
		System.out.println("two: " + s);
	}

}

public class Except {

	Except() {
		try {
			throw new MyException();
		} catch (Exception e) {
			System.out.println("Caught: " 
                               + e);
		}
	}

	public static void main(String[] args)
                        throws MyException {
		new Except();
		System.out.println("done");
	}

}

Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

What is printed?
class MyException extends Exception {

	MyException() {
		System.out.println("one");
	}

	MyException(String s) {
		System.out.println("two: " + s);
	}

}

public class Except {

	Except() {
		try {
			throw new MyException("Test");
		} catch (MyException e) {
			System.out.println("MyException:
                               " + e);
		} catch (Exception e) {
			System.out.println("Exception: "
                               + e);
		}
	}

	public static void main(String[] args)
                        throws MyException {
		new Except();
		System.out.println("done");
	}

}

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.

What is printed?
class MyException extends Exception {

	MyException() {
		System.out.println("one");
	}

	MyException(String s) {
		System.out.println("two: " + s);
	}

}

public class Except {

	Except() {
		try {
			throw new MyException("Test");
		} catch (MyException e) {
			System.out.println("MyException:
                               " + e);
		} catch (Exception e) {
			System.out.println("Exception: " 
                               + e);
		} finally {
			System.out.println("finally");
		}
	}

	public static void main(String[] args)
                        throws MyException {
		new Except();
		System.out.println("done");
	}

}

Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What is printed?
class MyException extends Exception {

	MyException() {
		System.out.println("one");
	}

	MyException(String s) {
		System.out.println("two: " + s);
	}

}

public class Except {

	Except() {
		try {
			if (4 > 40)
				throw
					new MyException("Test");
		} catch (MyException e) {
			System.out.println("MyException:
                               " + e);
		} catch (Exception e) {
			System.out.println("Exception: " 
                               + e);
		} finally {
			System.out.println("finally");
		}
	}

	public static void main(String[] args)
                        throws MyException {
		new Except();
		System.out.println("done");
	}

}

Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.

What is printed?
public class Except {

	Except() {
		for (int i=0; i<5; i++)
			tryme(i);
	}

	private void tryme(int n) {
		try {
			if (n % 3 == 0)
				throw new Exception(n + 
                 " is a multiple of three");
			else
				System.out.println("n = " +
                                    n);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static void main(String[] args) {
		new Except();
	}

}

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What is printed?
public class Except {

	Except() {
		for (int i=0; i<5; i++)
			try {
				tryme(i);
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
	}

	private void tryme(int n)
                         throws Exception {
		if (n % 3 != 0)
			throw new Exception(n + 
             " is not a multiple of three");
		
			System.out.println("n = " + n);
	}

	public static void main(String[] args) {
		new Except();
	}

}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.

What is printed?
public class Exceptions {

	Exceptions() {
		try {
			for (int n = 3; n >= -3; n--)
				System.out.println(100 / n);
		} catch (ArithmeticException e) {
			System.out.println("0-Divide!");
		} finally {
			System.out.println("Done");
		}
	}

	public static void main(String[] args) {
		new Exceptions();
	}
}

Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.

Which of the following are true?
  1. If an exception is thrown within a try block, the finally clause always executes.
  2. If no exception is thrown within a try block, the finally clause always executes.
  3. If an exception is thrown within an exception handler, the finally clause always executes.
  4. If an exception is thrown within a finally block, the exception is propagated out of the finally block, and the finally block does not complete its execution.

Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.
Which of the following is NOT a Timer method?


Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.

Which statement(s) are correct?
  1. If a method may throw an exception, it must either catch it or use a throws clause
  2. If a method may throw an exception, it must catch it
  3. A method may include a throws clause for an exception that it does not throw
  4. A method may not include a throws clause unless it actually throws an uncaught exception

Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.

Which statements are true?
  1. Unchecked exceptions should be handled by the code you write.
  2. Checked exceptions must be handled by code that you write.
  3. You need not provide an exception handler for an unchecked exception.
  4. You need not provide an exception handler for a checked exception.
  5. You need not provide a catch clause for a checked exception if you provide a throws clause.
  6. Every Java program has the potential for throwing an unchecked exception.

Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.
Why don't you need to include a try, catch, or throws for an ArrayIndexOutOfBoundsException?


Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page