Q: Which of these interfaces is implemented by the Thread class?
A: Runnable
Q: What will happen if two threads of the same priority are called to be processed simultaneously?
A: It is dependent on the operating system
Q: Which of the following are the most common compile time errors in Java programming?i) Missing semicolonsii) Use of undeclared variablesiii) Attempting to use a negative size for an arrayiv) Bad reference of objects
A: i, ii and iv only
Q: Which keyword, when applied to a method, indicates that only one thread should execute the method at a time?
A: synchronized
Q: What is the output for the below code ?
public class Test extends Thread{
public static void main(String argv[]){
Test t = new Test();
t.run();
}
public void start(){
for(int i = 0; i < 10; i++){
System.out.println("Value of i = " + i);
}
}
}
A: Clean compile but no output at runtime
Q: Which interface contains all the methods used for handling thread-related operations in Java?
A: Runnable interface
Q: What will be the output of the following program?
class Test implements Runnable {
public void run() {
System.out.println("Run");
}
}
class Myclass {
public static void main(String[] args) {
Test t = new Test();
t.start();
System.out.println("Main");
}
}
A: Compile time error
Q: What will be the output of the below code?
class One extends Thread{
public void run(){
for(int i=0; i<2; i++){
System.out.print(i);
}
}
}
public class Test{
public static void main(String args[]){
Test t = new Test();
t.call(new One());
}
public void call(One o){
o.start();
}
}
A: 01
Q: What does the notifyAll() method do?
A: Wakes up all threads that are waiting on this object's monitor
Q: What will be the output after compiling and executing the below code?
public class Test implements Runnable{
public static void main(String[] args) throws InterruptedException{
Thread a = new Thread(new Test());
a.start();
System.out.print("Begin");
a.join();
System.out.print("End");
}
public void run(){
System.out.print("Run");
}
}
A: "BeginRunEnd" is printed.
Q: Analyze the following code:
public class Test implements Runnable{
public static void main(String[] args){
Test t = new Test();
t.start();
}
public void run() { }
}
A: The program does not compile because the start() method is not defined in the Test class.
Q: What will be the output of this below code?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t);
}
}
A: Thread[New Thread,5,main].
Q: What will be the priority of the thread in the output for this program?
class multithreaded_programing
{
public static void main(String args[])
{
Thread t = Thread.currentThread();
t.setName("New Thread");
System.out.println(t.getName());
}
}
A: New Thread
Q: What will be the output of the following program?
class Main {
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println(t);
}
}
A: Thread[main,5,main]