收藏 分销(赏)

java习题.doc

上传人:仙人****88 文档编号:11398176 上传时间:2025-07-21 格式:DOC 页数:7 大小:56.50KB 下载积分:10 金币
下载 相关 举报
java习题.doc_第1页
第1页 / 共7页
java习题.doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
1、public class Test{ public static String output; public static void foo(int i){ try { if(i==1){ throw new Exception(); } output ="1"; } catch(Exception e){ output ="2"; return; } finally{ output ="3"; } output ="4"; } public static void main(String args[]){ foo(0); foo(1); } } 请写出程序运行过程中,output值的变化。 2、class Mammal{ Mammal(){ System.out.println("Four"); } public void ears() { System.out.println("Two"); } } class Dog extends Mammal{ Dog() { super.ears(); System.out.println("Three"); } } public class Scottie extends Dog{ public static void main (String args[ ]){ System.out.println("One"); Scottie d = new Scottie(); } } 3、import java.io.*; public class a{ public static void main(String args[]) throws IOException{ int i; float f,r; BufferedReader din = new BufferedReader( new InputStreamReader (System.in)); System.out.print("input i: "); i=Integer.parseInt(din.readLine()); System.out.print("input f: "); f=Float.parseFloat(din.readLine()); r=i+f; System.out.println(i+"+"+f+"="+r); } } 4、这是一个时钟Applet,它显示当前时间并逐秒进行更新。请根据程序需要填空。 import java.awt.*; import java.applet.*; import java.util.*; public class Clock extends Applet implements Runnable{ Thread clockThread; public void start(){ if( ){ clockThread=new Thread(this,"Clock"); clockThread. } } public void run(){ while(clockThread !=null){ try{ clockThread.sleep(1000); }catch(InterruptedException e){} } } public void paint(Graphics g){ Date now=new Date(); g.drawString(now.getHours()+";"+now.getMinutes()+";"+now.getSeconds(),5,10); } public void stop(){ clockThread.stop(); clockThread=null; } } 【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) 编 译 不 能 通 过。        【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        【3】 下 面 程 序 的 输 出 是 什 么 ? 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) 无 输 出        【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        【5】Java 运 行 时 例 外 是 在 运 行Java 程 序 时 由Java 运 行 时 系 统 负 责 抛 出 的 一 系 列 例 外。 本 章 例 程 中 所 提 到 的 许 多 例 外 就 是Java 运 行 时 例 外( 详 见 例8.2 等)。 请 详 细 阅 读 例 程, 选 择 对 于 如 下 的 程 序, 系 统 将 抛 出 哪 个 运 行 时 例 外。 class Quiz4{ int a[]=new int[10]; a[10]=0; } (A)ArithmeticException (B)ArrayIndexOutOfBoundsException (C)NegativeArraySizeException (D)IllegalArgumentException        【6】 为 了 编 程 需 要, 现 需 自 己 编 写 一 个 例 外 类。 一 般 说 来, 下 面 声 明 哪 个 最 为 合 适 ? (A)class myClass extentds Exception{... (B)class myException extends Error{... (C)class myException extends RuntimeException{... (D)class myException extends Exception{... 1. String s = new String("xyz");创建了几个String Object? 2. 是否可以继承String类? 3. Math.round(11.5)等於多少? Math.round(-11.5)等於多少? 4. 数组有没有length()这个方法? String有没有length()这个方法? 5. Overload和Override的区别。Overloaded的方法是否可以改变返回值的类型? 6. abstract class和interface有什么区别? 7. abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized? 8. 接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继承实体类(concrete class)? 9. 启动一个线程是用run()还是start()? 10. 构造器Constructor是否可被override? 11. try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服