Multiple choice technology programming languages

  1. public class Threads5 { 2. public static void main (String[] args) { 3. new Thread(new Runnable() { 4. public void run() { 5. System.out.print("bar"); 6. }}).start(); 7. } 8. } What is the result?

  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints "bar".

  4. The code executes normally, but nothing prints.

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

This code creates an anonymous inner class implementing Runnable, then starts a Thread with it. The code compiles successfully because: 1) The anonymous inner class syntax is correct: new Runnable() { public void run() {...} } 2) The Runnable interface is properly implemented with the run() method 3) The Thread constructor accepts a Runnable 4) The start() method is called correctly on the Thread. When the thread runs, it executes the run() method, which prints 'bar' to the console.