Human interference is a major factor in the decline of wild Venus Flytraps.
A.真
B.假
C.NOT GIVEN
第1题
publicstaticvoidmain(String[]args)throwsException{
Recordr1=newRecord();
Recordr2=newRecord();
doTransfer(r1,r2,5);
doTransfer(r2,r1,2);
doTransfer(r1,r2,1);
//printtheresult
System.out.println(”rl=+r1.get()+,r2=”+r2.get());
}
privatestaticvoiddoTransfer(
finalRecorda,finalRecordb,finalintamount){
Threadt=newThread(){
publicvoidrun(){
newClerk().transfer(a,b,amount);
}
};
t.start();
}
}
classClerk{
publicsynchronizedvoidtransfer(Recorda,Recordb,intamount){
synchronized(a){
synchronized(b){
a.add(-amount);
b.add(amount);
}
}
}
}
classRecord{
intnum=10;
publicintget(){returnnum;}
publicvoidadd(intn){num=num+n;}
}
IfTransfers.main()isrun,whichthreearetrue?()
第2题
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Synchronizingtherun()methodwouldmaketheclassthread-safe.
D.Thedatainvariable“x”areprotectedfromconcurrentaccessproblems.
E.DeclaringthedoThings()methodasstaticwouldmaketheclassthread-safe.
F.WrappingthestatementswithindoThings()inasynchronized(newObject()){}blockwouldmaketheclassthread-safe.
第3题
A.privatesynchronizedObjecto;
B.voidgo(){synchronized(){/*codehere*/}}
C.publicsynchronizedvoidgo(){/*codehere*/}
D.privatesynchronized(this)voidgo(){/*codehere*/}
E.voidgo(){synchronized(Object.class){/*codehere*/}}
F.voidgo(){Objecto=newObject();synchronized(o){/*codehere*/}}
第4题
A.Movetheline12printstatementintothefoo()method.
B.Changeline7topublicsynchronizedvoidgo(){.
C.Changethevariabledeclarationonline3toprivatevolatileintx;.
D.Wrapthecodeinsidethefoo()methodwithasynchronized(this)block.
E.Wraptheforloopcodeinsidethego()methodwithasynchronizedblocksynchronized(this){//forloopcodehere}.
第5题
publicclassNameList{
privateListnames=newArrayList();
publicsynchronizedvoidadd(Stringname){names.add(name);}
publicsynchronizedvoidprintAll(){
for(inti=0;iSystem.out.print(names.get(i)+);
}
}
publicstaticvoidmain(String[]args){
finalNameListsl=newNameList();
for(inti=0;i<2;i++){
newThread(){
publicvoidruin(){
sl.add(”A”);
sl.add(”B”);
sl.add(”C”);
sl.printAll();
}
}.start();
}
}
}
Whichtwostatementsaretrueifthisclassiscompiledandrun?()
第6题
A.0,2,4,4,6,8,10,6,
B.0,2,4,6,8,10,2,4,
C.0,2,4,6,8,10,12,14,
D.0,0,2,2,4,4,6,6,8,8,10,10,12,12,14,14,
E.0,2,4,6,8,10,12,14,0,2,4,6,8,10,12,14,
第7题
A.declarereset()usingthesynchronizedkeyword
B.declaregetName()usingthesynchronizedkeyword
C.declaregetCount()usingthesynchronizedkeyword
D.declaretheconstructorusingthesynchronizedkeyword
E.declareincrement()usingthesynchronizedkeyword
第8题
privatestaticObjectresource=newObject();
privatestaticvoiddelay(longn){
try{Thread.sleep(n);}
catch(Exceptione){System.out.print(”Error);}
}
publicstaticvoidmain(String[]args){
System.out.print(”StartMain);
newThread1().start();
delay(1000);
Threadt2=newThread2();
t2.start();
delay(1000);
t2.interrupt
delay(1000);
System.out.print(”EndMain);
}
staticclassThread1extendsThread{
publicvoidrun(){
synchronized(resource){
System.out.print(”Startl);
delay(6000);
System.out.print(”End1);
}
}
}
staticclassThread2extendsThread{
publicvoidrun(){
synchronized(resource){
System.out.print(”Start2);
delay(2000);
System.out.print(”End2);
}
}
}
}
Assumethatsleep(n)executesinexactlymmilliseconds,andallothercodeexecutesinaninsignificantamountoftime.Whatistheoutputifthemain()methodisrun?()
第9题
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesandprints“StartedComplete”.
D.Thecodeexecutesandprints“StartedComplete0123”.
E.Thecodeexecutesandprints“Started0l23Complete”.
第10题
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesnormallyandprints“sleep”.
D.Thecodeexecutesnormally,butnothingisprinted.
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!