ExamVeda
Login
Home
31
In java a thread can be created by ..........
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
32
When a class extends the Thread class ,it should override ............ method of Thread class to start that thread.
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
33
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");
        }
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
34
Which of the following constructor of class Thread is valid one?
Discuss
Answer & Solution
Answer: Option C
Solution:
A. Thread(Runnable threadOb, int priority)
This constructor is invalid because the Thread class does not have a constructor that takes a Runnable object and an int priority as arguments.

B. Thread(int priority)
This constructor is invalid because the Thread class does not have a constructor that takes only an int priority as an argument.

C. Thread(Runnable threadOb, String threadName)
This constructor is valid. It creates a new thread with the provided Runnable object and assigns it the specified name.

D. Thread(String threadName, int priority)
This constructor is invalid because the Thread class does not have a constructor that takes both a String thread name and an int priority as arguments.

E. None of these
This option is incorrect because option C, "Thread(Runnable threadOb, String threadName)," is a valid constructor for the Thread class.

Therefore, the correct answer is:
C. Thread(Runnable threadOb, String threadName)
35
Analyze the following code:
public abstract class Test implements Runnable{
        public void doSomething() { };
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
36
Analyze the following code:
public class Test implements Runnable{
        public static void main(String[] args){
                Test t = new Test();
                t.start();
        }

        public void run() { }
}
Discuss
Answer & Solution
Answer: Option A
Solution:
Code Analysis:

     1. Implementing Runnable: The Test class implements the Runnable interface, which requires the implementation of the run() method.

     2. Creating an Instance: In the main method, an instance of Test is created (Test t = new Test();).

     3. Calling start() Method: The code then calls t.start();. However, start() is a method of the Thread class, not the Runnable interface.

     4. Compilation Error: Since Test implements Runnable and not Thread, it does not inherit the start() method from Thread. Therefore, calling start() on an instance of Test causes a compilation error because the start() method is not defined in the Test class.

Therefore, the correct option is A: The program does not compile because the start() method is not defined in the Test class.
37
Analyze the following code:
public class Test implements Runnable{
        public static void main(String[] args){
                Test t = new Test();
        }

        public Test(){
                Thread t = new Thread(this);
                t.start();
        }

        public void run(){
                System.out.println("test");
        }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
38
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();
	}
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
39
What will happen when you attempt to compile and run the following code?
public class Test extends Thread{
      public static void main(String argv[]){
            Test t = new Test();
            t.run();
            t.start();
      }
      public void run(){
            System.out.println("run-test");
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

t.run() Legal, but does not start a new thread , it is like a method call of a class Test BUT t.start() creates a thread and call run() method.

40
What will happen after compiling and running following code?
class A implements Runnable{
      public void run(){
            System.out.println("run-a");
      }
}

public class Test{
      public static void main(String... args){
            A a = new A();
            Thread t = new Thread(a);
            t.start();
            t.start();
      }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

Once a thread has been started, it can never be started again. 2nd time t.start() throws java.lang.IllegalThreadStateException.