niusouti.com

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.prin

题目

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?()


相似考题
更多“publicclassSyncTest{publicstaticvoidmain(S ”相关问题
  • 第1题:

    publicclassX{publicstaticvoidmain(Stringargs){strings=newstring(Hello”);modify(s);System.out.printIn(s);}publicstaticvoidmodify(Strings){s+=world!”;}}Whatistheresult?()

    A.Theprogramrunsandprints“Hello”

    B.Anerrorcausescompilationtofail.

    C.Theprogramrunsandprints“Helloworld!”

    D.Theprogramrunsbutabortswithanexception.


    参考答案:A

  • 第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


    正确答案:A
    解析:String类的replace (char oldChar,char newChar)函数的作用是返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar而生成的。返回的是新字符串,但是原字符串变量的值并未发生改变。因此,输出的是“hello”而不是“hemmo”。如果替换语句换为: s=s.replace('l','m');,则输出“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


    2,2,3

  • 第4题:

    publicclassfoo{publicstaticvoidmain(Stringargs){Strings;system.out.printIn(s=”+s);}}Whatistheresult?()

    A.Thecodecompilesand“s=”isprinted.

    B.Thecodecompilesand“s=null”isprinted.

    C.Thecodedoesnotcompilebecausestringsisnotinitialized.

    D.Thecodedoesnotcompilebecausestringscannotbereferenced.

    E.Thecodecompiles,butaNullPointerExceptionisthrownwhentoStringiscalled.


    参考答案:C

  • 第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


    编译错误