Quiz 9

Wednesday

With a piece of paper, write down your name and answers to the following questions.

  1. Which of the following class does all exception classes inherit from?

    1. Error
    2. RuntimeException
    3. Throwable
    4. JavaException
  2. Which method can be used to retrieve the error message from an exception object?

    1. errorMessage
    2. errorString
    3. getError
    4. getMessage
  3. 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.

  4. 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.

100% 5 90 - 99% 2 80 - 89% 1 70 - 79% 4 60 - 69% 2 50 - 59% 1 40 - 49% 0 30 - 39% 0 20 - 29% 0 10 - 19% 0 0 - 9% 0

A table showing the average performance for each question in Quiz 9 on Wednesday.

Q1 0.9 / 1 Q2 1.0 / 1 Q3 0.6 / 1 Q4 0.8 / 1

Friday

With a piece of paper, write down your name and answers to the following questions.

  1. What is an interface?

    1. An interface is a collection of public methods.
    2. An interface is a collection of interactive graphic components.
    3. An interface is a collection of method definitions and constants.
    4. An interface is a class that serves as the superclass of other classes.
  2. 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
        @
        three

  3. Is the following statement correct, and why?

    Every class has a toString method and an equals method inherited from the Object class.

    Answer: Yes, this statement is correct.

  4. With the following class hierarchy...

    Exception Inheritance Tree

    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.

100% 1 90 - 99% 0 80 - 89% 0 70 - 79% 2 60 - 69% 0 50 - 59% 1 40 - 49% 0 30 - 39% 2 20 - 29% 2 10 - 19% 0 0 - 9% 0

A table showing the average performance for each question in Quiz 9 on Friday.

Q1 0.4 / 1 Q2 0.8 / 1 Q3 0.6 / 1 Q4 0.2 / 1