Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/running-a-thread-in-java
Question 180 / 297:  What is the could be the output of compiling and running the following code?
Choose 2
public class Test extends Thread {

static int count = 0;

public static void main(String argv[]) throws InterruptedException {

Test t = new Test ();

t.increment(count);

t.start();

Thread.sleep(1000);

System.out.println(count);

}

public void increment(int count) {

++count;

}

public void run() {

count = count + 5;

}

}

<< First < Previous Next > Last >>
Explanation:

The trick is that inside method increment(), what is incremented is not the static member count, rather the method-argument count, so the static member count stays 0 and then inside run incremented by 5 and prints 5 to the screen, unless Thread.sleep(1000) throws an InterruptedException and this is possible too.

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Sarah Warren
Start Test
Jugnu Khan
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>