Due to the print book page limit, we cannot inlcude all good CheckPoint questions in the physical book. The CheckPoint on this Website may contain extra questions not printed in the book. The questions in some sections may have been reordered as a result. Nevertheless, it is easy to find the CheckPoint questions in the book on this Website. Please send suggestions and errata to Dr. Liang at y.daniel.liang@gmail.com. Indicate the book, edition, and question number in your email. Thanks!

Chapter 12 Check Point Questions

Section 12.2
12.2.1
What is the advantage of using exception handling?
12.2.2
Which of the following statements will throw an exception?
System.out.println(1 / 0);
System.out.println(1.0 / 0);
12.2.3
Point out the problem in the following code. Does the code throw any exceptions?
long value = Long.MAX_VALUE + 1;
System.out.println(value);
12.2.4
What does the JVM do when an exception occurs? How do you catch an exception?
12.2.5
What is the output of the following code?
public class Test {
  public static void main(String[] args) {
    try {
      int value = 30;
      if (value < 40)
         throw new Exception("value is too small");
    }    
    catch (Exception ex) {
      System.out.println(ex.getMessage());
    }
    System.out.println("Continue after the catch block");
  }
}
What would be the output if the line
int value = 30;
were changed to
int value = 50;
12.2.6
Show the output of the following code.
(a)
public class Test {
  public static void main(String[] args) {
    for (int i = 0; i < 2; i++) {
      System.out.print(i + " ");
      try {
        System.out.println(1 / 0);
      }
      catch (Exception ex) {
      }
    }
  }
}

(b)
public class Test {
  public static void main(String[] args) {
    try {
      for (int i = 0; i < 2; i++) {
        System.out.print(i + " ");
        System.out.println(1 / 0);
      }
    }
    catch (Exception ex) {
    }
  }
}
Section 12.3
12.3.1
Describe the Java Throwable class, its subclasses, and the types of exceptions.
12.3.2
What RuntimeException will the following programs throw, if any?
(a)
public class Test {
  public static void main(String[] args) {
    System.out.println(1 / 0);
  }
}

(b)
public class Test {
  public static void main(String[] args) {
    int[] list = new int[5];
    System.out.println(list[5]);
  }
}

(c) 
public class Test {
  public static void main(String[] args) {
    String s = "abc";
    System.out.println(s.charAt(3));
  }
}

(d)
public class Test {
  public static void main(String[] args) {
    Object o = new Object();
    String d = (String)o;
  }
}

(e)
public class Test {
  public static void main(String[] args) {
    Object o = null;
    System.out.println(o.toString());
  }
}

(f)
public class Test {
  public static void main(String[] args) {
    System.out.println(1.0 / 0);
  }
}
Section 12.4
12.4.1
What is the purpose of declaring exceptions? How do you declare an exception, and where? Can you declare multiple exceptions in a method header?
12.4.2
What is a checked exception, and what is an unchecked exception?
12.4.3
How do you throw an exception? Can you throw multiple exceptions in one throw statement?
12.4.4
What is the keyword throw used for? What is the keyword throws used for?
12.4.5
Suppose that statement2 causes an exception in the following try-catch block:
try {
  statement1;
  statement2;
  statement3;
}
catch (Exception1 ex1) {
}
catch (Exception2 ex2) {
}

statement4;
Answer the following questions:
1. Will statement3 be executed?
2. If the exception is not caught, will statement4 be executed?
3. If the exception is caught in the catch block, will statement4 be executed?
12.4.6
What is displayed when running the following program?
public class Test {
  public static void main(String[] args) {
    try {
      int[] list = new int[10];
      System.out.println("list[10] is " + list[10]);      
    }   
    catch (ArithmeticException ex) {
      System.out.println("ArithmeticException");   
    }   
    catch (RuntimeException ex) {
      System.out.println("RuntimeException");   
    }   
    catch (Exception ex) {   
      System.out.println("Exception");   
    }   
  }
}
12.4.7
What is displayed when running the following program?
public class Test {  
  public static void main(String[] args) { 
    try {
      method();
      System.out.println("After the method call");   
    }   
    catch (ArithmeticException ex) { 
      System.out.println("ArithmeticException");   
    }   
    catch (RuntimeException ex) { 
      System.out.println("RuntimeException");   
    }   
    catch (Exception e) {
      System.out.println("Exception");  
    }   
  }

  static void method() throws Exception {
    System.out.println(1 / 0);
  }
}
12.4.8
What is displayed when running the following program?
public class Test { 
  public static void main(String[] args) {
    try {
      method();
      System.out.println("After the method call");   
    }   
    catch (RuntimeException ex) {
      System.out.println("RuntimeException in main");   
    }   
    catch (Exception ex) {
      System.out.println("Exception in main");   
    }   
  }

  static void method() throws Exception {
    try {
      String s ="abc";
      System.out.println(s.charAt(3));
    }
    catch (RuntimeException ex) {
      System.out.println("RuntimeException in method()");   
    }   
    catch (Exception ex) {
      System.out.println("Exception in method()");   
    }
  }
}
12.4.9
What does the method getMessage() do?
12.4.10
What does the method printStackTrace() do?
12.4.11
Does the presence of a try-catch block impose overhead when no exception occurs?
12.4.12
Correct a compile error in the following code:
public void m(int value) {
  if (value < 40)
    throw new Exception("value is too small");    
}
Section 12.5
12.5.1
Suppose you run the following code:
public static void main(String[] args) throws Exception2 {
  m(); 
  statement7;
}
     
