Java Thread, How does it turn out, answer A?

Question: http://www.javacertifications.net/javacert/scjp1.6Mock.jsp

No questions -20

What is the result for the code below?

public class Test extends Thread
{   
    static String sName = "good";
    public static void main(String argv[])
    {
        Test t = new Test();
        t.nameTest(sName);
        System.out.println(sName);    
    }
    public void nameTest(String sName)
    {
        sName = sName + " idea ";
        start();
    }
    public void run()
    {
        for(int i=0;i  <  4; i++)
        {
            sName = sName + " " + i;

        }
    }
}

Parameters Good B) good idea C) good idea good idea Correct answer: A

Explanations: changing a value in local methods would not have changed globally in the case of String (since the String object is immutable).

+3
source share
8 answers

None of the answers are correct, and there is no single correct answer.

The question is very bad because it mixes two completely separate issues:

  • sName nameTest() , .
  • nameTest() , run(), main() , . : , - :
    • good 0
    • good 0 1
    • good 0 1 2
    • good 0 1 2 3
+8

, nameTest String sName, static . ,

sName = sName + " idea ";

static. , , .. Test.sName.

run static sName , , , - "good 0 1 2 3". , , () t, sName , . , , , ( "" ). " 0", " 0 1" .. "". A - .

+3

:

public void nameTest(String sName){
        sName = sName + " idea ";
         start();
}

sName sName, Test.sName.

, run() , ("good")

+1

nameTest sName . "".

, , Test.sName

+1

nameTest. , .

Java, , , - "". , Test.sName.

, "" sName. , "", - A, 1 - .

0

'good' nameTest (sName), , , , nameTest . , nameTest. sName sName. , .

0

COPY ( arg). COPY, . good, good 0, good 0 1 ..

0

good 0 1 2 3

( java... ).

, :

nameTest sName String.

, , "" String - " " - sName. sName ( , ) "".

start(), run().

run() , sName.

, run() , sName , .

0

Source: https://habr.com/ru/post/1766923/


All Articles