publicclassSyncTest{
publicstaticvoidmain(Stringargs){
finalStringBuffers1=newStringBuffer();
finalStringBuffers2=newStringBuffer();
newThread(){
publicvoidrun(){
synchronized(s1){
s2.append(A”);
synchronized(s2){
s2.append(B”);
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
newThread(){
publicvoidrun(){
synchronized(s2){
s2.append(C”);
synchronized(s1){
s1.append(D”);
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
Whichtwostatementsaretrue?()
第1题:
A.Theprogramrunsandprints“Hello”
B.Anerrorcausescompilationtofail.
C.Theprogramrunsandprints“Helloworld!”
D.Theprogramrunsbutabortswithanexception.
第2题:
下列程序的输出结果是( )。 public class Test { public static void main (String[] args) { String s="hello"; s.replace ('r','m'); System.out.println(s); } }
A.hello
B.HELLO
C.hemmo
D.HEMMO
第3题:
写出以下程序的运行结果? public class Test{ public static void main(String argv[]){ String s = new String("hello"); myMethod(s); System.out.print("s=" + s); } public static void myMethod(String s){ s = s + "!"; } }
A.s=hello!
B.s=hello
C.hello!
D.hello
第4题:
A.Thecodecompilesand“s=”isprinted.
B.Thecodecompilesand“s=null”isprinted.
C.Thecodedoesnotcompilebecausestringsisnotinitialized.
D.Thecodedoesnotcompilebecausestringscannotbereferenced.
E.Thecodecompiles,butaNullPointerExceptionisthrownwhentoStringiscalled.
第5题:
1、写出以下程序的运行结果? public class Test{ public static void main(String argv[]){ String s = new String("hello"); myMethod(s); System.out.print("s=" + s); } public static void myMethod(String s){ s = s + "!"; } }
A.s=hello!
B.s=hello
C.hello!
D.hello