JAVA基础面试题-3(答案版).doc
《JAVA基础面试题-3(答案版).doc》由会员分享,可在线阅读,更多相关《JAVA基础面试题-3(答案版).doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、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 cod
2、e 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 关系一般来说表示某对象体内有其它对象的
3、存在,该对象体现为属性形态,在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
4、 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 ca
5、n 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 i
6、s-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.
7、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
8、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 co
9、de, 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:数据结构二叉排
10、序树,可以对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 pq = new PriorityQueue(); 5. pq.add(
11、”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的话,优先级队列中的元素默认按自然顺序排列,也就是数字
12、默认是小的在队列头,字符串则按字典序排列 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 hs = new HashSet();7. WrappedString ws1 =
13、 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.
14、 Compilation fails. G. An exception is thrown at runtime.答案:D考点:集合类型,hashset的理解,hashcode和equals调用契约说明: WrappedString类没有重写hashcode方法,那么这个类的两个对象在装入hashset的过程中,将会使用Object所提供的hashcode调用结果,由于Object的hashcode值均为不同,所以WrappedString两个对象全被hashset接收。 由于这里显式进行字符串构建,所以会创建两个独立的,有相同内容的字符串对象,但是由于String类已经完整重写了hashco
15、de和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.ad
16、d(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
17、 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 elem
18、ents from the coil collection, but the order is NOT guaranteed to be preserved.答案:D考点:枚举类型,集合类型(ArrayList, HashSet)Question 9Given: 1. public class Score implements Comparable 2. private int wins, losses; 3. public Score(int w, int 1) wins = w; losses = 1; 4. public int getWins() return wins; 5. pub
19、lic int getLosses() return losses; 6. public String toString() 7. return “”;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
20、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 imp
21、lementation 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.
22、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.equ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 基础 试题 答案
限制150内