Tag: programming languages

Questions Related to programming languages

  1. Starting of Main block Static block In the Main block Display block

  2. Static block Display block Starting of Main block In the Main block Display block

  3. Error: Non static method cannot accessed from static context

  4. Starting of Main block In the Main block Display block


Correct Option: B
  1. The type List is assignable to List.

  2. The type List is assignable to List>.

  3. The type List is assignable to List extends B>.

  4. The type List extends A> is assignable to List.

  5. The type List is assignable to any List reference.

  6. The type List extends B> is assignable to List extends A>.


Correct Option: B,C,F

class Sub extends Super{ String name; Sub(String s) { super(s); name=s; } public static void main(String args[]){ Super sup = new Super("First"); Sub sub = new Sub("Second"); System.out.println(sub.name + " " + sup.name); } } class Super{ String name; Super(String s){ name =s ; } }

  1. Error: Super(java.lang.String) in Super cannot be applied to ()

  2. First Second

  3. Second First

  4. Runtime Exception


Correct Option: C

Given: 1. public class Threads2 implements Runnable { 2. 3. public void nun() { 4. System.out.println(”run.”); 5. throw new RuntimeException(”Problem”); 6. } 7. public static void main(String[] args) { 8. Thread t = new Thread(new Threads2()); 9. t.start(); 10. System.out.println(”End of method.”); 11. } 12. } Which two can be results? (Choose two.) A. java.lang.RuntimeException: Problem B. run. java.lang.RuntimeException: Problem C. End of method. java.lang.RuntimeException: Problem D. End of method. run. java.lang.RuntimeException: Problem E. run. java.lang.RuntimeException: Problem End of method.

  1. A

  2. B

  3. C

  4. D

  5. E


Correct Option: D,E

AI Explanation

To answer this question, let's analyze the given code:

1. public class Threads2 implements Runnable {
2.
3. public void run() {
4.     System.out.println("run.");
5.     throw new RuntimeException("Problem");
6. }
7.
8. public static void main(String[] args) {
9.     Thread t = new Thread(new Threads2());
10.    t.start();
11.    System.out.println("End of method.");
12. }
13.
}

The code defines a class Threads2 that implements the Runnable interface. It has a run() method that prints "run." and throws a RuntimeException with the message "Problem". In the main() method, a new Thread is created with an instance of Threads2 and started. It also prints "End of method.".

Now let's go through each option to determine which ones can be the result:

Option A) A. java.lang.RuntimeException: Problem This option is correct because the run() method throws a RuntimeException with the message "Problem". This exception will be caught by the JVM, and the stack trace will be printed, resulting in the output java.lang.RuntimeException: Problem.

Option B) B. run. This option is incorrect because it only includes the output "run.". The code also prints "End of method." after starting the thread.

Option C) C. End of method. This option is incorrect because it only includes the output "End of method.". The code also throws a RuntimeException in the run() method, which will be caught by the JVM.

Option D) D. End of method. run. java.lang.RuntimeException: Problem This option is correct because it includes all the possible outputs. The code first prints "End of method.", then starts the thread, which executes the run() method and prints "run.". Finally, the exception is thrown and caught by the JVM, resulting in the output java.lang.RuntimeException: Problem.

Option E) E. run. java.lang.RuntimeException: Problem End of method. This option is correct because it includes all the possible outputs, but in a different order. The code first starts the thread, which executes the run() method and prints "run.". Then, the exception is thrown and caught by the JVM, resulting in the output java.lang.RuntimeException: Problem. Finally, "End of method." is printed.

Therefore, the correct answers are options D and E.