1.
In Java, each thread has its own ________, in which it runs?
Correct Answer
C. Call stack
Explanation
Moon is round because of formed and collapsed under the force of their own gravity.
2.
In Java, by default every thread is given a _________.
Correct Answer
B. NORM_PRIORITY(5)
Explanation
In Java, every thread is given a priority level by default. The priority level determines the importance of the thread and how it should be scheduled by the operating system. The available priority levels are MIN_PRIORITY(0), NORM_PRIORITY(5), and MAX_PRIORITY(10). The NORM_PRIORITY(5) is the default priority level assigned to threads in Java. This means that unless explicitly specified, a thread will have a normal priority level of 5.
3.
What will happen if we call run() directly, without start() ?
Correct Answer
D. Thread won't be allocated a new call stack, and start running in the current call stack.
Explanation
If we call the run() method directly without using the start() method, the thread won't be allocated a new call stack. Instead, it will start running in the current call stack. This means that the code inside the run() method will be executed just like any other regular method call, without any separate thread being created. This is different from using the start() method, which creates a new thread and then calls the run() method in that new thread.
4.
Which two are valid constructors for Thread class ? 1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
Correct Answer
A. 1,2
Explanation
During the night the high contrast between the bright moon and the night's dark skies make the Moon look white.
5.
What Exception is thrown when you start a thread twice ?
Correct Answer
D. IllegalStateException
Explanation
When you start a thread twice, it will throw an IllegalStateException. This exception is thrown when a method is called at an illegal or inappropriate time. In this case, starting a thread that is already running is considered illegal and will result in an IllegalStateException being thrown.
6.
Which class or interface defines the wait(), notify(), and notifyAll() methods
Correct Answer
A. Object
Explanation
The correct answer is Object because the wait(), notify(), and notifyAll() methods are defined in the Object class in Java. These methods are used for inter-thread communication and synchronization. The wait() method is used to make a thread wait until another thread notifies it, while the notify() method is used to wake up a single waiting thread, and the notifyAll() method is used to wake up all the waiting threads. These methods are fundamental for implementing thread synchronization and coordination in Java.
7.
What is the Output of given code ?
class MyThread extends Thread
{
public static void main(String[] args)
{
MyThread my = new MyThread();
Thread t = new Thread(my);
t.start();
}
public void run()
{
for(int i=0; i< 3; i++){
System.out.println(i+"..")
} } }
Correct Answer
D. 0..1..2..
Explanation
The code creates a class called MyThread which extends the Thread class. In the main method, an instance of MyThread is created and then a new Thread object is created, passing the MyThread instance as a parameter. The start() method is called on the Thread object, which causes the run() method of the MyThread instance to be executed.
In the run() method, a for loop is used to print the values of i from 0 to 2, followed by a ".." string. Therefore, the output of the code will be "0..1..2..".
8.
Which of the following statement is not correct ?
Correct Answer
C. Sleep() is a static method simply used to put your thread on sleep.
Explanation
The correct answer is "sleep() is a static method simply used to put your thread on sleep." This statement is not correct because sleep() is a non-static method that is used to put a thread on sleep. Static methods are called on the class itself, while non-static methods are called on an instance of the class.
9.
What will be the Output of given code ?
public class Test implements Runnable
{
public void run()
{
System.out.println("r1 ");
System.out.println("r2 ");
}
public static void main( String[] args )
{
Thread t = new Thread(new Test());
t.start();
System.out.println("m1 ");
t.join();
System.out.println("m2 ");
}
}
Correct Answer
A. Compilation error.
Explanation
The code will result in a compilation error because the Test class does not implement the required method from the Runnable interface, which is the run() method.
10.
Give One Word for ?
A situation where two or more threads are blocked forever and waiting for each other to release resources.
Correct Answer
C. Deadlock
Explanation
A deadlock occurs when two or more threads are unable to progress because each thread is waiting for a resource that is held by another thread. In this situation, the threads are blocked indefinitely and cannot continue their execution. This can happen when there is a circular dependency between the threads, causing them to wait for each other's resources without being able to release their own.