ExamVeda
Login
Home
71
Which of the following will ensure the thread will be in running state?
Discuss
Answer & Solution
Answer: Option E
Solution:
Let's understand this question about threads in Java!
The question asks us which of the given options will guarantee that a thread is in the running state.
Option A: yield()
yield() suggests to the Java Virtual Machine (JVM) that the current thread is willing to relinquish its current use of a processor.
The JVM is free to ignore this suggestion. It doesn't *guarantee* the thread will stay running or give up the processor; it just offers a hint.
Option B: notify()
notify() is used for inter-thread communication.
It wakes up a single thread that is waiting on the object's monitor.
It doesn't directly put a thread into the running state; it makes a waiting thread *eligible* to run, but it might have to wait its turn.
Option C: wait()
wait() causes the current thread to *relinquish* the lock and enter the waiting state.
The thread only becomes *eligible* to run again when another thread calls notify() or notifyAll() on the same object.
So, wait() definitely does *not* ensure the thread is in the running state; it does the opposite!
Option D: Thread.killThread()
There is no standard Thread.killThread() method in Java for directly killing a thread in a safe way.
This method is not a part of Java Thread API.
Option E: None of these
Since none of the options guarantees that a thread is in a running state by their use,
hence Option E is correct
Therefore, the answer is Option E: None of these.
72
Which of these method wakes up all the threads?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
73
Which of the following is a correct constructor for thread?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
74
What will be the output of the following Java code?
class newthread implements Runnable
{
Thread t;
newthread()
    {
  t = new Thread(this,"New Thread");
  t.start();
}
public void run()
    {
  t.setPriority(Thread.MAX_PRIORITY);	
        System.out.println(t);
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
75
Which of this method is used to find out that a thread is still running or not?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
76
Which of these statement is incorrect?
Discuss
Answer & Solution
Answer: Option D
Solution:
Option A is correct: You can create a thread by implementing the Runnable interface. Your class provides the run() method, which contains the code the thread will execute.
Option B is correct: You can also create a thread by extending the Thread class. This is another way to define what the thread will do within the run() method.
Option C is correct: The start() method is crucial. It's what actually initiates the thread's execution. It prepares the thread and then calls the run() method in a separate thread of execution.
Option D is incorrect: You should never call the run() method directly to start a thread. Calling run() directly simply executes the code within the run() method in the current thread, rather than creating a new thread. To properly start a thread, you must use the start() method.
77
Which of these method is used to begin the execution of a thread?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
78
What will be the output of the following Java program?
class newthread extends Thread
{
Thread t;
newthread()
    {
  t1 = new Thread(this,"Thread_1");
  t2 = new Thread(this,"Thread_2");
  t1.start();
  t2.start();
}
public void run()
    {
  t2.setPriority(Thread.MAX_PRIORITY);	
  System.out.print(t1.equals(t2));
    }    
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}
Discuss
Answer & Solution
Answer: Option D
Solution:
In the given program, the class newthread extends Thread, and within its constructor, it creates two thread objects: t1 and t2.

Both threads are started with t1.start() and t2.start(), and they both execute the same run() method of the class instance because this is passed as the target.

Inside the run() method, the priority of t2 is set to Thread.MAX_PRIORITY. However, this has no effect on the logic or output because thread priority only suggests execution preference to the scheduler.

The key line is System.out.print(t1.equals(t2)); which checks if t1 and t2 are equal.

Since t1 and t2 are two separate thread instances, the equals() method returns false.

Because both threads run the same run() method, the output false is printed twice — once by each thread.

Hence, the output is falsefalse.
79
What requires less resources?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
80
What will be the output of the following Java code?
class newthread extends Thread 
{
newthread()
    {
  super("My Thread");
  start();
}
public void run()
    {
  System.out.println(this);
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board