public static void m() {
  try {
    statement1;
    statement2;
    statement3;
  }
  catch (Exception1 ex1) {
    statement4;
  }
  finally {
    statement5;
  }
  statement6;
}
answer the questions:
1. If no exception occurs, which statements are executed?
2. If statement2 throws an exception of type Exception1, which statements are executed?
3. If statement2 throws an exception of type Exception2, which statements are executed?
4. If statement2 throws an exception that is neither Exception1 nor Exception2, which statements are executed?
12.5.2
Suppose you run the following code:
public static void main(String[] args) {
  try {
    m(); 
    statement7;
  }
  catch (Exception2 ex {
    statement8;
  }
}
     
public static void m() {
  try {
    statement1;
    statement2;
    statement3;
  }
  catch (Exception1 ex1) {
    statement4;
  }
  finally {
    statement5;
  }
  statement6;
}
answer the questions:
1. If no exception occurs, which statements are executed?
2. If statement2 throws an exception of type Exception1, which statements are executed?
3. If statement2 throws an exception of type Exception2, which statements are executed?
4. If statement2 throws an exception that is neither Exception1 nor Exception 2, which statements are executed?
Section 12.6
12.6.1
The following method tests whether a string is a numeric string:
public static boolean isNumeric(String token) {
  try {
    Double.parseDouble(token);
    return true;
  }
  catch (java.lang.NumberFormatException ex) {
    return false;
  }
}
Is it correct? Rewrite it without using exceptions.
Section 12.7
12.7.1
Suppose that statement2 may cause an exception in the following code:
try {
  statement1;
  statement2;
  statement3;
}
catch (Exception1 ex1) {
}
catch (Exception2 ex2) {
  throw ex2;
}
finally {
  statement4;
}
statement5;
Answer the following questions:
1. If no exception occurs, will statement4 be executed, and will statement5 be executed?
2. If the exception is of type Exception1, will statement4 be executed, and will statement5 be executed?
3. If the exception is of type Exception2, will statement4 be executed, and will statement5 be executed?
4. If the exception is not Exception1 nor Exception2, will statement4 be executed, and will statement5 be executed?
Section 12.8
12.8.1
What would be the output if line 16 is replaced by the following line?
throw new Exception("New info from method1");
Section 12.9
12.9.1
How do you define a custom exception class?
12.9.2
Suppose the setRadius method throws the InvalidRadiusException defined in Listing 12.10. What is displayed when running the following program?
public class Test { 
  public static void main(String[] args) {
    try {
      method();
      System.out.println("After the method call");   
    }   
    catch (RuntimeException ex) {
      System.out.println("RuntimeException in main");   
    }   
    catch (Exception ex) {
      System.out.println("Exception in main");   
    }   
  }

  static void method() throws Exception {
    try {
      Circle c1 = new Circle(1);
      c1.setRadius(-1);
      System.out.println(c1.getRadius());   
    }
    catch (RuntimeException ex) {  
      System.out.println("RuntimeException in method()");
    }   
    catch (Exception ex) {
      System.out.println("Exception in method()");
      throw ex;
    }
  }
}
Section 12.10
12.10.1
What is wrong about creating a File object using the following statement?
new File("c:\book\test.dat");
12.10.2
How do you check whether a file already exists? How do you delete a file? How do you rename a file? Can you find the file size (the number of bytes) using the File class? How do you create a directory?
12.10.3
Can you use the File class for I/O? Does creating a File object create a file on the disk?
Section 12.11
12.11.1
How do you create a PrintWriter to write data to a file? What is the reason to declare throws Exception in the main method in Listing 12.13, WriteData.java? What would happen if the close() method were not invoked in Listing 12.13?
12.11.2
Show the contents of the file temp.txt after the following program is executed.
public class Test {
  public static void main(String[] args) throws java.io.IOException {
    java.io.PrintWriter output = new java.io.PrintWriter("temp.txt");
    output.printf("amount is %f %e\r\n", 32.32, 32.32);
    output.printf("amount is %5.4f %5.4e\r\n", 32.32, 32.32);
    output.printf("%6b\r\n", (1 > 2));
    output.printf("%6s\r\n", "Java");
    output.close();
  }
}
12.11.3
Rewrite the code in the preceding question using a try-with-resources syntax.
12.11.4
How do you create a Scanner to read data from a file? What is the reason to define throws Exception in the main method in Listing 12.15, ReadData.java? What would happen if the close() method were not invoked in Listing 12.15?
12.11.5
What will happen if you attempt to create a Scanner for a nonexistent file? What will happen if you attempt to create a PrintWriter for an existing file?
12.11.6
Is the line separator the same on all platforms? What is the line separator on Windows?
12.11.7
Suppose you enter 45 57.8 789, then press the Enter key. Show the contents of the variables after the following code is executed.
Scanner input = new Scanner(System.in);
int intValue = input.nextInt();
double doubleValue = input.nextDouble();
String line = input.nextLine();
12.11.8
Suppose you enter 45, press the Enter key, 57.8, press the Enter key, 789, and press the Enter key. Show the contents of the variables after the following code is executed.
Scanner input = new Scanner(System.in);
int intValue = input.nextInt();
double doubleValue = input.nextDouble();
String line = input.nextLine();
Section 12.12
12.12.1
How do you create a Scanner object for reading text from a URL?
Section 12.13
12.13.1
Before a URL is added to listOfPendingURLs, line 25 tests whether it has been traversed. Is it possible that listOfPendingURLs contains duplicate URLs? If so, give an example.
12.13.2
Simplify the code in lines 20-28 as follows: 1. Delete lines 20 and 28; 2. Add an additional condition !listOfPendingURLs.contains(s) to the if statement in line 25. Write the complete new code for the while loop in lines 17-29. Does this revision work?