1、计算机 Java认证-2 及答案解析(总分:100.00,做题时间:90 分钟)一、不定项选择题(总题数:42,分数:100.00)1.Given: 1. public class Endless 2. public static void main(String args) 3. int i = 0; 4. short s = 0; 5. for(int j = 0, k = 0; j 3; j+); 6. for(int j = 0; j 3; counter(j); 7. for(int j = 0, int k = 0; j 3; j+); 8. for(; i 5; counter(
2、5), i+); 9. for(i = 0; i 3; i+, System.out.print(“howdy “); 10. 11. static int counter(int y) return y + i; 12. What is the result? (Choose all that apply.)(分数:2.50)A.howdy howdy howdyB.The code runs in an endless loop.C.Compilation fails due to an error on line 5.D.Compilation fails due to an error
3、 on line 6.E.Compilation fails due to an error on line 7.F.Compilation fails due to an error on line 8.G.Compilation fails due to an error on line 9.2.Given: 2. class Big 3. void doStuff(int x) 4. 5. class Heavy extends Big 6. / void doStuff(byte b) 7. / protected void doStuff(int x) throws Exceptio
4、n 8. 9. public class Weighty extends Heavy 10. / void doStuff(int x) 11. / String doStuff(int x) return “hi“; 12. / public int doStuff(int x) return 7; 13. / private int doStuff(char c) throws Error return 1; 14. Which method(s), ifuncommented independently, compile? (Choose all that apply.)(分数:2.50
5、)A.Line 6B.Line 7C.Line 10D.Line 11E.Line 12F.Line 133.Which are true? (Choose all that apply.)(分数:2.50)A.A given TreeSet“s ordering cannot be changed once it“s created.B.The java.util.Properties class is conceptually more like a List than like a Map.C.It“s a reasonable design choice to use a Linked
6、List when you want to design queue-like functionality.D.Of the main types of collections flavors (Lists, Sets, Maps), Queues are conceptually most like Sets.E.It“s programmatically easier to perform a non-destructive traversal of a PriorityQueue than a LinkedList.F.Classes that implement the Set int
7、erface are usually well suited for applications that require access to a collection based on an index.4.Given the following directory structure: test -| |- Finder.class |- testdir -| |- subdir |- subdir2 |- testfile.txt If test, testdir, subdir, and subdir2 are all directories, and Finder.class and
8、testfile.txt are files, and given: import java.io.*; public class Finder public static void main(String args) throws IOException String files = new String100; File dir = new File(args0); files = dir.list(); System. out.println (files.length); And, if the code compiles, the invocation: java Finder te
9、stdir What is the result?(分数:2.50)A.1B.2C.3D.4E.5F.100G.Compilation fails.H.An exception is thrown at runtime.5.Given: 1. public class Grids 2. public static void main(String args) 3. int ia2; 4. int ia1 = 1,2,3; 5. Object o = ia1; 6. ia2 = new int3 3 ; 7. ia20 = (int)o; 8. ia20 0 = (int)o; 9. What
10、is the result? (Choose all that apply.)(分数:2.50)A.Compilation fails due to an error on line 4.B.Compilation fails due to an error on line 5.C.Compilation fails due to an error on line 6.D.Compilation fails due to an error on line 7.E.Compilation fails due to an error on line 8.F.Compilation succeeds
11、 and the code runs without exception.G.Compilation succeeds and an exception is thrown at runtime.6.Given: 3. public class OffRamp 4. public static void main(String args) 5. int exits = 0,0,0,0,0,0; 6. int x1 = 0; 7. 8. for(int x = 0; x 4; x+) exits0 = x; 9. for(int x = 0; x 4; +x) exits1 = x; 10. 1
12、1. x1 = 0; while(x1+ 3) exits2 = x1; 12. x1 = 0; while(+x1 3) exits3 = x1; 13. 14. x1 = 0; do exits4 = x1; while(x1+ 7); 15. x1 = 0; do exits5 = x1; while(+x1 7); 16. 17. for(int x: exits) 18. System.out.print(x + “ “); 19. What is the result?(分数:2.50)A.3 3 2 2 6 6B.3 3 3 2 7 6C.3 3 3 2 7 7D.4 3 3 2
13、 7 6E.4 3 3 2 7 7F.Compilation fails.7.Given: 2. import java.util.*; 3. public class HR 4. public static void main(String args) 5. ListInteger i = new VectorInteger(); 6. i.add(3); i.add(2); i.add(5); 7. int ref = 1; 8. doStuff (ref); 9. System.out.println(i.get(ref); 10. 11. static int doStuff(int
14、x) 12. return +x; 13. What is the result?(分数:2.50)A.2B.3C.5D.Compilation fails.E.An exception is thrown at runtime.8.Given: 2. import java.util.*; 3. public class Vinegar 4. public static void main(String args) 5. SetInteger mySet = new HashSetInteger(); 6. do1 (mySet, “0“); do1 (mySet, “a“); 7. do2
15、 (mySet, “0“); do2 (mySet, “a“); 8. 9. public static void do1 (Set s, String st) 10. s.add (st); 11. s.add(Integer.parseInt (st); 12. 13. public static void do2(SetInteger s, String st) 14. s.add (st); 15. s.add(Integer.parseInt (st); 16. Which are true? (Choose all that apply.)(分数:2.50)A.Compilatio
16、n succeeds.B.Compilation fails due to an error on line 6.C.Compilation fails due to an error on line 13.D.Compilation fails due to an error on line 14.E.Compilation fails due to an error on line 15.F.If only the line(s) of code that don“t compile are removed, the code will run without exception.G.If
17、 only the line(s) of code that don“t compile are removed, the code will throw an exception.9.Given: 3. class Employee 4. private String name; 5. void setName(String n) name = n; 6. String getName() return name; 7. 8. interface Mungeable 9. void doMunging(); 10. 11. public class MyApp implements Mung
18、eable 12. public void doMunging() ; 13. public static void main(String args) 14. Employee e = new Employee(); 15. e.setName (“bob“); 16. System.out.print (e.getName(); 17. Which are true? (Choose all that apply.)(分数:2.50)A.MyApp is-a Employee.B.MyApp is-a Mungeable.C.MyApp has-a Employee.D.MyApp has
19、-a Mungeable.E.The code is loosely coupled.F.The Employee class is well encapsulated.10.Given that FileNotFoundException extends IOException, and given: 2. import java.io.*; 3. public class MacPro extends Laptop 4. public static void main(String args) 5. new MacPro() .crunch(); 6. 7. / insert code h
20、ere 8. 9. class Laptop 10. void crunch() throws IOException 11. Which method(s), inserted independently at line 7, compile? (Choose all that apply.)(分数:2.50)A.void crunch() B.void crunch() throws Exception C.void crunch(int x) throws Exception D.void crunch() throws RuntimeException E.void crunch()
21、throws FileNotFoundException 11.Given: 2. class Horse 3. String hands = “15“; 4. 5. class GaitedPony extends Horse 6. static String hands = “14“; 7. public static void main(String args) 8. String hands = “13.2“; 9. String result = new GaitedPony().getSize(hands); 10. System.out.println(“ “ + result)
22、; 11. 12. String getSize(String s) 13. System.out.print(“hands: “ + s); 14. return hands; 15. What is the result?(分数:2.50)A.14B.15C.hands: 13.2 14D.hands: 13.2 15E.Compilation fails.F.An exception is thrown at runtime.12.Given: 2. public class Humping 3. public static void main(String args) 4. Strin
23、g r = “-“; 5. char c = “a“, “b“, “c“, “z“; 6. for(char c1: c) 7. switch (c1) 8. case “a“: r += “a“; 9. case “b“: r += “b“; break; 10. default: r += “X“; 11. case “z“: r+= “z“; 12. 13. System. out.println(r); 14. What is the result?(分数:2.50)A.-abXzB.-abbXzC.-abbXzzD.-abbXzXzE.Compilation fails due to
24、 a single error.F.Compilation fails due to multiple errors.13.Given: 1. import java.util.*; 2. public class Garage 3. public static void main(String args) 4. MapString, String hm = new HashMapString, String(); 5. String k = null, “2“, “3“, null, “5“; 6. String v = “a“, “b“, “c“, “d“, “e“; 7. 8. for(
25、int i=0; i5; i+) 9. hm.put (ki, vi); 10. System.out.print(hm.get(ki) + “ “); 11. 12. System.out.print(hm.size() + “ “ + hm.values() + “/n“); 13. What result is most likely?(分数:2.50)A.a b c a e 4 c,b,a,eB.a b c d e 4 c,b,a,eC.a b c d e 4 c,d,b,eD.a b c, followed by an exception.E.An exception is thro
26、wn with no other output.F.Compilation fails due to error(s) in the code.14.Given: 2. class Jiggy extends Thread 3. Jiggy(String n) super(n); 4. public void run() 5. for(int i = 0; i i00; i+) 6. if(“tl“.equals(Thread.currentThread() .getName( ) 8. throw new Error(); 9. 10. if(“t2“.equals(Thread.curre
27、ntThread() .getName() 12. throw new Error(); 13. 14. System.out.print (Thread.currentThread() .getName() + “-“); 15. 16. 17. public static void main(String args) 18. Thread t1 = new Jiggy(“t1“); 19. Thread t2 = new Jiggy(“t2“); 20. t1.setPriority(1); t2.setPriority(9); 21. t2.start(); t1.start(); 22
28、. Which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.After throwing error(s), t3 will most likely complete before t4.C.After throwing error(s), t4 will most likely complete before t3.D.The code will throw one error and then no more output will be produced.E.The code will throw tw
29、o errors and then no more output will be produced.F.After throwing error(s) it“s difficult to determine whether t3 or t4 will complete first.15.Given: 3. class Stereo void makeNoise() assert false; 4. public class BoomBox extends Stereo 5. public static void main(String args) 6. new BoomBex() .go(ar
30、gs); 7. 8. void go(String args) 9. if(args.length 0) makeNoise(); 10. if(!args0 .equals (“x“) System.out.println(“!x“); 11. And, if the code compiles, the invocation: java -ea BoomBox What is the result?(分数:2.50)A.!xB.Compilation fails.C.An AssertionError is thrown.D.A NullPointerException is thrown
31、.E.An IllegalArgumentException is thrown.F.An ArrayIndexOutOfBoundsException is thrown.16.Given: 1. public class LaSelva extends Beach 2. LaSelva() s = “LaSelva“; 3. public static void main(String args) new LaSelva().go(); 4. void go() 5. Beach ba= newBeach(), newLaSelva(), (Beach) newLaSelva() ; 6.
32、 for(Beach b: ba) System.out.print(b.getBeach().s + “ “); 7. 8. LaSelva getBeach() return this; 9. 10. class Beach 11. String s; 12. Beach() s = “Beach“; 13. Beach getBeach() return this; 14. What is the result?(分数:2.50)A.Beach LaSelva BeachB.Beach LaSelva LaSelvaC.Beach LaSelva followed by an excep
33、tion.D.Compilation fails due to an error at line 5.E.Compilation fails due to an error at line 6.F.Compilation fails due to an error at line 8.G.Compilation fails due to an error at line 13.17.When using the java.io.Console class, which are tree? (Choose all that apply.)(分数:2.50)A.Objects of type ja
34、va.io.Console are created using a constructor from the same class.B.Objects of type java.io.Console are created using a method from the java.io.File class.C.Objects of type java.io.Console are created using a method from the java.lang.System class.D.Objects of type java.io.Console are created using
35、a method from the java.lang.Object class.E.The method(s) designed to read passwords can optionally disable the echoing of user input.F.The method(s) designed to read passwords return a char.18.Given: 3. public class Stealth 4. public static void main(String args) 5. Integer i = 420; 6. Integer i2; 7
36、. Integer i3; 8. i2 = i.intValue(); 9. i3 = i.valueOf(420); 10. System.out.println(i = i2) + “ “ + (i = i3); 11. What is the result?(分数:2.50)A.true trueB.true falseC.false trueD.false falseE.Compilation fails.F.An exception is thrown at runtime.19.Given: 2. import java.io.*; 3. interface Risky 4. St
37、ring doStuff() throws Exception; 5. Risky doCrazy(); 6. void doInsane(); 7. 8. class Bungee implements Risky 9. public String doStuff() throws IOException 10. throw new IOException(); 11. 12. public Bungee doCrazy() return new Bungee(); 13. public void doInsane() throws NullPointerException 14. thro
38、w new NullPointerException(); 15. What is the result? (Choose all that apply.)(分数:2.50)A.Compilation succeeds.B.The Risky interface will not compile.C.The Bungee.doStuff() method will not compile.D.The Bungee.doCrazy() method will not compile.E.The Bungee.doInsane() method will not compile.20.Given
39、that IllegalArgumentException extends RuntimeException, and given: 11. static String s = “; 12. public static void main(String args) 13. try doStuff(); 14. catch (Exception ex) s += “c1 “; 15. System.out.println(s); 16. 17. static void doStuff() throws RuntimeException 18. try 19. s += “t1 “; 20. th
40、row new IllegalArgumentException(); 21. 22. catch (IllegalArgumentException ie) s += “c2 “; 23. throw new IllegalArgumentException(); 24. What is the result?(分数:2.50)A.c1 t1 c2B.c2 t1 c1C.t1 c1 c2D.t1 c2 c1E.Compilation fails.F.An uncaught exception is thrown at nmtime.21.Given: 1. public class Netw
41、orking 2. public static void main(String args) 3. ListInteger i = new LinkedListInteger(); 4. i.add(4); i.add(2); i.add(5); 5. int r = i; 6. doStuff(r); 7. System.out.println(i.get(r); 8. 9. static int doStuff(int x) 10. return +x; 11. What is the result?(分数:2.50)A.2B.4C.5D.Compilation fails.E.An ex
42、ception is thrown at runtime.22.Given: 1. import java.util.*; 2. public class Unturned 3. public static void main(String args) 4. String towns = “aspen“, “vail“, “t-ride“, “dillon“; 5. MySort ms = new MySort(); 6. Arrays.sort(towns, ms); 7. System.out.println(Arrays.binarySearch(towns, “dillon“); 8.
43、 9. static class MySort implements ComparatorString 10. public int compare(String a, String b) 11. return pareTo(a); 12. What is the most likely result?(分数:2.50)A.-1B.1C.2D.3E.Compilation fails.F.An exception is thrown at runtime.23.Given: 2. class Weed 3. protected static String s = “; 4. final voi
44、d grow() s += “grow “; 5. static final void growFast() s += “fast “; 6. 7. public class Thistle extends Weed 8. void grow() s += “t-grow “; 9. void growFast() s+= “t-fast “; 10. Which are the FEWEST change(s) required for this code to compile? (Choose all that apply.)(分数:2.50)A.s must be marked publ
45、ic.B.Thistle.grow() must be marked final.C.Weed.grow() must NOT be marked final.D.Weed.growFast() must NOT be marked final.E.Weed.growFast() must NOT be marked static.F.Thistle.growFast() must be removed from the class.24.Given: 2. import java.util.regex.*; 3. public class Decaf 4. public static voi
46、d main(String args) 5. Pattern p = Ppile(args0); 6. Matcher m = p.matcher(args1); 7. while (m.find() 8. System.out.print(re.group() + “ “); 9. And the three command-line invocations: . java Decaf “0(0-7)?“ “1012 0208 430“ . java Decaf “0(0-7)*“ “1012 0208 430“ . java Decaf “0(0-7)+“ “1012 0208 430“
47、Which are true? (Choose all that apply.)(分数:2.50)A.All three invocations will return valid octal numbers.B.None of the invocations will return valid octal numbers.C.Only invocations and will return valid octal numbers.D.All three invocations will return the same set of valid octal numbers.E.Of those invocations that return only valid octal numbers, each invocation will return a different set of valid octal numbers.25.Given: 1. class Locker extends Thread 2. private static Thread t; 3. public void run() 4. if (Thread.currentThread() = t) 5. System.out.print (“1 “); 6. synchronized(t) doSle