Search This Blog

Friday, January 28, 2011

Question 1

Which of the following lines will not compile ?? Choose Two.

  1: public class Question1 {
  2:     public Question1() {
  3:     }
  4:     public static void main(){
  5:     	Thread t1 = new Thread(); // Line 1
  6:     	Thread t2 = new Thread(new MyClass());//Line 2
  7:     	Thread t3 = new Thread(new MyClass(),"Thread3");//Line 3
  8:     	Thread t4 = new Thread("Thread4");//Line 4
  9:     	Thread t5 = new Thread("Thread5",5);//Line 5
 10:     	Thread t6 = new Thread ("Thread6", new MyClass());//Line 6
 11:     }
 12: }
 13: class MyClass implements Runnable{
 14: 	public void run(){}
 15: }

Options :
a. Line 1
b. Line 2
c. Line 3
d. Line 4
e. Line 5
f. Line 6



Answer : e & f. ie Line 5 and Line 6.


This question tries to find your understanding of Thread constructor.


Line 5 is does not compile because Thread constructor don’t have a method to accept Thread priority. Similarly Line 6 does not compile because there is no such constructor to accept name and runnable interface.


JAVA API for reference.


Thread()
          Allocates a new Thread object.

Thread(Runnable target)
          Allocates a new Thread object.

Thread(Runnable target, String name)
          Allocates a new Thread object.

Thread(String name)
          Allocates a new Thread object.

image

Question 1SocialTwist Tell-a-Friend

No comments:

Post a Comment