JAVA基础面试题-3(答案版).doc
JAVA语言基础笔试题-3Question 1Given: 1. public class Team extends java.util.LinkedList 2. public void addPlayer(Player p) 3. add(p); 4. 5. public void compete(Team opponent) /* more code here */ 6. 7. class Player /* more code here */ Which two are true? (Choose two.) A. This code will compile. B. This code demonstrates proper design of an is-a relationship. C. This code demonstrates proper design of a has-a relationship. D. A Java programmer using the Team class could remove Player objects from a Team object.答案: AD考点: List接口实现,对象间泛化和关联关系说明: is a 关系一般用继承来表示,但此题这个继承从逻辑上不是太恰当. Has a 关系一般来说表示某对象体内有其它对象的存在,该对象体现为属性形态,在UML中叫做关联关系。本题Team中虽然可以保存Player,但并非显式以属性形式存在。 由于LinkedList自带remove方法,可以通过Team.remove(p) 来删除player对象。 Question 2Which four are true? (Choose four.) A. Has-a relationships should never be encapsulated. B. Has-a relationships should be implemented using inheritance. C. Has-a relationships can be implemented using instance variables. D. Is-a relationships can be implemented using the extends keyword. E. Is-a relationships can be implemented using the implements keyword. F. The relationship between Movie and Actress is an example of an is-a relationship. G. An array or a collection can be used to implement a one-to-many has-a relationship.答案:CDEG考点: 集合类型,对象间泛化和关联关系的理解Has a 关系一般表示为一个类拥有一个属性,属性被封装是常见的事情。Is a 关系一般用继承来表示,子类体内拥有父类部分。接口的实现,也适用于 is a 关系来理解,因为接口从本质来说,也属于类的形态。Question 3Which two are true about has-a and is-a relationships? (Choose two.) A. Inheritance(继承) represents an is-a relationship. B. Inheritance represents a has-a relationship. C. Interfaces must be used when creating a has-a relationship. D. Instance variables can be used when creating a has-a relationship.答案:AD考点:对象间泛化和关联关系的理解Question 4Given: 10. interface Jumper public void jump(); .20. class Animal .30. class Dog extends Animal 31. Tail tail; 32. .40. class Beagle extends Dog implements Jumper 41. public void jump() 42. .50. class Cat implements Jumper 51. public void jump() 52. Which three are true? (Choose three.) A. Cat is-a Animal B. Cat is-a Jumper C. Dog is-a Animal D. Dog is-a Jumper E. Cat has-a Animal F. Beagle has-a Tail G. Beagle has-a Jumper答案:BCF考点:对象间泛化和关联关系的理解Question 5Given: 1. import java.util.*; 2. public class Example 3. public static void main(String args) 4. / insert code here 5. set.add(new Integer(2); 6. set.add(new Integer(l); 7. System.out.println(set); 8. 9. Which code, inserted at line 4, guarantees that this program will output 1, 2? A. Set set = new TreeSet(); B. Set set = new HashSet(); C. Set set = new SortedSet(); D. List set = new SortedList(); E. Set set = new LinkedHashSet();答案:A考点:集合类型的概念,TreeSet的特性说明:HashSet:底层数据结构是hash表,先判断hash值,然后比较值TreeSet:数据结构二叉排序树,可以对set集合中的元素进行排序,自定义类需要实现comparable接口(排序时,当主要条件相同时,一定要判断次要条件) 此题从输出角度来看,属于天然顺序输出,目前只有TreeSet能达到该效果,同时Integer类也实现了Comparable接口,所以可以安全的在TreeSet中使用。Question 6Given: 1. import java.util.*; 2. public class PQ 3. public static void main(String args) 4. PriorityQueue<String> pq = new PriorityQueue<String>(); 5. pq.add(”carrot”); 6. pq.add(”apple”); 7. pq.add(”banana”); 8. System.out.println(pq.poll() +”:” + pq.peek(); 9. 10. What is the result? A. apple:apple B. carrot:apple C. apple:banana D. banana:apple E. carrot:carrot F. carrot:banana答案:C考点:集合类型,了解Queue接口的使用说明:优先级队列,如果不提供Comparable的话,优先级队列中的元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列 Poll()从队列头部取值,该值从队列中删除; peek()并未实际取值,而仅是取得队列头部的元素。Question 7Given: 1. import java.util.*; 2. public class WrappedString 3. private String s; 4. public WrappedString(String s) this.s = s; 5. public static void main(String args) 6. HashSet<Object> hs = new HashSet<Object>();7. WrappedString ws1 = new WrappedString(”aardvark”); 8. WrappedString ws2 = new WrappedString(”aardvark”); 9. String s1 = new String(”aardvark”); 10. String s2 = new String(”aardvark”); 11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2); 12. System.out.println(hs.size(); What is the result? A. 0 B. 1 C. 2 D. 3 E. 4 F. Compilation fails. G. An exception is thrown at runtime.答案:D考点:集合类型,hashset的理解,hashcode和equals调用契约说明: WrappedString类没有重写hashcode方法,那么这个类的两个对象在装入hashset的过程中,将会使用Object所提供的hashcode调用结果,由于Object的hashcode值均为不同,所以WrappedString两个对象全被hashset接收。 由于这里显式进行字符串构建,所以会创建两个独立的,有相同内容的字符串对象,但是由于String类已经完整重写了hashcode和equals方法,所以具有相同内容的两个对象,将无法同时保存入hashset.Question 8Click the Exhibit button. 1. import java.util.*; 2. public class TestSet 3. enum Example ONE, TWO, THREE 4. public static void main(String args) 5. Collection coll = new ArrayList();6. coll.add(Example.THREE); 7. coll.add(Example.THREE); 8. coll.add(Example.THREE); 9. coll.add(Example.TWO); 10. coll.add(Example.TWO); 11. coll.add(Example.ONE); 12. Set set = new HashSet(coll); 13. 14. Which statement is true about the set variable on line 12? A. The set variable contains all six elements from the coll collection, and the order is guaranteed to be preserved. B. The set variable contains only three elements from the coll collection, and the order is guaranteed to be preserved. C. The set variable contains all six elements from the coil collection, but the order is NOT guaranteed to be preserved. D. The set variable contains only three elements from the coil collection, but the order is NOT guaranteed to be preserved.答案:D考点:枚举类型,集合类型(ArrayList, HashSet)Question 9Given: 1. public class Score implements Comparable<Score> 2. private int wins, losses; 3. public Score(int w, int 1) wins = w; losses = 1; 4. public int getWins() return wins; 5. public int getLosses() return losses; 6. public String toString() 7. return “<“ + wins + “,“ + losses + “>”;8. 9. / insert code here 10. Which method will complete this class? A. public int compareTo(Object o) /*mode 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*/答案:B考点:Comparable接口的应用理解说明:任何实现了Comparable接口的类对象均可放置在TreeSet中使用,该接口只有一个方法,叫做CompareTo, 同时该接口是泛型的,所以参数数据类型不应该是Object,而是Score类型。Question 10A 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 requirements? A. java.util.Queue B. java.util.ArrayList C. java.util.LinearList D. java.util.LinkedList答案:D考点:List接口常见实现类的理解说明: LinkedList使用链表来进行对象的保存,虽然消耗内存空间较多,但是提供较高的插入和删除的性能。Question 11Given: 11. public class Person 12. private String name, comment; 13. private int age; 14. public Person(String n, int a, String c) 15. name = n; age = a; comment = c; 16. 17. public boolean equals(Object o) 18. if(! (o instanceof Person) return false; 19, Person p = (Person)o; 20. return age = p.age && name.equals(p.name); 21. 22. What is the appropriate definition of the hashCode method in class Person? A. return super.hashCode();B. return name.hashCode() + age * 7; C. return name.hashCode() + comment.hashCode() /2; D. return name.hashCode() + comment.hashCode() / 2 - age * 3;答案:B考点:Hashcode和equals的实现契约说明: Hashcode不同,equals一定不同; Hashcode相同,equals可以不同。 以上为hashcode和equals的实现契约,这两个方法重写好了,该对象就可以存入hashset中,同时也可以做hashMap的key。 选项A:在age,name都相同的情况下,hashcode仍然是不同的,是错误的。 选项CD:因为comment并未参与equals判断,确参与了hashcode判断,显然无法满足的契约的需求。Question 12Given: 11. public class Key 12. private long id1; 13. private long 1d2; 14. 15. / class Key methods 16. A programmer is developing a class Key, that will be used as a key in a standard java.util.HashMap. Which two methods should be overridden to assure that Key works correctly as a key? (Choose two.) A. public int hashCode() B. public boolean equals(Key k) C. public int compareTo(Object o) D. public boolean equals(Object o) E. public boolean compareTo(Key k)答案:AD考点:对HashMap的Key的特点的理解说明: 因为HashMap中的所有的key是使用Hashset来管理的,所以其内部所有的key对象均必须能够支持在HashSet环境下正常工作,所以实现hashcode和equals方法成为了必然。 这里要注意一点, equals方法不是泛型的,其参数数据类型为Object.Question 13Given: 11. public class Person 12. private name; 13. public Person(String name) 14. this.name = name; 15. 16. public boolean equals(Object o) 17. if( !o instanceof Person ) return false; 18. Person p = (Person) o; 19. return p.name.equals(this.name); 20. 21. Which is true? A. Compilation fails because the hashCode method is not overridden. B. A HashSet could contain multiple Person objects with the same name. C. All Person objects will have the same hash code because the hashCode method is not overridden. D. If a HashSet contains more than one Person object with name=”Fred”, then removing another Person, also with name=”Fred”, will remove them all.答案:B考点:hashcode和equals的契约理解说明: 一个类如果没有重写hashcode方法,将直接使用父类Object的HashCode方法,而Object的该方法,不同的对象均为不同的值。用对象匹配删除的时候,无论这个对象的属性写的多么雷同,结果都删除不掉或者仅仅删除hashcode值匹配的那一个对象。Question 14Given: 1. public class Person 2. private String name; 3. public Person(String name) this.name = name; 4. public boolean equals(Person p) 5. return p.name.equals(this.name); 6. 7. Which is true? A. The equals method does NOT properly override the Object.equals method. B. Compilation fails because the private attribute p.name cannot be accessed in line 5. C. To work correctly with hash-based data structures, this class must also implement the hashCode method. D. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.答案:A考点:equals方法签名的理解说明: 本题equals方法的签名书写是错误的,重写失败,此处应该算是对Object.equals方法的重载实现,所以在常规有应用equals的环境中,该方法是不会调用到的。 p.name属于从引用角度查看访问修饰符,是可以访问的。 选项C不应该是实现,而必须是重写。 选项D描述不大清晰,实际上很多SET结构是不依靠equals做雷同性判别的。Question 15Which two statements are true about the hashCode method? (Choose two.) A. The hashCode method for a given class can be used to test for object equality and object inequality for that class. B. The hashCode method is used by the java.util.SortedSet collection class to order the elements within that set. C. The hashCode method for a given class can be used to test for object inequality, but NOT object equality, for that class. D. The only important characteristic of the values returned by a hashCode method is that the distribution of values must follow a Gaussian distribution. E. The hashCode method is used by the java.util.HashSet collection class to group the elements within that set into hash buckets for swift retrieval.答案:AE考点:Hashset数据类型的理解。Question 16Click the Exhibit button. 1. import java.util.*; 2. class KeyMaster 3. public int i; 4. public KeyMaster(int i) this.i = i; 5. public boolean equals(Object o) return i = (KeyMaster)o).i; 6. public int hashCode() return i; 7. 8. public class MapIt 9. public static void main(String args) 10. Set<KeyMaster> set = new HashSet<KeyMaster>();11. KeyMaster k1 = new KeyMaster(1); 12. KeyMaster k2 = new KeyMaster(2); 13. set.add(k1); set.add(k1); 14. set.add(k2); set.add(k2); 15. System.out.print(set.size() + “:”);16. k2.i = 1; 17. System.out.print(set.size() + “:”);18. set.remove(k1); 19. System.out.print(set.size() + “:”);20. set.remove(k2); 21. System.out.print(set.size(); 22. 23. What is the result? A. 4:4:2:2 B. 4:4:3:2 C. 2:2:1:0 D. 2:2:0:0 E. 2:1:0:0 F. 2:2:1:1 G. 4:3:2:1答案: F考点: hashset,set,hashcode和equals方法的理解说明: 每个对象允许被存入hashset预先都会在hashset内部列表中留下key/value, 系统删除的时候也将解析要删除的对象的hashcode,到此表进行匹配删除。 这里要强调一点, 一旦一个对象被存入hashset, 这个对象后期发生任何改变,都不会导致hashset中的key/value表发生变化。Question 17Given: 1. import java.util.*; 2. public class Test 3. public static void main(String args) 4. List<String> strings = new ArrayList<String>();5. / insert code here 6. 7. Which four, inserted at line 5, will allow compilation to succeed? (Choose four.) A. String s = strings.get(0); B. Iterator i1 = strings.iterator();C. String array1 = strings.toArray();D. Iterator<String> i2 = strings.iterator();E. String array2 = strings.toArray(new String1); F. Iterator<String> i3 = strings.iterator<String>();答案:ABDE考点:ArrayList的API,泛型说明:普通的toArray()返回的是非泛型结果,是Object, 只有调用带数组参数的toArray方法,才能正确返回目标结果。由于strings本身已经是泛型的,所以其iterator()也支持泛型返回。Question 18Given:1. import java.util.*; 2. public class Old 3. public static Object get(List list) 4. return list.get(0); 5. 6. Which three will compile successfully? (Choose three.) A. Object o = Old.get(new LinkedList(); B. Object o = Old.get(new LinkedList<?>(); C. String s = Old.get(new LinkedList<String>(); D. Object o = Old.get(new LinkedList<Object>(); E. String s = (String)Old.get(new LinkedList<String>();答案:ADE考点:泛型,父子类引用变量指向问题说明:接口引用变量可以指向任何实现了该接口的对象。普通接口引用变量,可以指向泛型实现对象。Question 19Given: 11. public static void append(List list) list.add(”0042”); 12. public static void main(String args) 13. List<Integer> intList = new ArrayList<Integer>();14. append(intList); 15. System.out.println(intList.get(0); 16. What is the result? A. 42 B. 0042 C. An exception is thrown at runtime. D. Compilation fails because of an error in line 13. E. Compilation fails because of an error in line 14.答案:B考点:泛型的理解说明:泛型只是对集合类型存值的控制,本身集合类型还是可以存储各种类型的,如果一个集合类型被泛型引用变量指向,则呈现为录入值类型受控,但一旦用普通引用变量指向,又可以接收各种类型的数据,不受泛型限制。Question 20Given 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. Which three changes must be made to the method sum to use generics? (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<int> intList)” F. replace the method declaration with “sum(List<Integer> intList)”答案:ACF考点: 拆箱和装箱操作、for.each新版for循环,泛型的概念说明: 由于使用了泛型,该操作一步到位for (int i : intList), 这里不需要iterator的参与了,实际上在for.each循环被广泛使用之后,iterator的迭代循环的写法已经很少使用了,但for.each循环任