Deck 11: Handling Exceptions and Events
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
Play
Full screen (f)
Deck 11: Handling Exceptions and Events
1
Every program with a try block must end with a finally block.
False
2
The order in which catch blocks are placed in a program has no impact on which catch block is executed.
False
3
The class Throwable is derived from the class Exception.
False
4
When an exception occurs, an object of a particular exception class is created.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
The class Object is derived from the class Throwable.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
The class Exception and its subclasses are designed to catch exceptions that should be caught and processed during program execution.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
Statements that might generate an exception are placed in a catch block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
The methods getMessage and printStackTrace are private methods of the class Throwable.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
If you have a reference to an exception object, you can use the reference to throw the exception.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
During program execution, if division by zero occurs with integer values and is not addressed by the program, then the program terminates with an error message indicating an attempt to divide by zero.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
An unchecked exception is any exception that causes a program to terminate.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
Making a reference to an object that has not yet been instantiated would throw an exception from the NullPointerException class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
The class Exception contains two constructors.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
The try block contains statements that should be executed regardless of whether or not an exception occurs.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
The StringIndexOutOfBoundsException could be thrown by the method parseInt of the class Integer.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
If there is a finally block after the last catch block, the finally block always executes.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
If an exception occurs in a try block and that exception is caught by a catch block, then the remaining catch blocks associated with that try block are ignored.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
A checked exception is any exception checked for by the programmer.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
The class RuntimeException is part of the package java.io.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
NoSuchElementException is a method of the class Exception.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
The JList GUI component generates events that call the actionStateChanged method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
The class RuntimeException is the superclass of which of the following classes?
A) NullPointerException
C) IllegalAccessException
B) NoSuchMethodException
D) NoSuchFileException
A) NullPointerException
C) IllegalAccessException
B) NoSuchMethodException
D) NoSuchFileException
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
If you have created an exception class, you can define other exception classes extending the definition of the exception class you created.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
For interfaces such as WindowListener that contain more than one method, Java provides the class WindowAdapter.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
When an exception occurs in a method, you can use the method printStackTrace to determine the order in which the methods were called and where the exception was handled.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
A message is returned by which method of an Exception object?
A) printMessage()
C) printStackTrace()
B) getMessage()
D) traceMessage()
A) printMessage()
C) printStackTrace()
B) getMessage()
D) traceMessage()
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
Which of the following is NOT a method of the class Throwable?
A) getMessage
C) printStackTrace
B) throwMessage
D) toString
A) getMessage
C) printStackTrace
B) throwMessage
D) toString
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
How many finally blocks can there be in a try/catch structure?
A) There must be one finally block.
B) There can be one finally block following each catch block.
C) There can be zero or one finally blocks following the last catch block.
D) There is no limit to the number of finally blocks following the last catch block.
A) There must be one finally block.
B) There can be one finally block following each catch block.
C) There can be zero or one finally blocks following the last catch block.
D) There is no limit to the number of finally blocks following the last catch block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
What can a method do with a checked exception?
A) Check the exception or ignore it.
B) Return the exception to the sender or handle it in a catch block.
C) Throw the exception to the method that called this method, or handle the exception in a catch block.
D) Handle the exception in the try block or handle the exception in the catch block.
A) Check the exception or ignore it.
B) Return the exception to the sender or handle it in a catch block.
C) Throw the exception to the method that called this method, or handle the exception in a catch block.
D) Handle the exception in the try block or handle the exception in the catch block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
Which of the following exceptions might be thrown by the methods of the class String?
A) NullPointerException
C) NoSuchElementsException
B) FileNotFoundException
D) NumberFormatException
A) NullPointerException
C) NoSuchElementsException
B) FileNotFoundException
D) NumberFormatException
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
Which of the following statements is true?
A) The class Exception, which is derived from the class Object, is the superclass of the class Throwable.
B) The class Throwable, which is derived from the class Exception, is the superclass of the class Object.
C) The class Throwable, which is derived from the class Object, is the superclass of the class Exception.
D) None of these
A) The class Exception, which is derived from the class Object, is the superclass of the class Throwable.
B) The class Throwable, which is derived from the class Exception, is the superclass of the class Object.
C) The class Throwable, which is derived from the class Object, is the superclass of the class Exception.
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
When is a finally block executed?
A) Only when an exception is thrown by a try block
B) Only when there are no exceptions thrown
C) At the end of a program
D) Always after the execution of a try block, regardless of whether or not an exception is thrown
A) Only when an exception is thrown by a try block
B) Only when there are no exceptions thrown
C) At the end of a program
D) Always after the execution of a try block, regardless of whether or not an exception is thrown
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
Which of the following will cause the first exception to occur in the code in the accompanying figure?
A) If the divisor is zero
B) If the dividend is zero
C) If the quotient is zero
D) This code will not compile, so an exception cannot be triggered.
A) If the divisor is zero
B) If the dividend is zero
C) If the quotient is zero
D) This code will not compile, so an exception cannot be triggered.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
Which method throws the second exception in the code in the accompanying figure?
A) nextInt
C) println
B) toString
D) nextLine
A) nextInt
C) println
B) toString
D) nextLine
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
What happens in a method if there is an exception thrown in a try block but there is no catch block following the try block?
A) The program ignores the exception.
B) The program will not compile without a complete try/catch structure.
C) The program terminates immediately.
D) The program throws an exception and proceeds to execute the finally block.
A) The program ignores the exception.
B) The program will not compile without a complete try/catch structure.
C) The program terminates immediately.
D) The program throws an exception and proceeds to execute the finally block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
To handle window events, you first create a class that implements the interface WindowListener and then create and register objects of that class.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
Which of the following inputs would be caught by the second catch block in the program in the accompanying figure?
A) 0
C) h3
B) 10
D) -1
A) 0
C) h3
B) 10
D) -1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
If a negative value is used for an array index, ____.
A) a NumberFormatException is thrown
B) the program terminates automatically without throwing an exception
C) the last index of the array is automatically accessed instead
D) an ArrayIndexOutOfBoundsException is thrown
A) a NumberFormatException is thrown
B) the program terminates automatically without throwing an exception
C) the last index of the array is automatically accessed instead
D) an ArrayIndexOutOfBoundsException is thrown
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
How many constructors does the class Exception have?
A) zero
C) two
B) one
D) three
A) zero
C) two
B) one
D) three
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
An exception that can be recognized by the compiler is a(n) ____.
A) unchecked exception
C) compiler exception
B) checked exception
D) execution exception
A) unchecked exception
C) compiler exception
B) checked exception
D) execution exception
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
Which class of exceptions is NOT checked?
A) FileNotFoundException
C) RuntimeException
B) ArithmeticException
D) All exceptions are checked.
A) FileNotFoundException
C) RuntimeException
B) ArithmeticException
D) All exceptions are checked.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
How many times will the code in the try block in the accompanying figure execute?
A) Until the user specifies that he/she wants to quit the program
B) Until the user inputs a valid integer
C) If there is an exception thrown, it will execute just once because the program will terminate at that point.
D) Zero times; the program will terminate before it reaches the try block.
A) Until the user specifies that he/she wants to quit the program
B) Until the user inputs a valid integer
C) If there is an exception thrown, it will execute just once because the program will terminate at that point.
D) Zero times; the program will terminate before it reaches the try block.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
Which of the following statements is NOT true about creating your own exceptions?
A) Typically, constructors are the only methods that you include when you define your own exception class.
B) The exception class that you define extends either the class Throwable or one of its subclasses.
C) If you have created an exception class, you can define other exception classes by extending the definition of the exception class you created.
D) You must throw your own exceptions using the throw statement.
A) Typically, constructors are the only methods that you include when you define your own exception class.
B) The exception class that you define extends either the class Throwable or one of its subclasses.
C) If you have created an exception class, you can define other exception classes by extending the definition of the exception class you created.
D) You must throw your own exceptions using the throw statement.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following is NOT a typical action of the catch block?
A) Completely handling the exception
B) Partially processing of the exception
C) Rethrowing the same exception for the calling environment
D) Throwing the exception
A) Completely handling the exception
B) Partially processing of the exception
C) Rethrowing the same exception for the calling environment
D) Throwing the exception
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
To handle window events, if the class containing the application program does not extend the definition of another class, you can make that class extend the definition of the class ____.
A) MouseAdapter
C) WindowAdapter
B) Adapter
D) WindowListener
A) MouseAdapter
C) WindowAdapter
B) Adapter
D) WindowListener
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
Which of the following methods prints a list of the methods that were called before the exception was thrown?
A) getMessage()
C) printStackTrace()
B) printCalledMethods()
D) traceMethodStack()
A) getMessage()
C) printStackTrace()
B) printCalledMethods()
D) traceMethodStack()
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
What is the most likely type of exception in the code in the accompanying figure?
A) IllegalArgumentException
C) FileNotFoundException
B) InputMismatchException
D) NumberFormatException
A) IllegalArgumentException
C) FileNotFoundException
B) InputMismatchException
D) NumberFormatException
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
Which exception-handling technique is the code in the accompanying figure using?
A) Terminate the program.
C) Log the error and continue.
B) Fix the error and continue.
D) None of the above.
A) Terminate the program.
C) Log the error and continue.
B) Fix the error and continue.
D) None of the above.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
If you want to provide your own code to terminate the program when the window is closed, or if you want the program to take a different action when the window closes, then you use the interface ____.
A) WindowListener
C) CloseListener
B) ActionListener
D) MouseListener
A) WindowListener
C) CloseListener
B) ActionListener
D) MouseListener
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
The MouseListener interface defines ____ methods.
A) two
C) four
B) three
D) five
A) two
C) four
B) three
D) five
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck