41. 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();
}
}
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();
}
}
42. What will be the output?
class A extends Thread{
public void run(){
for(int i=0; i<2; i++){
System.out.println(i);
}
}
}
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.check(new A(){});
}
public void check(A a){
a.start();
}
}
class A extends Thread{
public void run(){
for(int i=0; i<2; i++){
System.out.println(i);
}
}
}
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.check(new A(){});
}
public void check(A a){
a.start();
}
}
43. 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");
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
Thread t = new Thread(a);
System.out.println(t.isAlive());
t.start();
System.out.println(t.isAlive());
}
}
class A implements Runnable{
public void run(){
System.out.println("run-A");
}
}
public class Test{
public static void main(String argv[]){
A a = new A();
Thread t = new Thread(a);
System.out.println(t.isAlive());
t.start();
System.out.println(t.isAlive());
}
}
44. Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()
1) yield()
2) sleep(long msec)
3) go()
4) stop()
45. What notifyAll() method do?
46. 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);
}
}
}
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);
}
}
}
47. Which keyword when applied on a method indicates that only one thread should execute the method at a time.
48. What is the output for the below code?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class Test{
public static void main(String... args){
A a = new A();
Thread t = new Thread(a);
t.setName("good");
t.start();
}
}
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class Test{
public static void main(String... args){
A a = new A();
Thread t = new Thread(a);
t.setName("good");
t.start();
}
}
49. Predict the output:
public class Test extends Thread{
private int i;
public void run(){
i++;
}
public static void main(String[] args){
Test a = new Test();
a.run();
System.out.print(a.i);
a.start();
System.out.print(a.i);
}
}
public class Test extends Thread{
private int i;
public void run(){
i++;
}
public static void main(String[] args){
Test a = new Test();
a.run();
System.out.print(a.i);
a.start();
System.out.print(a.i);
}
}
50. 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");
}
}
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");
}
}
Read More Section(Threads)
Each Section contains maximum 100 MCQs question on Threads. To get more questions visit other sections.