Q: What will be the output after compiling and executing the following 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.Compilation fails.
B.An exception is thrown at runtime.
C."BeginRunEnd" is printed.
D."BeginEndRun" is printed.
Correct Option: CExplanation: "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.
B.The program compiles, but it does not run because the start() method is not defined.
C.The program compiles, but it does not run because the run() method is not implemented.
D. The program compiles and runs fine.
Correct Option: AExplanation: The program does not compile because the start() method is not defined in the Test class.
Q: What will happen after compiling and running following code?
class A implements Runnable{
public void run(){
System.out.println("run-a");
}
}
1. public class Test{
2. public static void main(String... args){
3. A a = new A();
4. Thread t = new Thread(a);
5. t.start();
6. t.start();
7. }
8. }
A. run-a
B.run-a run-a
C.Compilation fails with an error at line 6
D.Compilation succeed but Runtime Exception
Correct Option: DExplanation: Compilation succeed but Runtime Exception
Q: Which of the following are methods of the Thread class?
1) yield() 2) sleep(long msec) 3) go() 4) stop()
A.1 , 2 and 4
B.1 and 3
C. 3 only
D.None of the above
Correct Option: AExplanation: 1 , 2 and 4
Q: Predict the output:
class A implements Runnable{
public void run(){
try{
for(int i=0;i<4;i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName());
}
}catch(InterruptedException e){
}
}
}
public class Test{
public static void main(String argv[]) throws Exception{
A a = new A();
Thread t = new Thread(a, "A");
Thread t1 = new Thread(a, "B");
t.start();
t.join();
t1.start();
}
}
A.A A A A B B B B
B. A B A B A B A B
C.Output order is not guaranteed
D.Compilation succeed but Runtime Exception
Correct Option: AExplanation: A A A A B B B B
Q: In java, a thread can be created by ..........
A.Extending the thread class.
B.Implementing Runnable interface.
C.Both of the above
D.None of these
Correct Option: CExplanation: Both of the above
Q: What will be the output?
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. 0 0
B.Compilation Error
C. 0 1
D.None of these
Correct Option: CExplanation: 0 1
Q:
When a class extends the Thread class, it should override ............ method of Thread class to start that thread.
A.start()
B. run()
C.init()
D. go()
Correct Option: BExplanation: run()
Q: What will be the output of the following program code?
public class Test implements Runnable{
public static void main(String[] args){
Thread t = new Thread(this);
t.start();
}
public void run(){
System.out.println("test");
}
}
A.The program does not compile because this cannot be referenced in a static method.
B.The program compiles fine, but it does not print anything because t does not invoke the run() method
C.The program compiles and runs fine and displays test on the console.
D.None of the above
Correct Option: AExplanation: The program does not compile because this cannot be referenced in a static method.
Q: What will happen when you attempt to compile and run the following code?
class A implements Runnable{
public void run(){
System.out.println("run-A");
}
}
1. public class Test{
2. public static void main(String argv[]){
3. A a = new A();
4. Thread t = new Thread(a);
5. System.out.println(t.isAlive());
6. t.start();
7. System.out.println(t.isAlive());
8. }
9. }