Part 5. Data Structures

 
[Page 597 ( continued )]

Review Questions

Sections 17.2 “17.3

17.1 Describe the Java Throwable class, its subclasses, and the types of exceptions. What RunTimeException will the following programs throw, if any?
17.2 Show the output of the following code:
17.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); 

17.4 What is the purpose of declaring exceptions? How do you declare an exception, and where? Can you declare multiple exceptions in a method declaration?

[Page 598]
17.5 What is a checked exception, and what is an unchecked exception?
17.6 How do you throw an exception? Can you throw multiple exceptions in one throw statement?
17.7 What is the keyword throw used for? What is the keyword throws used for?
17.8 What does the JVM do when an exception occurs? How do you catch an exception?
17.9 What is the printout 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 printout if the line

   int   value =   30   ; is changed to   int   value =   50   ; 

17.10 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:

  • Will statement3 be executed?

  • If the exception is not caught, will statement4 be executed?

  • If the exception is caught in the catch block, will statement4 be executed?

  • If the exception is passed to the caller, will statement4 be executed?

17.11 What is displayed when the following program is run?
   public class   Test {   public static void   main(String[] args) {   try   {   int   [] list =   new int   [   10   ]; System.out.println(   "list[10] is "   + list[   10   ]); } 

[Page 599]
   catch   (ArithmeticException ex) { System.out.println(   "ArithmeticException"   ); }   catch   (RuntimeException ex) { System.out.println(   "RuntimeException"   ); }   catch   (Exception ex) { System.out.println(   "Exception"   ); } } } 

17.12 What is displayed when the following program is run?
   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   /     ); } } 

17.13 What is displayed when the following program is run?
   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()"   ); } 

[Page 600]
   catch   (Exception ex) { System.out.println(   "Exception in method()"   ); } } } 

17.14 If an exception is not caught in a non-GUI application, what will happen? If an exception is not caught in a GUI application, what will happen?
17.15 What does the method printStackTrace do?
17.16 Does the presence of a try-catch block impose overhead when no exception occurs?
17.17 Correct a compilation error in the following code:
   public void   m(   int   value) {   if   (value <   40   )   throw new   Exception(   "value is too small"   ); } 

Sections 17.4 “17.7

17.18 Suppose that statement2 causes an exception in the following statement:
   try   { statement1;  statement2;  statement3; }   catch   (Exception1 ex1) { }   catch   (Exception2 ex2) { }   catch   (Exception3 ex3) {   throw   ex3; }   finally   { statement4; }; statement5; 

Answer the following questions:

  • Will statement5 be executed if the exception is not caught?

  • If the exception is of type Exception3 , will statement4 be executed, and will statement5 be executed?

17.19 Suppose the setRadius method throws the RadiusException declared in §17.7. What is displayed when the following program is run?
   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"   ); } } 

[Page 601]
   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 17.10 Assertions

17.20 What is assertion for? How do you declare assertions? How do you compile code with assertions? How do you run programs with assertions?
17.21 What happens when you run the following code?
   public class   Test {   public static void   main(String[] args) {   int   i;   int   sum =     ;   for   (i =     ; i <   11   ; i++) { sum += i; }   assert   i == 10:   "i is "   + i; } } 

Comprehensive

17.22 A student wrote a method that checks whether a string is a numeric string, as follows :
   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.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net