Multiple choice technology programming languages

Given: 1. public class Threads4 { 2. public static void main (String[] args) { 3. new Threads4().go(); 4. } 5. public void go() { 6. Runnable r = new Runnable() { 7. public void run() { 8. System.out.print(”foo”); 9. } 10. }; 11. Thread t = new Thread(r); 12. t.start(); 13. t.start(); 14. } 15. } What is the result?

  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints ‘foo”.

  4. The code executes normally, but nothing is printed.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Calling start() twice on the same Thread object throws IllegalThreadStateException at runtime. Once a thread has been started, subsequent calls to start() cause an exception - you cannot restart a thread.