Quiz 9
Wednesday
With a piece of paper, write down your name and answers to the following questions.
Which of the following class does all exception classes inherit from?
Error
RuntimeException
Throwable
JavaException
Which method can be used to retrieve the error message from an exception object?
errorMessage
errorString
getError
getMessage
Is the following statement correct, and why?
Regardless of whether an exception is thrown by code inside of a try block, all statements in the try block are always executed.
Answer: False, the normal execution of the try block would be interrupted when an exception is thrown.
Find and correct the error in the following code.
// assume inputFile references to a Scanner object. try { input = inputFile.nextInt(); } finally { inputFile.close(); } catch (inputMismatchexception e) { System.err.println("Input Mismatch"); }
Correction:
// assume inputFile references to a Scanner object. try { input = inputFile.nextInt(); } catch (inputMismatchexception e) { System.err.println("Input Mismatch"); } finally { inputFile.close(); }
Student Performance and Statistics
A histogram of student performance on percentage grades for Quiz 9 on Wednesday.
A table showing the average performance for each question in Quiz 9 on Wednesday.
Friday
With a piece of paper, write down your name and answers to the following questions.
What is an interface?
- An interface is a collection of public methods.
- An interface is a collection of interactive graphic components.
- An interface is a collection of method definitions and constants.
- An interface is a class that serves as the superclass of other classes.
What will the following program print?
import java.util.StringTokenizer; public class Program { public static void main(String[] args) { String str = "one#two@three"; StringTokenizer strTokenizer = new StringTokenizer(str, "&%@#", true); while (strTokenizer.hasMoreTokens()) System.out.println(strTokenizer.nextToken()); } }
Answer:
one
#
two
@
threeIs the following statement correct, and why?
Every class has a
toString
method and anequals
method inherited from theObject
class.Answer: Yes, this statement is correct.
With the following class hierarchy...
Is the following code correct, and why?
public class Program { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); } catch (NoSuchElementException e) { System.err.println("No Such Element"); } catch (InputMismatchException e) { System.err.println("Input Mismatch"); } } }
Correction:
public class Program { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); } catch (InputMismatchException e) { System.err.println("Input Mismatch"); } catch (NoSuchElementException e) { System.err.println("No Such Element"); } } }
Student Performance and Statistics
A histogram of student performance on percentage grades for Quiz 9 on Friday.
A table showing the average performance for each question in Quiz 9 on Friday.