《Java基础选择题.docx》由会员分享,可在线阅读,更多相关《Java基础选择题.docx(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、QUESTION 1 Given a pre-generics implementation of a method: 11. public static int sum(List list) 12. int sum = 0;13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) 14. int i = (Integer)iter.next().intValue(); 15. sum += i; 16. 17. return sum; 18. What three changes allow the class to be us
2、ed with generics and avoid an unchecked warning? (Choose three.) A. Remove line 14. B. Replace line 14 with int i = iter.next();. C. Replace line 13 with for (int i : intList) . D. Replace line 13 with for (Iterator iter : intList) . E. Replace the method declaration with sum(List intList). F. Repla
3、ce the method declaration with sum(List intList). Explanation/Reference: Explanation: QUESTION 2 A programmer has an algorithm that requires a java.util.List that provides an efficient implementation of add(0, object), but does NOT need to support quick random access. What supports these requirement
4、s? A. java.util.QueueB. java.util.ArrayList C. java.util.LinearList D. java.util.LinkedList Explanation/Reference: Explanation: QUESTION 3 Given:11. / insert code here 12. private N min, max;13. public N getMin() return min; 14. public N getMax() return max; 15. public void add(N added) 16. if (min
5、= null | added.doubleValue() max.doubleValue()19. max = added;20. 21. Which two, inserted at line 11, will allow the code to compile? (Choose two.) A. public class MinMax B. public class MinMax C. public class MinMax D. public class MinMax E. public class MinMax F. public class MinMax Explanation/Re
6、ference: Explanation: QUESTION 4 Given: 12. import java.util.*;13. public class Explorer2 14. public static void main(String args) 15. TreeSet s = new TreeSet(); 16. TreeSet subs = new TreeSet(); 17. for(int i = 606; i 613; i+)18. if(i%2 = 0) s.add(i);19. subs = (TreeSet)s.subSet(608, true, 611, tru
7、e); 20. s.add(629);21. System.out.println(s + + subs);22. 23. What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. 608, 610, 612, 629 608, 610 D. 608, 610, 612, 629 608, 610, 629 E. 606, 608, 610, 612, 629 608, 610 F. 606, 608, 610, 612, 629 608, 610, 629 Explanation/Re
8、ference: Explanation: QUESTION 5 Given: 1. public class Score implements Comparable 2. private int wins, losses; 3. public Score(int w, int l) wins = w; losses = l; 4. public int getWins() return wins; 5. public int getLosses() return losses; 6. public String toString() 7. return ; 8. 9. / insert co
9、de here10. Which method will complete this class? A. public int compareTo(Object o)/*more code here*/ B. public int compareTo(Score other)/*more code here*/ C. public int compare(Score s1,Score s2)/*more code here*/ D. public int compare(Object o1,Object o2)/*more code here*/ Explanation/Reference:
10、Explanation: QUESTION 6 Given:11. public class Person 12. private name;13. public Person(String name) 14. this.name = name;15. 16. public int hashCode() 17. return 420;18. 19. Which statement is true? A. The time to find the value from HashMap with a Person key depends on the size of the map. B. Del
11、eting a Person key from a HashMap will delete all map entries for all keys of type Person. C. Inserting a second Person object into a HashSet will cause the first Person object to be removed as a duplicate. D. The time to determine whether a Person object is contained in a HashSet is constant and do
12、es NOT depend on the size of the map. Explanation/Reference: Explanation: QUESTION 7 Given:5. import java.util.*;6. public class SortOf 7. public static void main(String args) 8. ArrayList a = new ArrayList(); 9. a.add(1); a.add(5); a.add(3);11. Collections.sort(a);12. a.add(2);13. Collections.rever
13、se(a);14. System.out.println(a);15. 16. What is the result?A. 1, 2, 3, 5 B. 2, 1, 3, 5 C. 2, 5, 3, 1 D. 5, 3, 2, 1 E. 1, 3, 5, 2 F. Compilation fails. G. An exception is thrown at runtime. Explanation/Reference: Explanation: QUESTION 8 Given11. public interface Status 12. /* insert code here */ int
14、MY_VALUE = 10; 13. Which three are valid on line12?(Choose three.) A. finalB. staticC. nativeD. publicE. privateF. abstract G. protected Explanation/Reference: Explanation: QUESTION 9 Given:5. class Atom 6. Atom() System.out.print(atom ); 7. 8. class Rock extends Atom 9. Rock(String type) System.out
15、.print(type); 10. 11. public class Mountain extends Rock 12. Mountain() 13. super(granite );14. new Rock(granite ); 15. 16. public static void main(String a) new Mountain(); 17. What is the result? A. Compilation fails. B. atom granite C. granite granite D. atom granite granite E. An exception is th
16、rown at runtime. F. atom granite atom granite Explanation/Reference: Explanation: QUESTION 10 Click the Exhibit button. Which three statements are true? (Choose three.) A. Compilation fails. B. The code compiles and the output is 2. C. If lines 16, 17 and 18 were removed, compilation would fail. D.
17、If lines 24, 25 and 26 were removed, compilation would fail. E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2. F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1. Explanation/Reference: Explanation: QUESTION 11 Given: 10.
18、class Line 11. public class Point public int x,y; 12. public Point getPoint() return new Point(); 13. 14. class Triangle 15. public Triangle() 16. / insert code here 17. 18. Which code, inserted at line 16, correctly retrieves a local instance of a Point object? A. Point p = Line.getPoint(); B. Line
19、.Point p = Line.getPoint(); C. Point p = (new Line().getPoint(); D. Line.Point p = (new Line().getPoint(); Explanation/Reference: Explanation: QUESTION 12 Given:11. class Alpha 12. public void foo() System.out.print(Afoo ); 13. 14. public class Beta extends Alpha 15. public void foo() System.out.pri
20、nt(Bfoo ); 16. public static void main(String args) 17. Alpha a = new Beta();18. Beta b = (Beta)a; 19. a.foo(); 20. b.foo(); 21. 22. What is the result? A. Afoo Afoo B. Afoo Bfoo C. Bfoo Afoo D. Bfoo Bfoo E. Compilation fails. F. An exception is thrown at runtime. Explanation/Reference: Explanation:
21、 QUESTION 13 Click the Exhibit button. Which statement is true about the classes and interfaces in the exhibit? A. Compilation will succeed for all classes and interfaces. B. Compilation of class C will fail because of an error in line 2. C. Compilation of class C will fail because of an error in li
22、ne 6. D. Compilation of class AImpl will fail because of an error in line 2. Explanation/Reference: Explanation: QUESTION 14 Which two code fragments correctly create and initialize a static array of int elements? (Choose two.) A. static final int a = 100,200 ; B. static final int a;static a=new int
23、2; a0=100; a1=200; C. static final int a = new int2 100,200 ; D. static final int a;static void init() a = new int3; a0=100; a1=200; Explanation/Reference: Explanation: QUESTION 15 Given:10. interface Foo int bar(); 11. public class Sprite 12. public int fubar( Foo foo ) return foo.bar(); 13. public
24、 void testFoo() 14. fubar(15. / insert code here16. );17. 18. Which code, inserted at line 15, allows the class Sprite to compile? A. Foo public int bar() return 1; B. new Foo public int bar() return 1; C. new Foo() public int bar() return 1; D. new class Foo public int bar() return 1; Explanation/R
25、eference: Explanation: QUESTION 16 Given:1. class Alligator 2. public static void main(String args) 3. int x = 1,2, 3,4,5, 6,7,8,9;4. int y = x;5. System.out.println(y21);6. 7. What is the result? A. 2 B. 3 C. 4 D. 6 E. 7 F. Compilation fails.Section: (none) Explanation/Reference: Explanation: QUEST
26、ION 17 Given: 22. StringBuilder sb1 = new StringBuilder(123); 23. String s1 = 123; 24. / insert code here25. System.out.println(sb1 + + s1); Which code fragment, inserted at line 24, outputs 123abc 123abc? A. sb1.append(abc); s1.append(abc); B. sb1.append(abc); s1.concat(abc); C. sb1.concat(abc); s1
27、.append(abc); D. sb1.concat(abc); s1.concat(abc); E. sb1.append(abc); s1 = s1.concat(abc); F. sb1.concat(abc); s1 = s1.concat(abc); G. sb1.append(abc); s1 = s1 + s1.concat(abc); H. sb1.concat(abc); s1 = s1 + s1.concat(abc); Explanation/Reference: Explanation: QUESTION 18 Given that the current direc
28、tory is empty, and that the user has read and write permissions, and the following: 11. import java.io.*;12. public class DOS 13. public static void main(String args) 14. File dir = new File(dir);15. dir.mkdir();16. File f1 = new File(dir, f1.txt);17. try 18. f1.createNewFile();19. catch (IOExceptio
29、n e) ; 20. File newDir = new File(newDir); 21. dir.renameTo(newDir); 22. 23. Which statement is true? A. Compilation fails. B. The file system has a new empty directory named dir. C. The file system has a new empty directory named newDir. D. The file system has a directory named dir, containing a fi
30、le f1.txt. E. The file system has a directory named newDir, containing a file f1.txt. Explanation/Reference: Explanation: QUESTION 19 Given: 11. class Converter 12. public static void main(String args) 13. Integer i = args0; 14. int j = 12; 15. System.out.println(It is + (j=i) + that j=i.); 16. 17.
31、What is the result when the programmer attempts to compile the code and run it with the command java Converter 12? A. It is true that j=i. B. It is false that j=i. C. An exception is thrown at runtime. D. Compilation fails because of an error in line 13. Explanation/Reference: Explanation: QUESTION
32、20 Given:11. String test = Test A. Test B. Test C.; 12. / insert code here 13. String result = test.split(regex); Which regular expression, inserted at line 12, correctly splits test into Test A, Test B, and Test C? A. String regex = ; B. String regex = ; C. String regex = .*; D. String regex = s; E
33、. String regex = .s*; F. String regex = w . +; Explanation/Reference: Explanation: QUESTION 21 Given:5. import java.util.Date;6. import java.text.DateFormat;21. DateFormat df;22. Date date = new Date();23. / insert code here24. String s = df.format(date);Which code fragment, inserted at line 23, all
34、ows the code to compile? A. df = new DateFormat(); B. df = Date.getFormat(); C. df = date.getFormat(); D. df = DateFormat.getFormat(); E. df = DateFormat.getInstance(); Explanation/Reference: Explanation: QUESTION 22 Given a class Repetition:1. package utils;2.3. public class Repetition 4. public st
35、atic String twice(String s) return s + s; 5. and given another class Demo: 1. / insert code here 2. 3. public class Demo 4. public static void main(String args) 5. System.out.println(twice(pizza); 6. 7. Which code should be inserted at line 1 of Demo.java to compile and run Demo to print pizzapizza? A. import utils.*; B. static import utils.*; C. import utils.Repetition.*; D. static import utils.Repetition.*; E. import utils.Repetition.twice(); F. import static utils.Repetition.twice; G. static import utils.Repetition.twice;
限制150内