1、计算机 Java认证-4 及答案解析(总分:100.00,做题时间:90 分钟)一、不定项选择题(总题数:44,分数:100.00)1.Given: 2. public class Payroll 3. int salary; 4. int getSalary() return salary; 5. void setSalary(int s) 6. assert(s 30000); 7. salary = s; 8. Which are true? (Choose all that apply.)(分数:1.50)A.Compilation fails.B.The class is well
2、encapsulated as it stands.C.Removing line 6 would weaken the class“s degree of cohesion.D.Removing line 6 would weaken the class“s degree of encapsulation.E.If the salary variable was private, the class would be well encapsulated.F.Removing line 6 would make the class“s use of assertions more approp
3、riate.2.Given: 1. class GardenTool 2. static String s = “; 3. String name = “Tool “; 4. GardenTooi(String arg) this(); s += name; 5. GardenTool() s += “gt “; 6. 7. public class Rake extends GardenTool 8. name = “Rake “; 9. Rake(String arg) s += name; 10. public static void main(String args) 11. new
4、GardenTool(“hey “); 12. new Rake(“hi “); 13. System.out.println(s); 14. 15. name = “myRake “; 16. What is the result?(分数:1.50)A.Tool RakeB.Tool myRakeC.gt Tool RakeD.gt Tool myRakeE.gt Tool gt RakeF.gt Tool gt myRakeG.gt Tool gt Tool myRakeH.Compilation fails.3.Given: 3. public class Race 4. public
5、static void main(String args) 5. Horse h = new Horse(); 6. Thread t1 = new Thread(h, “Andi“); 7. Thread t2 = new Thread(h, “Eyra“); 8. new Race() .go(t2); 9. t1.start(); 10. t2.start(); 11. 12. void go(Thread t) t.start(); 13. 14. class Horse implements Runnable 15. public void run() 16. System.out.
6、print(Thread.currentThread() .getName() + “ “); 17. What is the result? (Choose all that apply.)(分数:1.50)A.Compilation fails.B.No output is produced.C.The output could be: “Andi Eyra“D.The output could be: “Eyra Andi Eyra“E.The output could be: “Eyra “, followed by an exception.F.The output could be
7、: “Eyra Andi “, followed by an exception.4.Given the proper import statement(s) and given: 4. MapString, String h = new HashtableString, String(); 5. String k = “1“, “2“, “3“, null; 6. String v = “a“, “b“, null, “d“; 7. 8. for(int i=0; i4; i+) 9. h.put(ki, vi); 10. System.out.print(h. get (k i) + “
8、“); 11. 12. System.out.print(h.size() + “ “ + h.values() + “/n“); What result is most likely?(分数:1.50)A.Compilation fails.B.“a b d 3 b, d, a“C.“a b null d 3 b, d, a“D.“a b null d 4 b, d, null, a“E.“a b“, followed by an exception.F.“a b null“, followed by an exception.5.Given: 3. public class Fiji 4.
9、 static Fiji base; 5. Fiji f; 6. public static void main(String args) 7. new Fiji() .go(); 8. / do more stuff 9. 10. void go() 11. Fiji f1 = new Fiji(); 12. base = f1; 13. Fiji f2 = new Fiji(); 14. f1.f = f2; 15. Fiji f3 = f1.f; 16. f2.f = f1; 17. base = null; f1 = null; f2 = null; 18. / do stuff 19
10、. Which are true? (Choose all that apply.)(分数:1.50)A.At line 8, one object is eligible for garbage collection.B.At line 8, two objects are eligible for garbage collection.C.At line 8, three objects are eligible for garbage collection.D.At line 18, 0 objects are eligible for garbage collection.E.At l
11、ine 18, two objects are eligible for garbage collection.F.At line 18, three objects are eligible for garbage collection.6.Your company makes compute-intensive, 3D rendering software for the movie industry. Your chief scientist has just discovered a new algorithm for several key methods in a commonly
12、 used utility class. The new algorithm will decrease processing time by 15 percent, without having to change any method signatures. After you change these key methods, and in the course of rigorous system testing, you discover that the changes have introduced no new bugs into the software. In terms
13、of your software“s overall design, which are probably true? (Choose all that apply.)(分数:1.50)A.Your software is well encapsulated.B.Your software demonstrated low cohesion.C.Your software demonstrated high cohesion.D.Your software demonstrated loose coupling.E.Your software demonstrated tight coupli
14、ng.7.Which are true about the classes and interfaces in j ava.util? (Choose all that apply.)(分数:1.50)A.LinkedHashSet is-a Collection.B.Vector is-a List.C.LinkedList is-a Queue.D.LinkedHashMap is-a Collection.E.TreeMap is-a SortedMap.F.Queue is-a Collection.G.Hashtable is-a Set.8.Given: 2. class Gran
15、dfather 3. static String name = “gf “; 4. String doStuff() return “grandf “; 5. 6. class Father extends Grandfather 7. static String name = “fa “; 8. String doStuff() return “father “; 9. 10. public class Child extends Father 11. static String name = “ch “; 12. String doStuff() return “child “; 13.
16、public static void main(String args) 14. Father f = new Father(); 15. System.out.print(Grandfather)f).name + (Grandfather)f) .doStuff(); 16. Child c = new Child(); 17. System. out.println(Grandfather) c) .name + (Grandfather)c) .doStuff() + (Father)c) .doStuff(); 18. What is the result? (Choose all
17、that apply.)(分数:1.50)A.Compilation fails.B.fa father ch child childC.gf father gf child childD.fa grandf ch grandf fatherE.gf grandf gf grandf fatherF.An exception is thrown at runtime.9.Given: 1. class MyClass And given that MyClass2 has properly overridden equals() and hashCode() objects from whic
18、h classes make good hashing keys? (Choose all that apply.)(分数:1.50)A.MyClassB.MyClass2C.java.lang.StringD.java.lang.IntegerE.java.lang.StringBuilder10.Given: 2. public class Buffalo 3. static int x; 4. int y; 5. public static int getX() return x; 6. public static void setX(int newX) x = newX; 7. pub
19、lic int getY() return y; 8. public void setY(int newY) y = newY; 9. Which lines of code need to be changed to make the class thread safe? (Choose all that apply.)(分数:1.50)A.Line 2B.Line 3C.Line 4D.Line 5E.Line 6F.Line 7G.Line 811.Given: 2. class Animal 3. class Dog extends Animal 4. class Cat extend
20、s Animal 5. public class MixerA extends Animal 6. public C extends Cat Mixer? super Dog useMe(A a, C c) 7. /Insert Code Here 8. Which, inserted independently at line 7, compile? (Choose all that apply.)(分数:2.50)A.return null;B.return new MixerDog();C.return new MixerAnimal();D.return new MixerA();E.
21、return new Mixera();F.return new Mixerc();12.Given: 2. class Chilis 3. Chilis(String c, int h) color = c; hotness = h; 4. String color; 5. private int hotness; 6. public boolean equals(Object o) 7. Chilis c = (Chilis)o; 8. if(color.equals(c.color) 9. return false; 10. 11. / insert code here 12. Whic
22、h, inserted independently at line 11, fulfill the equals() and hashCode() contract for Chilis? (Choose all that apply.)(分数:2.50)A.public int hashCode() return 7; B.public int hashCode() return hotness; C.public int hashCode() return color.length(); D.public int hashCode() return (int)(Math.random()
23、* 200); E.public int hashCode() return (color.length() + hotness); 13.Given the proper import(s), and this code in a method: 4. ListString x = new LinkedListString(); 5. SetString hs = new HashSetString(); 6. String v = “a“, “b“, “c“, “b“, “a“; 7. for(String s: v) 8. x.add(s); hs.add(s); 9. 10. Syst
24、em.out.print(hs.size() + “ “ + x.size() + “ “); 11. HashSet hs2 = new HashSet(x); 12. LinkedList x2 = new LinkedList(hs); 13. System.out.println(hs2.size() + “ “ + x2.size(); What is the result?(分数:2.50)A.3 3 3 3B.3 5 3 3C.3 5 3 5D.5 5 3 3E.5 5 5 5F.Compilation fails.G.An exception is thrown at runt
25、ime.14.Given the proper import(s), and given: 4. public static void main(String args) 5. ListInteger x = new ArrayListInteger(); 6. x.add(new Integer(3); 7. doStuff (x); 8. for(Integer i: x) 9. System.out.print (i + “ “); 10. 11. static void doStuff(List y) 12. y.add(new Integer(4); 13. y.add(new Fl
26、oat(3.14f); 14. What is the result? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.The output will be “4 “C.The output will be “3 4“D.The output will be “3 4 3.14“E.The output will be “3 3.14 4 “F.The output will be “3 4 “, followed by an exception.G.An exception will be thrown before any ou
27、tput is produced.15.Given: 3. class Bonds 4. Bonds force() return new Bonds(); 5. 6. public class Covalent extends Bonds 7. Covalent force() return new Covalent(); 8. public static void main(String args) 9. new Covalent() .go(new Covalent(); 10. 11. void go(Covalent c) 12. go2(new Bonds() .force(),
28、c.force(); 13. 14. void go2(Bonds b, Covalent c) 15. Covalent c2 = (Covalent)b; 16. Bonds b2 = (Bonds)c; 17. What is the result? (Choose all that apply.)(分数:2.50)A.A ClassCastException is thrown at line 15.B.A ClassCastException is thrown at line 16.C.Compilation fails due to an error on line 7.D.Co
29、mpilation fails due to an error on line 12.E.Compilation fails due to an error on line 15.F.Compilation fails due to an error on line 16.16.Given: 1. import java.util.*; 2. public class Drawers 3. public static void main(String args) 4. ListString desk = new ArrayListString(); 5. desk.add(“pen“); de
30、sk.add(“scissors“); desk.add(“redStapler“); 6. System.out.print(desk.indexOf(“redStapler“); 7. Collection.reverse(desk); 8. System.out.print(“ “ + desk.indexOf(“redStapler“); 9. Collection.sort(desk); 10. System.out.println(“ “ + desk.indexOf(“redStapler“); 11. What is the result?(分数:2.50)A.1 1 1B.2
31、 0 1C.2 0 2D.2 2 2E.Compilation fails.F.An exception is thrown at runtime.17.Given: 1. import java.util. *; 2. class Radio 3. String getFreq() return “97.3“; 4. static String getF() return “97.3“; 5. 6. vclass Ham extends Radio 7. String getFreq() return “50.1“; 8. static String getF() return “50.1“
32、; 9. public static void main(String args) 10. ListRadio radios = new ArrayListRadio(); 11. radios.add(new Radio(); 12. radios.add(new Ham(); 13. for(Radio r: radios) 14. System.out.print(r.getFreq() + “ “ + r.getF() + “ “); 15. What is the result?(分数:2.50)A.50.1 50.1 50.1 50.1B.50.1 97.3 50.1 97.3C.
33、97.3 50.1 50.1 50.1D.97.3 97.3 50.1 50.1E.97.3 97.3 50.1 97.3F.97.3 97.3 97.3 97.3G.Compilation fails.H.An exception is thrown at runtime.18.Given two files: 1. package com; 2. public class MyClass 3. public static void howdy() System.out.print(“howdy “); 4. public static final int myConstant = 343;
34、 5. public static final MyClass mc = new MyClass(); 6. public int instVar = 42; 7. 11. import static com. MyClass.*; 12. public class TestImports 13. public static void main(String args) 14. System.out.print(myConstant + “ “); 15. howdy(); 16. System.out.print(mc.instVar + “ “); 17. System.out.print
35、(instVar + “ “); 18. What is the result? (Choose ALL that apply.)(分数:2.50)A.343 howdy 42 42B.Compilation fails due to an error on line 11.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 d
36、ue to an error on line 17.19.Given this code in a method: 4. int x = 0; 5. int primes = 1,2,3,5; 6. for(int i: primes) 7. switch (i) 8. case 1: x += i; 9. case 5: x += i; 10. default: x += i; 11. case 2: x += i; 12. 13. System.out.println (x); What is the result?(分数:2.50)A.11B.13C.24D.27E.Compilatio
37、n fails due to an error on line 7.F.Compilation fails due to an error on line 10.G.Compilation fails due to an error on line 11.20.Given: 1. import java.util.*; 2. public class Elway 3. public static void main(String args) 4. ArrayList ls = new ArrayList3; 5. for(int i = 0; i 3; i+) 6. lsi = new Arr
38、ayList(); 7. lsi.add(“a“ + i); 8. 9. Object o = ls; 10. do3(ls) ; 11. for(int i = 0; i 3; i+) 12. / insert code here 13. 14. 15. static Object do3(ArrayList a) 16. for(int i = 0; i 3; i+) ai .add(“e“); 17. return a; 18. And the following fragments: . System.out.print (o i + “ “); . System.out.print(
39、ArrayList) i + “ “); . System.out.print( (Object)o) i + “ “); . System.out.print(ArrayList)o) i + “ “); If the fragments are added to line 12, independently, which are true? (Choose all that apply.)(分数:2.50)A.Fragment will compile.B.Fragment will compile.C.Fragment will compile.D.Fragment will compi
40、le.E.Compilation fails due to other errors.F.Of those that compile, the output will be a0 a1 a2G.Of those that compile, the output will be a0, e a1, e a2, e21.Given: 1. enum MyEnum HI, ALOHA, HOWDY; 2. public class PassEnum 3. public static void main(String args) 4. PassEnum p = new PassEnum(); 5. M
41、yEnum v = MyEnum.values(); 6. v = MyEnum.getValues(); 7. for(MyEnum me: MyEnum.values() p.getEnum(me); 8. for(int x = 0; xMyEnum.values().length; x+) p.getEnum(vx); 9. for(int x = 0; x MyEnum.length; x+) p.getEnum(vx); 10. for(MyEnum me: v) p.getEnum(me); 11. 12. public void getEnum(MyEnum e) 13. Sy
42、stem.out.print(e + “ “); 14. Which line(s) of code will cause a compiler error? (Choose all that apply.)(分数:2.50)A.line 1B.line 5C.line 6D.line 7E.line 8F.line 9G.line 10H.line 1222.Given: 3. public class Drip extends Thread 4. public static void main(String args) 5. Thread t1 = new Thread(new Drip(
43、); 6. t1.start(); 7. t1.join(); 8. for(int i = 0; i 1000; i+) / Loop #1 9. System.out.print(Thread.currentThread() .getName() + “ “); 10. 11. public void run() 12. for(int i = 0; i 1000; i+) / Loop #2 13. System.out.print(Thread.currentThread() .getName() + “ “); 14. Which are true? (Choose all that
44、 apply.)(分数:2.50)A.Compilation fails.B.An exception is thrown at runtime.C.Loop #1 will run most of its iterations before Loop #2.D.Loop #2 will run most of its iterations before Loop #1.E.There is no way to predict which loop will mostly run first.23.Given this code in a method: 5. int x = 3; 6. fo
45、r(int i = 0; i 3; i+) 7. if(i = 1) x = x+; 8. if(i % 2 = 0 9. if(i % 2 = 0 10. if(i = 2 x = 4) System.out.print(“,“); 11. 12. System. out.println (“); What is the result?(分数:2.50)A.-.,B.-,C.-.,D.-,E.-.-,F.Compilation fails.24.Given: 2. public class Incomplete 3. public static void main(String args)
46、/ change code here? 4. / insert code here? 5. new Incomplete() .doStuff(); 6. / insert code here? 10. 11. static void doStuff() throws Exception 12. throw new Exception(); 13. Which are true? (Choose all that apply.)(分数:2.50)A.Compilation succeeds with no code changes.B.Compilation succeeds if main(
47、) is changed to throw an Exception.C.Compilation succeeds if a try-catch is added, surrounding line 5.D.Compilation succeeds if a try-finally is added, surrounding line 5.E.Compilation succeeds only if BOTH main() declares an Exception AND a try-catch is added, surrounding line 5.25.Given the proper
48、 imports, and given: 24. Date d1 = new Date(); 25. Date d2 = d1; 26. System.out.println(d1); 27. d2.setTime(d1.getTime() + (7 * 24 * 60 * 60); 28. System.out.println(d2); Which are true? (Choose all that apply.)(分数:2.50)A.Compilation fails.B.An exception is thrown at runtime.C.Some of the output will be today“s date.D.Some of the output will be next week“s dat