Question 226 / 297:  What are the possible results of compiling and running the following code?
Choose 4
public class Test {

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

Runnable t1 = new Runnable() {

public void run() {

try {

System.out.print("t1before");

Thread.sleep(100);

System.out.print("t1after");

} catch (InterruptedException e) {

}

}

};

final Thread t2 = new Thread() {

public void run() {

try {

System.out.print("t2before");

wait();

System.out.print("t2after");

} catch (InterruptedException e) {

}

}

};

t2.start();

new Thread(t1).start();

}

}

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

A thread calling wait() on an object (in this case "this"), which the thread doesn't own its lock causes an IllegalMonitorStateException

Therefore "t2after" cannot be part of the output.

To avoid this exception surround wait() with

synchronized (this) {

wait();

}

There is nothing that can cause a NumberFormatException to be thrown

Exam Home Page
Ask
Michael Sag
Start Exam
Yasser Ibrahim
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>