浏览 3004 次
锁定老帖子 主题:SCJP认证试题(八)
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-11-22
/** * * @author yaoyuan */ 11 public static void test(String str){ 12 if(str == null | str.length() ==){ 13 System.out.println("String is empty"); 14 }else{ 15 System.out.println("String is not empty"); 16 } 17 } And the invocation: 31 test(null); what is the result? A An exception is thrown at runtime B "String is empty" is printed to output C Compilation fails because of an error in line 12 D "String is not empty" is printed to output Answer : A /** * * @author yaoyuan */ 15 public class Yippee{ 16 public static void main(String[] args){ 17 for(int x=1;x<args.length;x++){ 18 System.out.println(args[x] + " "); 19 } 20 } 21 } and two separate command line invocations: java Yippee java Yippee 1 2 3 4 What is the result? A No output is produced 1 2 3 B No output is produced 2 3 4 C No output is produced 1 2 3 4 D An exception is thrown runtime 1 2 3 E An exception is thrown runtime 2 3 4 F An exception is thrown runtime 1 2 3 4 Answer : B /** * * @author yaoyuan */ 13 public class Pass{ 14 public static void main(String[] args){ 15 int x = 5; 16 Pass p = new Pass(); 17 p.doStuff(x); 18 System.out.println("main x = " + x); 19 } 20 21 void doStuff(int x){ 22 System.out.print("doStuff x=" + x++); 23 } 24 } What is the result? A Compilation fails B An exception is thrown at runtime C doStuff x=6 main x =6 D doStuff x=5 main x =5 E doStuff x=5 main x =6 F doStuff x=6 main x =5 Answer : D /** * * @author yaoyuan */ 11 class A{ 12 public void process(){System.out.print("A");}} 13 class B extends A{ 14 public void process() throw IOException{ 15 super.process(); 16 System.out.print("B"); 17 throw new IOException(); 18 }} 19 public static void main(String[] args){ 20 try{new B().process();} 21 catch(IOException e){System.out.println("Exception");}} What is the result? A Exception B A,B,Exception C Compilation fails because of an error in line 20 D Compilation fails because of an error in line 14 E A NullPointerException is thrown at runtime Answer : D /** * * @author yaoyuan */ public class Test{ public enum Dogs{collie,harrier,shepherd}; public static void main(String[] args){ Dogs myDog = Dogs.shepherd; switch(myDog){ case collie: System.out.print("collie"); case default: System.out.print("retriever"); case harrier: System.out.print("harrier"); } } } What is the result? A harrier B shepherd C retriever D Compilation fails E retriever harrier F An exception is thrown at runtime Answer: D /** * * @author yaoyuan */ public static Collection get(){ Collection sorted = new LinkedList(); sorted.add("B"); sorted.add("C"); sorted.add("A"); return sorted; } public static void main(String[] args){ for(Object obj:get()){ System.out.print(obj + ", "); } } What is the result? A A,B,C B B,C,A C Compilation fails D The code runs with no output E An exception is thrown at runtime Answer: B /** * * @author yaoyuan */ static void test() throws Error{ if(true) throw new AssertionError(); System.out.print("test"); } public static void main(String[] args){ try{test();} catch(Exception ex){System.out.print("exception");} System.out.println("end"); } What is the result? A end B Compilation fails C exception end D exception test end E A Throwable is thrown by main F A Exception is thrown by main Answer : E /** * * @author yaoyuan */ Float pi = new Float(3.14f); if(pi > 3){ System.out.print("pi is bigger than 3."); } else{ System.out.print("pi is not bigger than 3"); } finally{ System.out.print("Have a nice day."); } What is the result? A Compilation fails B pi is bigger than 3 C An exception occurs at runtime D pi is bigger than 3.Have a nice day. E pi is not bigger than 3.Have a nice day. Answer:A /** * * @author yaoyuan */ 10 interface Foo{} 11 class Alpha implements Alpha{} 12 class Beta extends Beta{} 13 class Delta extends Beta{ 14 public static void main(String[] args){ 15 Beta x= new Beta(); 16 //insert code here 17 } 18 } Which code, inserted at line 16 will cause a java.lang.ClassCaseException? A Alpha a = x; B Foo f = (Delta)x; C Foo f = (Alpha)x; D Beta b = (Beta)(Alpha)x; Answer : B /** * * @author yaoyuan */ Given a method that must ensure that its parameter is not null: 11 public void someMethod(Object value) 12 //check for null value ............. 20 System.out.println(value.getClass()); 21 } What inserted at line 12 is the appropriate way to handle a null value? A assert value == null; B assert value != null,"value is null"; C if(value == null){ throw new AssertionException("value is null"); } D if(value == null){ throw new IllegalArgumentException("value is null"); } Answer : D 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-11-22
不错,可是貌似都做过曾经
|
|
返回顶楼 | |
发表时间:2008-11-23
牛头人 写道 不错,可是貌似都做过曾经 前几天,有几个哥们做,我就看了一下,挺不错的,有时间就整理一下........ |
|
返回顶楼 | |
发表时间:2008-11-23
我郁闷,不实际运行的话,都会做错。
|
|
返回顶楼 | |
发表时间:2008-11-24
看完,感受:
革命尚未成功 同志还需努力 |
|
返回顶楼 | |
发表时间:2008-12-18
麻烦帅哥你放题目时候还是要态度认真一点,你看你第一道题,的第一个判断子式是if(str == null | str.length() ==),虽然应该能想到是str.length==0,但是你打的时候也注意点撒,还有,null后面是只有一个“|”还是你少输入一个?
|
|
返回顶楼 | |