1、计算机 Java认证-5 及答案解析(总分:100.00,做题时间:90 分钟)一、不定项选择题(总题数:44,分数:100.00)1.Given: 3. class Department 4. Department getDeptName() return new Department(); 5. 6. class Accounting extends Department 7. Accounting getDeptName() return new Accounting(); 8. / insert code here 13. And the following four code fra
2、gments: . String getDeptName(int x) return “mktg“; . void getDeptName(Department d) ; . void getDeptName(long x) throws NullPointerException throw new NullPointerException(); . Department getDeptName() throws NullPointerException throw new NullPointerException(); return new Department(); Which are t
3、rue? (Choose all that apply.)(分数:1.50)A.If fragment is inserted at line 8, the code compiles.B.If fragment is inserted at line 8, the code compiles.C.If fragment is inserted at line 8, the code compiles.D.If fragment is inserted at line 8, the code compiles.E.If none of the fragments are inserted at
4、 line 8, the code compiles.2.Given: 2. import java.util.*; 3. interface Canine 4. class Dog implements Canine 5. public class Collie extends Dog 6. public static void main(String args) 7. ListDog d = new ArrayListDog(); 8. ListCollie c = new ArrayListCollie(); 9. d. add (new Collie(); 10. c. add (ne
5、w Collie(); 11. do1(d); do1(c); 12. do2(d); do2(c); 13. 14. static void dol (List? extends Dog d2) 15. d2.add(new Collie(); 16. System.out.print(d2.size(); 17. 18. static void do2(List? extends Canine c2) 19. Which are true? (Choose all that apply.)(分数:1.50)A.Compilation succeeds.B.Compilation fails
6、 due to an error on line 9.C.Compilation fails due to an error on line 14.D.Compilation fails due to an error on line 15.E.Compilation fails due to an error on line 16.F.Compilation fails due to an error on line 18.G.Compilation fails due to errors on lines 11 and 12.3.Given: 42. void go() 43. int c
7、ows = 0; 44. int twisters = 1,2,3; 45. for(int i = 0; i 4; i+) 46. switch(twistersi) 47. case 2: cows+; 48. case 1: cows += 10; 49. case 0: go(); 50. 51. System.out.println(cows); 52. What is the result?(分数:1.50)A.11B.21C.22D.Compilation fails.E.A StackOverflowError is thrown at runtime.F.An Arrayln
8、dexOutOfBoundsException is thrown at runtime.4.Given: 2. class Robot 3. interface Animal 4. class Feline implements Animal 5. public class BarnCat extends Feline 6. public static void main(String args) 7. Animal af = new Feline(); 8. Feline ff = new Feline(); 9. BarnCat b = new BarnCat(); 10. Robot
9、r = new Robot(); 11. if(af instanceof Animal) System.out.print(“1 “); 12. if(af instanceof BarnCat) System.out.print(“2 “); 13. if(b instanceof Animal) System.out.print(“3 “); 14. if(ff instanceof BarnCat) System.out.print (“4 “); 15. if(r instanceof Animal) System.out .print (“5 “); 16. 17. What is
10、 the result?(分数:1.50)A.1B.1 3C.1 2 3D.1 3 4E.1 2 3 4F.Compilation fails.G.An exception is thrown at runtime.5.Given: 2. public class Sunny extends Weather 3. public static void main(String args) 4. try 5. new Sunny() .do1(); 6. new Sunny() .do2(); 7. new Sunny() .do3(); 8. 9. catch(Throwable t) Syst
11、em.out.print(“exc “); 10. 11. class Weather 12. void do1() System.out.print(“do1 “); 13. private void do2() System.out.print(“do2 “); 14. protected void do3() System.out.print(“do3 “); 15. What is the result?(分数:1.50)A.do1 excB.do1 do2 excC.do1 do2 do3D.Compilation fails.E.An exception is thrown at
12、runtime.6.Given that FileNotFoundException extends IOException and given: 2. import java.io.*; 3. public class Changeup 4. public static void main(String args) throws IOException 5. new Changeup() .go(); 6. new Changeup() .go2(); 7. new Changeup() .go3(); 8. 9. void go() throw new IllegalArgumentExc
13、eption(); 10. 11. void go2() throws FileNotFoundException 12. 13. void go3() 14. try throw new Exception(); 15. catch (Throwable th) throw new NullPointerException(); 16. What is the result? (Choose all that apply.)(分数:1.50)A.An IOException is thrown at runtime.B.A NullPointerException is thrown at
14、runtime.C.An IllegalArgumentException is thrown at runtime.D.Compilation fails due to an error at line 4.E.Compilation fails due to an error at line 9.F.Compilation fails due to an error at line 11.G.Compilation fails due to an error at line 15.7.Which are true? (Choose all that apply.)(分数:1.50)A.If
15、 class A is-a class B, then class A cannot be considered well encapsulated.B.If class A has-a class B, then class A cannot be considered well encapsulated.C.If class A is-a class B, then the two classes are said to be cohesive.D.If class A has-a class B, then the two classes are said to be cohesive.
16、E.If class A is-a class B, it“s possible for them to still be loosely coupled.F.If class A has-a class B, it“s possible for them to still be loosely coupled.8.Given: 2. import java.util.*; 3. public class Volleyball 4. public static void main(String args) 5. TreeSetString s = new TreeSetString(); 6.
17、 s.add(“a“); s.add(“f“); s.add(“b“); 7. System.out.print(s + “ “); 8. Collections.reverse(s); 9. System.out.println(s); 10. What is the result?(分数:1.50)A.Compilation fails.B.a, b, f a, b, fC.a, b, f f, b, aD.a, f, b b, f, aE.a, b, f, followed by an exception.F.a, f, b, followed by an exception.9.Giv
18、en: 2. public class Boggy 3. final static int mine = 7; 4. final static Integer i = 57; 5. public static void main(String args) 6. int x : go(mine); 7. System.out.print(mine + “ “ + x + “ “); 8. x += mine; 9. Integer i2 = i; 10. i2 = go(i); 11. System.out.println(x + “ “ + i2); 12. i2 = new Integer(
19、60); 13. 14. static int go(int x) return +x; 15. What is the result?(分数:1.50)A.7 7 14 57B.7 8 14 57C.7 8 15 57D.7 8 15 58E.7 8 16 58F.Compilation fails.G.An exception is thrown at runtime.10.Given: 3. import java.io.*; 4. public class Kesey 5. public static void main(String args) throws Exception 6.
20、 File file = new File(“bigData.txt“); 7. FileWriter w = new FileWriter(file); 8. w.println(“lots o“ data“); 9. w.flush(); 10. w.close(); 11. What is the result? (Choose all that apply.)(分数:1.50)A.An empty file named “bigData.txt“ is created.B.Compilation fails due only to an error on line 5.C.Compil
21、ation fails due only to an error on line 6.D.Compilation fails due only to an error on line 7.E.Compilation fails due only to an error on line 8.F.Compilation fails due to errors on multiple lines.G.A file named “bigData.txt“ is created, containing one line of data.11.Given: 3. class Wanderer implem
22、ents Runnable 4. public void run() 5. for(int i = 0; i 2; i+) 6. System.out.print(Thread.currentThread() .getName() + “ “); 7. 8. public class Wander 9. public static void main(String args) 10. Wanderer w = new Wanderer(); 11. Thread t1 = new Thread(); 12. Thread t2 = new Thread(w); 13. Thread t3 =
23、new Thread(w, “fred“); 14. t1.start(); t2.start(); t3.start(); 15. Which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.No output is produced.C.The output could be Thread- 1 fred fred Thread- 1D.The output could be Thread-1 Thread-1 Thread-2 Thread-2E.The output could be Thread-1 f
24、red Thread-1 Thread-2 Thread-2 fredF.The output could be Thread-1 Thread-1 Thread-2 Thread-3 fred fred12.Given: 2. import java.util.*; 3. public class MyFriends 4. String name; 5. MyFriends(String s) name = s; 6. public static void main(String args) 7. SetMyFriends ms = new HashSetMyFriends(); 8. ms
25、.add(new MyFriends(“Bob“); 9. System.out.print(ms + “ “); 10. ms.add(new MyFriends(“Bob“); 11. System.out.print(ms + “ “); 12. ms.add(new MyFriends(“Eden“); 13. System.out.print(ms + “ “); 14. 15. public String toString() return name; 16. What is the most likely result?(分数:2.50)A.Compilation fails.B
26、.Bob Bob Eden, BobC.Bob Bob Eden, Bob, BobD.Bob, followed by an exception.E.Bob Bob, Bob Eden, Bob, Bob13.Given the proper imports, and given: 17. public void go() 18. NumberFormat nf, nf2; 19. Number n; 20. Locale la = NumberFormat.getAvailableLocales(); 21. for(int x=0; x 10; x+) 22. nf = NumberFo
27、rmat.getCurrencyInstance(lax); 23. System.out.println(nf.format(123.456f); 24. 25. nf2 = NumberFormat.getInstance(); 26. n = nf2.parse(“123.456f“); 27. System.out.println(n); 28. Given that line 20 is legal, which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.An exception is throw
28、n at runtime.C.The output could contain “123.46“D.The output could contain “123.456“E.The output could contain “$123.46“14.Given: 59. Integer i1 = 2001; / set 1 60. Integer i2 = 2001; 61. System.out.println(i1 = i2) + “ “ + i1.equals(i2); / output 1 62. Integer i3 = 21; / set 2 63. Integer i4 = new
29、Integer(21); 64. System.out.println(i3 = i4) + “ “ + i3.equals(i4); / output 2 65. Integer i5 = 21; / set 3 66. Integer i6 = 21; 67. System.out.println(i5 = i6) + “ “ + i5.equals(i6); / output 3 What is the result? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.An exception is thrown at runt
30、ime.C.All three sets of output will be the same.D.The last two sets of output will be the same.E.The first two sets of output will be the same.F.The first and last sets of output will be the same.15.Given: 2. public class Skip 3. public static void main(String args) throws Exception 4. Thread t1 = n
31、ew Thread(new Jump(); 5. Thread t2 = new Thread(new Jump(); 6. t1.start(); t2.start(); 7. t1.join(500); 8. new Jump() .run(); 9. 10. class Jump implements Runnable 11. public void run() 12. for(int i = 0; i 5; i+) 13. try Thread.sleep(200); 14. catch (Exception e) System.out.print(“e “); 15. System.
32、out.print(Thread.currentThread() .getId() + “-“ + i+ “ “); 16. What is the result?(分数:2.50)A.Compilation fails.B.The main thread will run mostly before t1 runs.C.The main thread will run after t1, but together with t2.D.The main thread will run after t2, but together with t1.E.The main thread will r
33、un after both t1 and t2 are mostly done.F.The main thread“s execution will overlap with t1 and t2“s execution.16.Given: 1. import java.util.*; 2. public class Piles 3. public static void main (String args) 4. TreeMapString, String tm = new TreeMapString, String(); 5. TreeSetString ts = new TreeSetSt
34、ring(); 6. String k = “1“, “b“, “4“, “3“; 7. String v = “a“, “d“, “3“, “b“; 8. for(int i=0; i4; i+) 9. tm.put (ki, vi); 10. ts.add(vi); 11. 12. System.out.print(tm.values() + “ “); 13. Iterator it2 = ts.iterator(); 14. while(it2.hasNext() System.out.print(it2.next() + “-“); 15. Which of the followin
35、g could be a part of the output? (Choose two.) A. a, b, 3, d B. d, a, b, 3 C. 3, a, b, d D. a, b, d, 3 E. 1, 3, 4,b F. b, 1,3,4 G. 3-a-b-d- H. a-b-d-3- I. a-d-3-b- (分数:2.50)A.B.C.D.E.F.G.H.I.17.Given this code in a method: 5. String s = “dogs. with words.“; 6. / insert code here 7. for(String o: out
36、put) 8. System.out.print(o + “ “); Which of the following, inserted independently at line 6, will produce output that contains the String “dogs“? (Choose all that apply.)(分数:2.50)A.String output = s.split(“s“);B.String output = s.split(“d“);C.String output = s.split(“/d“);D.String output = s.split(“
37、/s“);E.String output = s.split(“/w“);F.String output = s.split(“/.“);18.Given the design implied by this partially implemented class: 2. public class RobotDog 3. int size; 4. void bark() /* do barking */ 5. int getSize() return size; 6. size = 16; 7. int getNetworkPrinterID() 8. /* do lookup */ 9. r
38、eturn 37; 10. 11. voidprintRobotDogStuff(intprinterID) /*print RobotDog stuff*/ 12. Which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.To improve cohesion, the size variable should be declared private.C.To improve cohesion, the initialization block should be placed inside a const
39、ructor.D.To improve cohesion, printRobotDogStuff() should be moved to a different class.E.To improve cohesion, getNetworkPrinterlD0 should be moved to a different class.19.Given: 2. import java.util.*; 3. public class Foggy extends Murky 4. public static void main(String args) 5. final ListString s
40、= new ArrayListString(); 6. s.add(“a“); s.add(“f“); s.add(“a“); 7. new Foggy() .mutate(s); 8. System.out.println(s); 9. 10. ListString mutate (ListString s) 11. ListString ms = s; 12. ms.add (“c“); 13. return s; 14. 15. 16. class Murky 17. final void mutate(Set s) 18. What is the most likely result?
41、(分数:2.50)A.a,fB.a, f, aC.a, f, cD.a, f, a, cE.Compilation fails.F.An exception is thrown at runtime.20.Given: 1. import java.util.*; 2. enum Heroes GANDALF, HANS, ENDER 3. public class MyStuff 4. public static void main(String args) 5. ListString stuff = new ArrayListString(); 6. stuff.add(“Bob“); s
42、tuff.add(“Fred“); 7. new MyStuff().go(); 8. 9. Heroes myH = Heroes.ENDER; 10. void go() 11. for(Heroes h: Heroes.values() 12. if(h = myH) System.out.println(myH); 13. Which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.main() has-a ListC.MyStuffhas-a ListD.MyStuff has-a HeroesE.Th
43、e output is “ENDER“F.The output is “Heroes.ENDER“G.An exception is thrown at runtime.21.Given: 2. public class Pregnant extends Thread 3. int x = 0; 4. public static void main(String args) 5. Runnable r1 = new Pregnant(); 6. new Thread(r1) .start(); 7. new Thread(r1) .start(); 8. 9. public void run(
44、) 10. for(int j = 0; j 3; j+) 11. x = x + 1; 12. x = x + 10; 13. System.out.println(x + “ “); 14. x = x + 100; 15. If the code compiles, which value(s) could appear in the output? (Choose all that apply.)(分数:2.50)A.12B.22C.122D.233E.244F.566G.Compilation fails.22.Given: 2. public class Toolbox 3. st
45、atic Toolbox st; 4. public static void main(String args) 5. new Toolbox().go(); 6. / what“s eligible? 7. 8. void go() 9. MyInner in = new MyInner(); 10. Integer i3 = in.doInner(); 11. Toolbox t = new Toolbox(); 12. st = t; 13. System.out.println(i3); 14. 15. class MyInner 16. public Integer doInner(
46、) return new Integer(34); 17. 18. When the code reaches line 6, which are eligible for garbage collection? (Choose all that apply.)(分数:2.50)A.stB.inCi3D.The object created on line 5.E.The object created on line 9.F.The object created on line 10.G.The object created on line 11.23.Given: 2. class Ball
47、 3. static String s = “; 4. void doStuff() s += “bounce “; 5. 6. class Basketball extends Ball 7. void doStuff() s += “swish “; 8. 9. public class Golfball extends Ball 10. public static void main(String args) 11. Ball b = new Golfball(); 12. Basketball bb = (Basketball)b; 13. b.doStuff(); 14. bb.do
48、Stuff(); 15. System.out.println(s); 16. 17. void doStuff() s += “fore “; 18. What is the result?(分数:2.50)A.fore foreB.fore swishC.bounce swishD.bounce bounceE.Compilation fails.F.An exception is thrown at runtime.24.Given the following three files: 2. package apollo; 3. import apollo.modules.Lunar; 4. public class