Given: 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.


Correct Option: C

AI Explanation

To answer this question, we need to understand the behavior of the code provided.

In the given code, a new thread is created using an anonymous inner class that implements the Runnable interface. The run() method in the inner class is overridden to print "bar". The start() method is then called on the newly created thread to start its execution.

When a thread is started using the start() method, it executes independently in parallel with the main thread. In this case, the newly created thread will execute the run() method and print "bar".

Therefore, the correct answer is:

C) The code executes normally and prints "bar".

Find more quizzes: