Select the correct answerWhat will be the output of the following Java program?class newthread implements Runnable { Thread var1,var2; newthread() { var1 = new Thread(this,"Thread_1"); var2 = new Thread(this,"Thread_2"); var1.start(); var2.start(); } public void run() { var2.setPriority(Thread.MAX_PRIORITY); System.out.print(var1.equals(var2)); } } class Multithreaded_programing { public static void main(String args[]) { new newthread(); } }Optionsfalsetruetruetruefalsefalse
Question
Select the correct answerWhat will be the output of the following Java program?class newthread implements Runnable { Thread var1,var2; newthread() { var1 = new Thread(this,"Thread_1"); var2 = new Thread(this,"Thread_2"); var1.start(); var2.start(); } public void run() { var2.setPriority(Thread.MAX_PRIORITY); System.out.print(var1.equals(var2)); } } class Multithreaded_programing { public static void main(String args[]) { new newthread(); } }Optionsfalsetruetruetruefalsefalse
Solution
The correct answer is "false".
Here's why:
In the given Java program, two threads (var1 and var2) are created in the constructor of the class "newthread". Both threads are started in the constructor itself.
In the run() method, the priority of var2 is set to MAX_PRIORITY. However, setting the priority of a thread does not change the fact that var1 and var2 are two different thread objects.
The equals() method in Java is used to check the equality of two objects. In this case, it checks whether var1 and var2 are the same object, which they are not. Therefore, the output of var1.equals(var2) will be "false".
Similar Questions
Select the correct answerWhat will be the output of the following Java code?class newthread extends Thread { Thread T; newthread() { T = new Thread(this,"T1"); T.start(); } public void run() { try { T.join() System.out.println(T.getName()); } catch(Exception e) { System.out.print("Exception"); } } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }OptionsExceptionThread[My Thread,5,main] My ThreadRuntime Error
Select the correct answerWhat is the priority of the thread in output in the following Java program?class Main{ public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("New Thread"); System.out.println(t.getName()); }}OptionsNew ThreadThreadmainThread[New Thread,5,main]
What will be the output of the following Java code?class multithreaded_programing { public static void main(String args[]) {Thread t = Thread.currentThread();System.out.println(t); } } Thread[5,main]Thread[main,5]Thread[main,0]Thread[main,5,main]
Find the output of the below code:public class CN{public static void main(String args[]) { Thread startThread1 = new Thread(new Output("start1")); Thread runThread = new Thread(new Output("run")); Thread startThread2 = new Thread(new Output("start2")); startThread2.start(); startThread1.start(); runThread.run(); }private static class Output implements Runnable{ private String str; public Output(String str){ this.str = str; } @Override public void run() { System.out.println(str + " is executed by Thread : " + Thread.currentThread().getName()); }}}
elect the correct answerWhat will be the output of the following Java code?class Main{ public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("New Thread"); System.out.println(t); }}OptionsThread[main,5,main]Thread[New Thread,5]Thread[5,main]Thread[New Thread,5,main]
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.