-duet
JAVA
语言基础笔试题
-3
Question 1
Given:
1. public class Team extends List {
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
,
但并非显式以属性形式存在。
由于<
/p>
LinkedList
自带
remove
方法,可以通过
(p)
来删除
player
对象。
Question 2
Which
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 3
Which 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 4
Given:
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 5
Given:
1. import .*;
2. public class Example {
3. public static void main(String[]
args) {
4. // insert code
here
5. (new Integer(2));
6. (new Integer(l));
7. n(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
考点:集合类型的概念,<
/p>
TreeSet
的特性
说明:
HashSet:
底层数据结构是
hash
表,先判断
hash
值,然后比较值
TreeSet:
数据结构二叉排序树,可以对
set
集合中的元素进行排序,自定义类需要实现
comparable
接口(排序时,当主要条件相同时,一定要判断次要条件)
此题从
输出角度来看,
属于天然顺序输出,
目前只有
< br>TreeSet
能达到该效果,
同时
Integer
类也实现了
Comparable
接口,所以可以安全的在
TreeSet
中使用
。
Question 6
Given:
1. import
.*;
2. public class PQ {
3. public static void
main(String[] args) {
4.
PriorityQueue
5. (”carrot”);
6. (”apple”);
7.
(”banana”);
8.
n(() +”:” + ());
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
考点:集合类型,了解
p>
Queue
接口的使用
说明:
优先级队列,如果不提供
p>
Comparable
的话,优先级队列中的元素默认按自然顺序排
列,也就
是数字默认是小的在队列头,字符串则按字典序排列
Poll()
从队列头部取值,该值
从队列中删除
; peek()
并未实际取值,而仅是取得队列
头部
的元素。
Question 7
Given:
1. import .*;
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
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.
(ws1); (ws2); (s1); (s2);
12. n(()); } }
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
调用契约
说明:
Wra
ppedString
类没有重写
hashcode
方法,那么这个类的两个对象在装入
hashset
的
过程中,
将会使用
Object<
/p>
所提供的
hashcode
调用结果,<
/p>
由于
Object
的
hashcode
值均为不同,
所以
WrappedString
两个对象全被
hashset<
/p>
接收。
由于这里显式进行字符串构建,
所以会创
建两个独立的,有相同内容的字符串对象,但是由于
S
tring
类已经完整重写了
hashcode
和
equals
方法,所以具有相同内容的两个对象,
将无法同时保存入
hashset.
Question 8
Click the Exhibit
button.
1. import .*;
2. public class
TestSet {
3. enum Example {
ONE, TWO, THREE }
4. public
static void main(String[] args) {
5. Collection coll = new ArrayList();
6. ();
7. ();
8. ();
9. ();
10. ();
11. ();
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 9
Given:
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. public int getLosses()
{ return losses; }
6.
public String toString() {
7. r
eturn “<“ + 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
接口的应用理解
< br>说明:任何实现了
Comparable
接口的类对象均
可放置在
TreeSet
中使用,该接口只有一个方
法,
叫做
CompareTo,
同时该接口是泛型的,
所以参数数据类型不应该是
Ob
ject
,
而是
Score
类型。
Question 10
A programmer has
an algorithm that requires a that
provides an efficient implementation of
add(0,object), but does
NOT
need to support quick random access. What supports
these
requirements?
A.
B. ist
C. List
D. List
答案:
D
考点:
List
接口常见实现类的理解
说明:
LinkedList
使用链表来进行对象的保存,虽然消耗内存空间较多,但是提供较高的插入
和删除的性能。
Question
11
Given:
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 == && ();
21. }
22. }
What is the appropriate
definition of the hashCode method in class
Person?
A. return de();
B. return
de() + age * 7;
C. return
de() + de() /2;
D. return
de() + de() / 2 - age * 3;
答案:
B
考点:
Hashcode
和
equals
的实现契约
说明:
Hashcode
不同,
equals
一定不同;
Hashcode
相同,
equals
可以不同。
以上为
h
ashcode
和
equals
的实现
契约,这两个方法重写好了,该对象就可以存入
hashset
中,同时也可以做
hashMap
的
k
ey
。
选项
A
:在
age,name
都相同的情况下,
h
ashcode
仍然是不同的,是错误的。
选项<
/p>
CD
:因为
comment
并未参与
equals
判断,确参与了
hashcode
判断,显然无法满
足的契约的需求。
Question 12
Given:
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 p. 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
的特
点的理解
说明:
因为
Ha
shMap
中的所有的
key
是使用<
/p>
Hashset
来管理的,所以其内部所有的
key
对象均
必须能够支持在
Ha
shSet
环境下正常工作,所以实现
hashcode
和
equals
方法成为了必
然。
这里要注意一点,
equals
p>
方法不是泛型的,其参数数据类型为
Object.
Question 13
Given:
11.
public class Person {
12.
private name;
13. public
Person(String name) {
14.
= name;
15. }
16. public boolean equals(Object o) {
17. if( !o instanceof
Person ) return false;
18.
Person p = (Person) o;
19.
return ();
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
方法,将直接使用父类
Objec
t
的
HashCode
方法,而
Object
的该方法,不同的对象均为不同的值。用对象匹配删除的
时候,无论这个对象的属性写
的多么雷同,结果都删除不掉或者仅仅删除
hashcode
值匹配的那一个对象。
Question 14
Given:
1. public
class Person {
2. private
String name;
3. public
Person(String name) { = name; }
4. public boolean equals(Person p) {
-duet
-duet
-duet
-duet
-duet
-duet
-duet
-duet
-
上一篇:新理念大学英语5.0学习大厅答案
下一篇:印刷术语