Module 7 集合和泛型.docx

上传人:b****7 文档编号:26538011 上传时间:2023-06-20 格式:DOCX 页数:17 大小:20KB
下载 相关 举报
Module 7 集合和泛型.docx_第1页
第1页 / 共17页
Module 7 集合和泛型.docx_第2页
第2页 / 共17页
Module 7 集合和泛型.docx_第3页
第3页 / 共17页
Module 7 集合和泛型.docx_第4页
第4页 / 共17页
Module 7 集合和泛型.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

Module 7 集合和泛型.docx

《Module 7 集合和泛型.docx》由会员分享,可在线阅读,更多相关《Module 7 集合和泛型.docx(17页珍藏版)》请在冰豆网上搜索。

Module 7 集合和泛型.docx

Module7集合和泛型

CopyrightTarenaCorporation,2008.Allrightsreserved

Module7-集合和泛型

一、选择题:

Question1

Given:

11.publicclassPerson{

12.privatename;

13.publicPerson(Stringname){

14.this.name=name;

15.}

16.publicinthashCode(){

17.return420;

18.}

19.}

Whichistrue?

A.ThetimetofindthevaluefromHashMapwithaPersonkeydepends

onthesizeofthemap.

B.DeletingaPersonkeyfromaHashMapwilldeleteallmapentriesfor

allkeysoftypePerson.

C.InsertingasecondPersonobjectintoaHashSetwillcausethefirst

Personobjecttoberemovedasaduplicate.

D.ThetimetodeterminewhetheraPersonobjectiscontainedina

HashSetisconstantanddoesNOTdependonthesizeofthemap.

Answer:

A

Question2

Given:

11.publicstaticCollectionget(){

12.Collectionsorted=newLinkedList();

13.sorted.add("B");sorted.add("C");sorted.add("A");

14.returnsorted;

15.}

16.publicstaticvoidmain(String[]args){

17.for(Objectobj:

get()){

18.System.out.print(obj+",");

19.}

20.}

Whatistheresult?

A.A,B,C,

B.B,C,A,

C.Compilationfails.

D.Thecoderunswithnooutput.

E.Anexceptionisthrownatruntime.

Answer:

BCopyrightTarenaCorporation,2008.Allrightsreserved

Question3

Given:

1.importjava.util.*;

2.publicclassExample{

3.publicstaticvoidmain(String[]args){

4.//insertcodehere

5.set.add(newinteger

(2));

6.set.add(newinteger(l));

7.System.out.println(set);

8.}

9.}

Whichcode,insertedatline4,guaranteesthatthisprogramwill

output[1,2]?

A.Setset=newTreeSet();

B.Setset=newHashSet();

C.Setset=newSortedSet();

D.Listset=newSortedList();

E.Setset=newLinkedHashSet();

Answer:

A

Question4

Given:

1.importjava.util.*;

