|
Java 编 程 练 习 题 库 (三)第 一 题 【1】 编 译 运 行 如 下 程 序 的 结 果 是 什 么 ? class InvalidIndexException extends Exception{ private int i; InvalidIndexException(int a){ i=a; } public String toString(){ return i+" is out of boundary--0 < i < 8"; } } public class Weekdays{ public static void main(String args[]){ try{ for (int i=1; i < 9; i++) System.out.println(i+"---"+giveName(i)); }catch(InvalidIndexException e){ System.out.println(e.toString()); }finally{ System.out.println("These days makes up a week.");} } public static String giveName(int d) { String name; switch(d){ case 1: name="Monday"; break; case 2: name="Tuesday"; break; case 3: name="Wednesday"; break; case 4: name="Thursday"; break; case 5: name="Friday"; break; case 6: name="Saturday"; break; case 7: name="Sunday"; break; default: throw new InvalidIndexException(d); } return name; } } (A) 1---Monday 2---Tuesday 3---Wednesday 4---Thursday 5---Friday 6---Saturday 7---Sunday These days makes up a week. (B) 1---Monday 2---Tuesday 3---Wednesday 4---Thursday 5---Friday 6---Saturday 7---Sunday 8 is out of boundary--0 < i < 8 These days makes up a week. (C) 1---Monday 2---Tuesday 3---Wednesday 4---Thursday 5---Friday 6---Saturday 7---Sunday (D) 编 译 不 能 通 过。答 案:(B) 【2】 有 如 下 一 段Java 程 序: import java.io.*; public class Quiz1{ public static void main(String arg[]){ int i; System.out.print("Go "); try{ System.out.print("in "); i=System.in.read(); if (i=='0') {throw new MyException();} System.out.print("this "); } catch(IOException e){} catch(MyException e){ System.out.print("that "); } System.out.print("way.\n"); } } class MyException extends Exception{}运 行 该 程 序 后 输 入 字 符'0', 请 问 运 行 结 果 为 何 ? (A)Go in this way (B)Go in that this way (C)Go in that (D)Go in that way答 案:(D) public class Quiz2{ public static void main(String args[]){ try {throw new MyException(); }catch(Exception e){ System.out.println("It's caught!"); }finally{ System.out.println("It's finally caught!"); } } } class MyException extends Exception{} (A)It's finally caught! (B)It's caught! (C)It's caught! It's finally caught! (D) 无 输 出答 案:(C) 【4】 下 面 的 程 序 是 一 个 嵌 套 例 外 处 理 的 例 子, 请 选 择 其 运 行 结 果: public class Quiz3{ public static void main(String args[]){ try{ try{ int i; int j=0; i=1/j; }catch(Exception e){ System.out.print("Caught "); throw e; }finally{ System.out.print("Inside "); } }catch(Exception e){ System.out.print("Caught "); }finally{ System.out.print("Outside\n"); } } } (A)Caught Outside (B)Caught Inside (C)Caught Caught Outside (D)Caught Inside Caught Outside答 案:(D) class Quiz4{ int a[]=new int[10]; a[10]=0; } (A)ArithmeticException (B)ArrayIndexOutOfBoundsException (C)NegativeArraySizeException (D)IllegalArgumentException答 案:(B) 【6】 为 了 编 程 需 要, 现 需 自 己 编 写 一 个 例 外 类。 一 般 说 来, 下 面 声 明 哪 个 最 为 合 适 ? (A)class myClass extentds Exception{... (B)class myException extends Error{... (C)class myException extends RuntimeException{... (D)class myException extends Exception{...答 案:(D)
|