ExamVeda
Login
Home
61
What will be the output of the following Java code?
class newthread implements Runnable
{
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
No explanation is given for this question. Let's Discuss on Board
62
Which of these method wakes up the first thread that called wait()?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
63
Which of these keywords are used to implement synchronization?
class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
    {
  name = threadname;
  t = new Thread(this,name);
  t.start();
}
public void run()
    {
    }

}
class multithreaded_programing
{
    public static void main(String args[])
    {
  newthread obj1 = 	 new newthread("one");
  newthread obj2 =	 new newthread("two");
        try
        {
            obj1.t.wait();	
            System.out.print(obj1.t.isAlive());
        }
        catch(Exception e)
        {
  System.out.print("Main thread interrupted");
        }
    }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
64
Which of this method can be used to make the main thread to be executed last among all the threads?
Discuss
Answer & Solution
Answer: Option C
Solution:
The join() method in Java is used to pause the execution of the current thread until the thread on which join() is called has finished executing.

To ensure that the main thread executes last, we can call the join() method on all other threads in the main thread.

This causes the main thread to wait for the completion of other threads before it proceeds.

sleep() is used to pause a thread for a specific time, not to coordinate the order of thread completion.

stop() is deprecated and unsafe.

call() is not a valid method for controlling thread execution in this context.
65
Which of the following stops execution of a thread?
Discuss
Answer & Solution
Answer: Option B
Solution:
Understanding Thread Execution Control in Java
Let's break down why one of these options stops a thread's execution.

Option A: Calling SetPriority() method on a Thread object
setPriority() is used to influence the scheduler to give a thread more or less CPU time.
It doesn't stop the thread; it just suggests a priority.

Option B: Calling notify() method on an object
notify() is used for thread communication.
It wakes up a single thread that's waiting on the object's monitor.
It doesn't stop the currently running thread; it potentially allows another thread to run.

Option C: Calling wait() method on an object
wait() causes the current thread to pause its execution and enter a waiting state.
The thread releases the lock on the object and waits until another thread calls notify() or notifyAll() on the same object.
This is the correct answer.

Option D: Calling read() method on an InputStream object
read() is used to read data from an input stream.
If no data is available, the thread might block temporarily until data becomes available.
While it *can* appear to pause the thread, it's waiting for I/O, not explicitly stopping execution in the same way as wait().
The thread remains alive.

In Summary: The wait() method directly suspends a thread's execution and places it in a waiting state, making Option C the correct answer.
66
What will be the output of the following Java program?
class newthread extends Thread
{
Thread t;
String name;
newthread(String threadname)
    {
  name = threadname;
  t = new Thread(this,name);
  t.start();
}
public void run()
    {
    }

}
class multithreaded_programing
{
    public static void main(String args[])
    {
      newthread obj1 = 	 new newthread("one");
      newthread obj2 =	 new newthread("two");
        try
        {
            Thread.sleep(1000);	
            System.out.print(obj1.t.isAlive());
        }
        catch(InterruptedException e)
        {
        System.out.print("Main thread interrupted");
        }
    }
}
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
67
Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
68
What is true about time slicing?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
69
Thread priority in Java is?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
70
What decides thread priority?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board