2.publicclassPQ{

3.publicstaticvoidmain(String[]args){

4.PriorityQueuepq=newPriorityQueue();

5.pq.add("carrot");

6.pq.add("apple");

7.pq.add("banana");

8.System.out.println(pq.poll()+":

"+pq.peek());

9.}

10.}

Whatistheresult?

A.apple:

apple

B.carrot:

apple

C.apple:

banana

D.banana:

apple

E.carrot:

carrot

F.carrot:

banana

Answer:

C

Question5

Given:

1.importjava.util.*;CopyrightTarenaCorporation,2008.Allrightsreserved

2.publicclassWrappedString{

3.privateStrings;

4.publicWrappedString(Strings){this.s=s;}

5.publicstaticvoidmain(String[]args){

6.HashSeths=newHashSet();

7.WrappedStringws1=newWrappedString("aardvark");

8.WrappedStringws2=newWrappedString("aardvark");

9.Strings1=newString("aardvark");

10.Strings2=newString("aardvark");

11.hs.add(ws1);hs.add(ws2);hs.add(s1);hs.add(s2);

12.System.out.println(hs.size());}}

Whatistheresult?

A.0

B.1

C.2

D.3

E.4

F.Compilationfails.

G.Anexceptionisthrownatruntime.

Answer:

D

Question6

ClicktheExhibitbutton.

1.importjava.util.*;

2.publicclassTestSet{

3.enumExample{ONE,TWO,THREE}

4.publicstaticvoidmain(String[]args){

5.Collectioncoll=newArrayList();

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.Setset=newHashSet(coll);

13.}

14.}

Whichstatementistrueaboutthesetvariableonline12?

A.Thesetvariablecontainsallsixelementsfromthecollcollection,

andtheorderisguaranteedtobepreserved.

B.Thesetvariablecontainsonlythreeelementsfromthecoll

collection,andtheorderisguaranteedtobepreserved.

C.Thesetvariablecontainsallsixelementsfromthecoilcollection,

buttheorderisNOTguaranteedtobepreserved.

D.ThesetvariablecontainsonlythreeelementsfromthecoilCopyrightTarenaCorporation,2008.Allrightsreserved

collection,buttheorderisNOTguaranteedtobepreserved.

Answer:

D

Question7

Aprogrammerhasanalgorithmthatrequiresajava.util.Listthat

providesanefficientimplementationofadd(0,object),butdoes

NOTneedtosupportquickrandomaccess.Whatsupportsthese

requirements?

A.java.util.Queue

B.java.util.ArrayList

C.java.util.LinearList

D.java.util.LinkedList

Answer:

D

Question8

ClicktheExhibitbutton.

1.importjava.util.*;

2.classKeyMaster{

3.publicinti;

4.publicKeyMaster(inti){this.i=i;}

5.publicbooleanequals(Objecto){returni==((KeyMaster)o).i;}

6.publicinthashCode(){returni;}

7.}

8.publicclassMapIt{

9.publicstaticvoidmain(String[]args){

10.Setset=newHashSet();

11.KeyMasterk1=newKeyMaster

(1);

12.KeyMasterk2=newKeyMaster

(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.}

Whatistheresult?

A.4:

4:

2:

2

B.4:

4:

3:

2

C.2:

2:

1:

0

D.2:

2:

0:

0CopyrightTarenaCorporation,2008.Allrightsreserved

E.2:

1:

0:

0

F.2:

2:

1:

1

G.4:

3:

2:

1

Answer:

F

Question9

Given:

11.publicstaticvoidappend(Listlist){list.add("0042");}

12.publicstaticvoidmain(String[]args){

13.ListintList=newArrayList();

14.append(intList);

15.System.out.println(intList.get(0));

16.}

‘Whatistheresult?

A.42

B.0042

C.Anexceptionisthrownatruntime.

D.Compilationfailsbecauseofanerrorinline13.

E.Compilationfailsbecauseofanerrorinline14.

Answer:

B

Question10

Given:

int[]myArray=newint[]{1,2,3,4,5};

Whatallowsyoutocreatealistfromthisarray?

A.ListmyList=myArray.asList();

B.ListmyList=Arrays.asList(myArray);

C.ListmyList=newArrayList(myArray);

D.ListmyList=Collections.fromArray(myArray);

Answer:

B

Question11

Given:

34.HashMapprops=newHashMap();

35.props.put("key45","somevalue");

36.props.put("key12","someothervalue");

37.props.put("key39","yetanothervalue");

38.Sets=props.keySet();

39.//insertcodehere

What,insertedatline39,willsortthekeysinthepropsHashMap?

A.Arrays.sort(s);

B.s=newTreeSet(s);

C.Collections.sort(s);CopyrightTarenaCorporation,2008.Allrightsreserved

D.s=newSortedSet(s);

Answer:

B

Question12

Given:

11.publicclassPerson{

12.privateStringname,comment;

13.privateintage;

14.publicPerson(Stringn,inta,Stringc){

15.name=n;age=a;comment=c;

16.}

17.publicbooleanequals(Objecto){

18.if(!

(oinstanceofPerson))returnfalse;

19,Personp=(Person)o;

20.returnage==p.age&&name.equals(p.name);

21.}

22.}

WhatistheappropriatedefinitionofthehashCodemethodinclass

Person?

A.returnsuper.hashCode();

B.returnname.hashCode()+age*7;

C.returnname.hashCode()+comment.hashCode()/2;

D.returnname.hashCode()+comment.hashCode()/2-age*3;

Answer:

B

Question13

Given:

11.publicclassKey{

12.privatelongid1;

13.privatelong1d2;

14.

15.//classKeymethods

16.}

AprogrammerisdevelopingaclassKey,thatwillbeusedasakeyin

astandardjava.util.HashMap.Whichtwomethodsshouldbe

overriddentoassurethatKeyworkscorrectlyasakey?

(Choosetwo.)

A.publicinthashCode()

B.publicbooleanequals(Keyk)

C.publicintcompareTo(Objecto)

D.publicbooleanequals(Objecto)

E.publicbooleancompareTo(Keyk)

Answer:

ADCopyrightTarenaCorporation,2008.Allrightsreserved

Question14

Given:

11.publicclassPerson{

12.privatename;

13.publicPerson(Stringname){

14.this.name=name;

15.}

16.publicbooleanequals(Objecto){

17.if(!

oinstanceofPerson)returnfalse;

18.Personp=(Person)o;

19.returnp.name.equals(this.name);

20.}

21.}

Whichistrue?

A.CompilationfailsbecausethehashCodemethodisnotoverridden.

B.AHashSetcouldcontainmultiplePersonobjectswiththesame

name.

C.AllPersonobjectswillhavethesamehashcodebecausethe

hashCodemethodisnotoverridden.

D.IfaHashSetcontainsmorethanonePersonobjectwith

name="Fred",thenremovinganotherPerson,alsowithname="Fred",

willremovethemall.

Answer:

B

Question15

Given:

1.publicclassPerson{

2.privateStringname;

3.publicPerson(Stringname){this.name=name;}

4.publicbooleanequals(Personp){

5.returnp.name.equals(this.name);

6.}

7.}

Whichistrue?

A.TheequalsmethoddoesNOTproperlyoverridetheObject.equals

method.

B.Compilationfailsbecausetheprivateattributep.namecannotbe

accessedinline5.

C.Toworkcorrectlywithhash-baseddatastructures,thisclassmust

alsoimplementthehashCodemethod.

D.WhenaddingPersonobjectstoajava.util.Setcollection,theequals

methodinline4willpreventduplicates.

Answer:

ACopyrightTarenaCorporation,2008.Allrightsreserved

Question16

WhichtwostatementsaretrueaboutthehashCodemethod?

(Choose

two.)

A.ThehashCodemethodforagivenclasscanbeusedtotestfor

objectequalityandobjectinequalityforthatclass.

B.ThehashCodemethodisusedbythejava.util.SortedSetcollection

classtoordertheelementswithinthatset.

C.ThehashCodemethodforagivenclasscanbeusedtotestfor

objectinequality,butNOTobjectequality,forthatclass.

D.Theonlyimportantcharacteristicofthevaluesreturnedbya

hashCodemethodisthatthedistributionofvaluesmustfollowa

Gaussiandistribution.

E.ThehashCodemethodisusedbythejava.util.HashSetcollection

classtogrouptheelementswithinthatsetintohashbucketsfor

swiftretrieval.

Answer:

CE

Question17

Given:

enumExample{ONE,TWO,THREE}

Whichistrue?

A.Theexpressions(ONE==ONE)andONE.equals(ONE)areboth

展开阅读全文
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1