Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/how-to-synchronize-a-method-in-java
Question 218 / 297:  What is the possible result of compiling and running the following code?
class Swimmer implements Runnable {

String name;

Pool pool;

Swimmer(String name, Pool pool) {

this.name = name;

this.pool = pool;

}

public void run() {

pool.swimIn(name);

}

}

public class Pool {

public void swimIn(String name) {

synchronized {

System.out.print(name);

System.out.print(name);

}

}

public static void main(String[] args) {

Pool pool = new Pool();

new Thread(new Swimmer("Tom", pool)).start();

new Thread(new Swimmer("Hanks", pool)).start();

}

}

A  TomTomHanksHanks
B  HanksHanksTomTom
C  HanksTomHanksTom
D  undetermined order
E  compilation error
<< First < Previous Next > Last >>
Explanation:

Compilation error : synchronized block should be defined on an object for example this.

synchronized(this) {

System.out.print(name);

System.out.print(name);

}

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

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Michael Sag
Start Exam
Heather McAvoy
Start Quiz
Hannah Sheth
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>