Java Multiple Choice Questions & Answers on Java.lang package ThreadGroup class & Runnable Interface for Freshers

1. Which of interface contains all the methods used for handling thread related operations in Java?

a) Runnable interface
b) Math interface
c) System interface
d) ThreadHandling interface
Answer: a

Explanation: Runnable interface defines all the methods for handling thread operations in Java.
2. Which of these class is used to make a thread?

a) String
b) System
c) Thread
d) Runnable
Answer: c

Explanation: Thread class is used to make threads in java, Thread encapsulates a thread of execution. To create a new thread the program will either extend Thread or implement the Runnable interface.
3. Which of these interface is implemented by Thread class?

a) Runnable
b) Connections
c) Set
d) MapConnections
Answer: a
4. Which of these method of Thread class is used to suspend a thread for a period of time?

a) sleep()
b) terminate()
c) suspend()
d) stop()
Answer: a
5. toRadian() and toDegree() methods were added by which version of Java?
a) Java 1.0
b) Java 1.5
c) Java 2.0
d) Java 3.0
Answer: c

Explanation: toRadian() and toDegree() methods were added by Java 2.0 before that there was no method which could directly convert degree into radians and vice versa.
6. What is the output of this program?
  1.     class newthread implements Runnable {
  2.  Thread t1,t2;
  3.         newthread() {
  4.             t1 = new Thread(this,"Thread_1");
  5.             t2 = new Thread(this,"Thread_2");
  6.             t1.start();
  7.             t2.start();
  8.  }
  9.  public void run() {
  10.             t2.setPriority(Thread.MAX_PRIORITY); 
  11.      System.out.print(t1.equals(t2));
  12.         }    
  13.     }
  14.     class multithreaded_programing {
  15.         public static void main(String args[]) {
  16.             new newthread();        
  17.         }
  18.     }
a) true
b) false
c) truetrue
d) falsefalse
Answer: d

Explanation: Threads t1 & t2 are created by class newthread that is implementing runnable interface, hence both the threads are provided their own run() method specifying the actions to be taken. When constructor of newthread class is called first the run() method of t1 executes than the run method of t2 printing 2 times “false” as both the threads are not equal one is having different priority than other, hence falsefalse is printed.
7. What is the output of this program?
  1.     class newthread implements Runnable {
  2.  Thread t;
  3.         newthread() {
  4.             t = new Thread(this,"New Thread");
  5.             t.start();
  6.  }
  7.  public void run() {
  8.             t.setPriority(Thread.MAX_PRIORITY); 
  9.             System.out.println(t);
  10.  }
  11.     }
  12.     class multithreaded_programing {
  13.         public static void main(String args[]) {
  14.             new newthread();        
  15.         }
  16.     }
a) Thread[New Thread,0,main] 
b) Thread[New Thread,1,main] 
c) Thread[New Thread,5,main] 
d) Thread[New Thread,10,main]

Answer: d

Explanation: Thread t has been made with default priority value 5 but in run method the priority has been explicitly changed to MAX_PRIORITY of class thread, that is 10 by code ‘t.setPriority(Thread.MAX_PRIORITY);’ using the setPriority function of thread t.
8. What is the output of this program?
  1.     class newthread implements Runnable {
  2.  Thread t;
  3.         newthread() {
  4.             t = new Thread(this,"My Thread");
  5.             t.start();
  6.  }
  7.     }
  8.     class multithreaded_programing {
  9.         public static void main(String args[]) {
  10.             new newthread();        
  11.         }
  12.     }
a) My Thread
b) Thread[My Thread,5,main] c) Compilation Error
d) Runtime Error
Answer: c

Explanation: Thread t has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error.
9. What is the output of this program?
  1.     class newthread implements Runnable {
  2.  Thread t;
  3.         newthread() {
  4.             t = new Thread(this,"My Thread");
  5.             t.start();
  6.  }
  7.  public void run() {
  8.      System.out.println(t.getName());
  9.  }
  10.     }
  11.     class multithreaded_programing {
  12.         public static void main(String args[]) {
  13.             new newthread();        
  14.         }
  15.     }
a) My Thread
b) Thread[My Thread,5,main] c) Compilation Error
d) Runtime Error
Answer: a
10. What is the output of this program?
  1.     class newthread implements Runnable {
  2.  Thread t;
  3.         newthread() {
  4.             t = new Thread(this,"My Thread");
  5.             t.start();
  6.  }
  7.  public void run() {
  8.      System.out.println(t);
  9.  }
  10.     }
  11.     class multithreaded_programing {
  12.         public static void main(String args[]) {
  13.             new newthread();        
  14.         }
  15.     }
a) My Thread
b) Thread[My Thread,5,main] c) Compilation Error
d) Runtime Error
Answer: b

Related

Multiple Choice Questions 8369547818720350961

Post a Comment

emo-but-icon

item