Tag: programming languages

Questions Related to programming languages

  1. An exception is thrown if you attempt to add an element with a duplicate value.

  2. The add method returns false if you attempt to add an element with a duplicate value.

  3. A set may contain elements that return duplicate values from a call to the equals method.

  4. Duplicate values will cause an error at compile time.


Correct Option: B

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

AI Explanation

To answer this question, let's go through each option:

Option A) A compile time error indicating that no run method is defined for the Thread class. This option is incorrect. The code defines a run method in the Bground class, which extends the Thread class. Therefore, there is a run method defined for the Thread class.

Option B) A run time error indicating that no run method is defined for the Thread class. This option is also incorrect. As mentioned earlier, the code defines a run method in the Bground class, which extends the Thread class. Therefore, there is a run method defined for the Thread class.

Option C) Clean compile and at run time the values 0 to 9 are printed out. This option is incorrect. Although the code defines a start method in the Bground class, it doesn't actually start a new thread by calling the start method. Instead, it directly calls the run method on the Bground object. This means that the code will execute the for loop and print the values 0 to 9, but it will do so in the main thread, not in a separate thread.

Option D) Clean compile but no output at runtime. This option is correct. The code will compile without any errors, but it will not produce any output at runtime. This is because the start method, which would have started a new thread and executed the code in the run method concurrently, is not called. Instead, the code directly calls the run method, which executes the for loop in the main thread. Since the code doesn't use the start method, there is no separate thread to execute the for loop and print the values.

Therefore, the correct answer is option D) Clean compile but no output at runtime.

  1. Runtime Exception

  2. finally block will be executed

  3. finally block will not be executed

  4. Compilation error in else block


Correct Option: C