java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx

上传人:b****7 文档编号:11190008 上传时间:2023-02-25 格式:DOCX 页数:26 大小:20.82KB
下载 相关 举报
java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx_第1页
第1页 / 共26页
java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx_第2页
第2页 / 共26页
java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx_第3页
第3页 / 共26页
java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx_第4页
第4页 / 共26页
java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx

《java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx》由会员分享,可在线阅读,更多相关《java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx(26页珍藏版)》请在冰豆网上搜索。

java认证 习题 第06章 有答案版 OK该试题还有第0405章.docx

java认证习题第06章有答案版OK该试题还有第0405章

第6章练习题

一、String、StringBuffer、StringBuilder

1(4).Given:

classTKO{

publicstaticvoidmain(String[]args){

Strings="-";

Integerx=343;

longL343=343L;

if(x.equals(L343))s+=".e1";

if(x.equals(343))s+=".e2";

Shorts1=(short)((newShort((short)343))/(newShort((short)49)));

if(s1==7)s+="=s";

if(s1

System.out.println(s);

}}

WhichofthefollowingwillbeincludedintheoutputStrings?

(Chooseallthatapply.)

A..e1

B..e2

C.=s

D.fly

E.Noneoftheabove.

F.Compilationfails.

G.Anexceptionisthrownatruntime.

2(10).

aboutthethreejava.langclassesString,StringBuilder,andStringBufferaretrue?

(Chooseallthatapply.)

A.Allthreeclasseshavealength()method.

B.ObjectsoftypeStringBufferarethread-safe.

C.Allthreeclasseshaveoverloadedappend()methods.

D.The"+"isanoverloadedoperatorforallthreeclasses.

E.AccordingtotheAPI,StringBufferwillbefasterthanStringBuilderundermost

implementations.

F.Thevalueofaninstanceofanyofthesethreetypescanbemodifiedthroughvarious

methodsintheAPI.

3(13)Given:

classPolish{

publicstaticvoidmain(String[]args){

intx=4;

StringBuffersb=newStringBuffer("..fedcba");

sb.delete(3,6);

sb.insert(3,"az");

if(sb.length()>6)x=sb.indexOf("b");

sb.delete((x-3),(x-2));

System.out.println(sb);

Whatistheresult?

A..faza

B..fzba

C...azba

D..fazba

E...fezba

F.Compilationfails.

4(22)

whatStringinstancemethodwouldreturntruewheninvokedlikethis

a.method(b);

wherea="GROUNDhog"andb="groundHOG"

selectallanswers

A.equals()

B.toLowercase()

C.toUppercase()

D.equalsIgnoreCase()

E.Noneoftheabove

5(25)

Attheendofthesetwolinesofcode:

Strings="hypertext";

Stringt=s.subString(2,5);

Whatdoestheobjectreferencetcontain?

A."yper"

B."ype"

C."pert"

D."per"

E."perte"

6(26)

Whichofhefollowingstatementswouldreturnfalse?

ifgiventhefollowingstatements

Strings=newString("Newyear");

Strings1=newString("newYear");

A.s==s1

B.s.equals(s1)

C.s=s1;

D.noneoftheabove

7(28)

Whichwillcompileandrunwithoutexception,generatetheoutput4247

A.classExample{

publicstaticvoidmain(String[]args){

Strings="123456789";

s=(s-"123").replace(1,3,"24")-"89";

System.out.println(s);

}

}

B.classExample{

publicstaticvoidmain(String[]args){

StringBuffers=newStringBuffer("123456789");

s.delete(0,3).replace(1,3,"24").delete(4,6)

System.out.println(s);

}

}

C.classExample{

publicstaticvoidmain(String[]args){

StringBuffers=newStringBuffer("123456789");

s.substring(3,6).delete(1,3).insert(1,"24");

System.out.println(s);

}

}

D.classExample{

publicstaticvoidmain(String[]args){

StringBuilders=newStringBuilder("123456789");

s.substring(3,6).delete(1,2).insert(1,"24");

System.out.println(s);

}

}

8(29)

classExample{

publicstaticvoidmain(String[]args){

Strings="ABCD";

s.concat("E");

s.replace('C','F');

System.out.println(s);

}

}

whatwillbeprintedout?

A.CompilationErrorssaystheStringisimmutable

B.ABFDE

C.ABCDE

D.ABCD

9(33)

classExample{

publicstaticvoidmain(String[]args){

Stringstr="Welcome";

str.concat("Tojava");

System.out.println(str);

}

}

whatwillbeprintedout?

A.theStringisimmutable,CompilationErrorsatline3

B.theStringisimmutable,RunExceptionatline3

C.Prints"Welcome"

D.prints"WelcomeTojava"

10.(39)

classExample{

publicstaticvoidmain(String[]args){

Strings1=newString("amit");

Strings2=s1.replace('m','i');

s1.concat("Poddar");

System.out.println(s1);

System.out.println((s1+s2).charAt(5));

}

}

whatwillbeprintedout?

A.CompilationError

B.amitPoddaro

C.amitPoddari

D.amiti

二、I/O、串行化

1

(2)Given:

importjava.io.*;

classPlayer{

Player(){System.out.print("p");}

}

classCardPlayerextendsPlayerimplementsSerializable{

CardPlayer(){System.out.print("c");}

publicstaticvoidmain(String[]args){

CardPlayerc1=newCardPlayer();

try{

FileOutputStreamfos=newFileOutputStream("play.txt");

ObjectOutputStreamos=newObjectOutputStream(fos);

os.writeObject(c1);

os.close();

FileInputStreamfis=newFileInputStream("play.txt");

ObjectInputStreamis=newObjectInputStream(fis);

CardPlayerc2=(CardPlayer)is.readObject();

is.close();

}catch(Exceptionx){}

}

}

Whatistheresult?

A.pc

B.pcc

C.pcp

D.pcpc

E.Compilationfails.

F.Anexceptionisthrownatruntime.

2(3)

Given:

bwisareferencetoavalidBufferedWriter

Andthesnippet:

15.BufferedWriterb1=newBufferedWriter(newFile("f"));

16.BufferedWriterb2=newBufferedWriter(newFileWriter("f1"));

17.BufferedWriterb3=newBufferedWriter(newPrintWriter("f2"));

18.BufferedWriterb4=newBufferedWriter(newBufferedWriter(bw));

Whatistheresult?

A.Compilationsucceeds.

B.Compilationfailsdueonlytoanerroronline15.

C.Compilationfailsdueonlytoanerroronline16.

D.Compilationfailsdueonlytoanerroronline17.

E.Compilationfailsdueonlytoanerroronline18.

F.Compilationfailsduetoerrorsonmultiplelines.

3(6)

importjava.io.*;

classKeyboard{}

publicclassComputerimplementsSerializable{

privateKeyboardk=newKeyboard();

publicstaticvoidmain(String[]args){

Computerc=newComputer();

c.storeIt(c);

}

voidstoreIt(Computerc){

try{

ObjectOutputStreamos=newObjectOutputStream(

newFileOutputStream("myFile"));

os.writeObject(c);

os.close();

System.out.println("done");

}catch(Exceptionx){System.out.println("exc");}

}

}

Whatistheresult?

(Chooseallthatapply.)

A.exc

B.done

C.Compilationfails.

D.Exactlyoneobjectisserialized.

E.Exactlytwoobjectsareserialized.

4(7)

Giventhefewestfragmentspossible(andfillingthefewestslotspossible),completethecode

belowsothattheclassbuildsadirectorynamed"dir3"andcreatesafilenamed"file3"inside

"dir3".Noteyoucanuseeachfragmenteitherzerooronetimes.

Code:

importjava.io.______________

classMaker{

publicstaticvoidmain(String[]args){

_________________________________

_________________________________

_________________________________

_________________________________

_________________________________

_________________________________

_________________________________

}}

Fragments:

File;FileDescriptor;FileWriter;Directory;

try{.createNewDir();FiledirFile

{}(Exceptionx)("dir3");file

file.createNewFile();=newFile=newFile

dir(dir,"file3");(dir,file);.createFile();

}catch("dir3","file3");.mkdir();Filefile

Answer:

importjava.io.File;

classMaker{

publicstaticvoidmain(String[]args){

try{

Filedir=newFile("dir3");

dir.mkdir();

Filefile=newFile(dir,"file3");

file.createNewFile();

}catch(Exceptionx){}

}}

5(12)

Given:

importjava.io.*;

classDirectories{

staticString[]dirs={"dir1","dir2"};

publicstaticvoidmain(String[]args){

for(Stringd:

dirs){

//insertcode1here

Filefile=newFile(path,args[0]);

//insertcode2here

}

}

}

andthattheinvocation

javaDirectoriesfile2.txt

isissuedfromadirectorythathastwosubdirectories,"dir1"and"dir1",andthat"dir1"hasa

file"file1.txt"and"dir2"hasafile"file2.txt",andtheoutputis"falsetrue",which

set(s)ofcodefragmentsmustbeinserted?

(Chooseallthatapply.)

A.Stringpath=d;

System.out.print(file.exists()+"");

B.Stringpath=d;

System.out.print(file.isFile()+"");

C.Stringpath=File.separator+d;

System.out.print(file.exists()+"");

D.Stringpath=File.separator+d;

System.out.print(file.isFile()+"");

6(15)

Given:

importjava.io.*;

publicclassTestSer{

publicstaticvoidmain(String[]args){

SpecialSerials=newSpecialSerial();

try{

ObjectOutputStreamos=newObjectOutputStream(

newFileOutputStream("myFile"));

os.writeObject(s);os.close();

System.out.print(++s.z+"");

ObjectInputStreamis=newObjectInputStream(

newFileInputStream("myFile"));

SpecialSerials2=(SpecialSerial)is.readObject();

is.close();

System.out.println(s2.y+""+s2.z);

}catch(Exceptionx){System.out.println("exc");}

classSpecialSerialimplementsSerializable{

transientinty=7;

staticintz=9;

Whicharetrue?

(Chooseallthatapply.)

A.Compilationfails.

B.Theoutputis1009

C.Theoutputis10010

D.Theoutputis1079

E.Theoutputis10710

F.InordertoalterthestandarddeserializationprocessyouwouldoverridethereadObject()

methodinSpecialSerial.

G.Inordertoalterthestandarddeserializationprocessyouwouldoverridethe

defaultReadObject()methodinSpecialSerial.

7(3)Whatisresultwhenyoucompileandrunthefollowingcode?

importjava.io.*;

publicclassExample

{publicstaticvoidmain(String[]args){

Filef=newFile("hello.test");

try{

FileOutputStreamout=newFileOutputStream(f);

}

catch(FileNotFoundExceptione){}

}

}

A.Createanewfilenamed"hello.test"ifitdoesnotyetexit.Italsoopensthefilesoyoucanwritetoitandreadfromit

B.Createanewfilenamed"hello.test"ifitdoesnotyetexit.Thefileisnotopened

C.openafilenamed"hello.test"sothatyoucanwritetoitandreadfromit,butdoesnotcreatethefileifitdoesnotyetexit

D.openafilenamed"hello.test"sothatyoucanwritetoitbutcannotreadfromit

8(4)Whatisresultwhenyoucompileandrunthefollowingcode?

importjava.io.*;

publicclassExample

{publicstaticvoidmain(String[]args){

Filef=newFile("c:

\\large.txt"

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

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

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