What will happen when you attempt to compile and run the following code? public class Bground extends Thread{ public static void main(String argv[]){ Bground b = new Bground(); b.run(); } public void start(){ for (int i = 0; i <10; i++){ System.out.println("Value of i = " + i); } } }

  1. A compile time error indicating that no run method is defined for the Thread class.

  2. A run time error indicating that no run method is defined for the Thread class.

  3. Clean compile and at run time the values 0 to 9 are printed out.

  4. Clean compile but no output at runtime.


Correct Option: D
Explanation:

To understand what will happen when you attempt to compile and run the given code, the user needs to know the concept of multithreading in Java.

The code defines a class Bground that extends the Thread class and overrides its start() method. In the main method, an instance of Bground is created and its run() method is called.

Now, let's go through each option and explain why it is right or wrong:

A. A compile time error indicating that no run method is defined for the Thread class. This option is incorrect because the Thread class already has a run() method defined, and the Bground class inherits this method from Thread. Therefore, there will be no compile time error.

B. A run time error indicating that no run method is defined for the Thread class. This option is incorrect because the Bground class has a run() method defined that overrides the run() method of the Thread class. Therefore, there will be no run time error.

C. Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect because the run() method of the Bground class is not called with the start() method. Instead, the run() method is called directly in the main method. Therefore, only the start() method of the Bground class is executed, which does not print any output.

D. Clean compile but no output at runtime. This option is correct. The code will compile without any errors and there will be no output at runtime because only the start() method of the Bground class is executed, which does not print any output.

Therefore, the correct answer is: D.

Find more quizzes: