Topic – Multithreading
1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized
View Answer
Answer: d
2. Which of this method is used to avoid polling in Java?
a) wait()
b) notify()
c) notifyAll()
d) all of the mentioned
View Answer
Answer: d
Explanation: Polling is a usually implemented by looping in CPU is wastes CPU time, one thread
being executed depends on other thread output and the other thread depends on the response on
the data given to the first thread. In such situation CPU time is wasted, in Java this is avoided by
using methods wait(), notify() and notifyAll().
3. Which of these method is used to tell the calling thread to give up a monitor and go to sleep
until some other thread enters the same monitor?
a) wait()
b) notify()
c) notifyAll()
d) sleep()
View Answer
Answer: a
Explanation: wait() method is used to tell the calling thread to give up a monitor and go to sleep
until some other thread enters the same monitor. This helps in avoiding polling and minimizes
CPU idle time.
4. Which of these method wakes up the first thread that called wait()?
a) wake()
b) notify()
c) start()
d) notifyAll()
View Answer
Answer: b
Explanation: None.
5. Which of these method wakes up all the threads?
a) wakeAll()
b) notify()
c) start()
d) notifyAll()
View Answer
Answer: d
Explanation: notifyAll() wakes up all the threads that called wait() on the same object. The
highest priority thread will run first.
6. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need access to a shared
resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information the require
View Answer
Answer: a
Explanation: When two or more threads need to access the same shared resource, they need some
way to ensure that the resource will be used by only one thread at a time, the process by which
this is achieved is called synchronization
7. What will be the output of the following Java program?
1. class newthread extends Thread
2. {
3. Thread t;
4. String name;
5. newthread(String threadname)
6. {
7. name = threadname;
8. t = new Thread(this,name);
9. t.start();
10. }
11. public void run()
12. {
13. }
14.
15. }
16. class multithreaded_programing
17. {
18. public static void main(String args[])
19. {
20. newthread obj1 = new newthread("one");
21. newthread obj2 = new newthread("two");
22. try
23. {
24. obj1.t.wait();
25. System.out.print(obj1.t.isAlive());
26. }
27. catch(Exception e)
28. {
29. System.out.print("Main thread interrupted");
30. }
31. }
32. }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Answer: c
Explanation: obj1.t.wait() causes main thread to go out of processing in sleep state hence causes
exception and “Main thread interrupted” is printed.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Main thread interrupted
8. What will be the output of the following Java program?
1. class newthread extends Thread
2. {
3. Thread t;
4. String name;
5. newthread(String threadname)
6. {
7. name = threadname;
8. t = new Thread(this,name);
9. t.start();
10. }
11. public void run()
12. {
13. }
14.
15. }
16. class multithreaded_programing
17. {
18. public static void main(String args[])
19. {
20. newthread obj1 = new newthread("one");
21. newthread obj2 = new newthread("two");
22. try
23. {
24. Thread.sleep(1000);
25. System.out.print(obj1.t.isAlive());
26. }
27. catch(InterruptedException e)
28. {
29. System.out.print("Main thread interrupted");
30. }
31. }
32. }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Answer: b
Explanation: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence
onj1.t.isAlive() returns false.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
false
9. What will be the output of the following Java program?
1. class newthread extends Thread
2. {
3. Thread t;
4. String name;
5. newthread(String threadname)
6. {
7. name = threadname;
8. t = new Thread(this,name);
9. t.start();
10. }
11. public void run()
12. {
13. }
14.
15. }
16. class multithreaded_programing
17. {
18. public static void main(String args[])
19. {
20. newthread obj1 = new newthread("one");
21. newthread obj2 = new newthread("two");
22. try
23. {
24. System.out.print(obj1.t.equals(obj2.t));
25. }
26. catch(Exception e)
27. {
28. System.out.print("Main thread interrupted");
29. }
30. }
31. }
a) true
b) false
c) Main thread interrupted
d) None of the mentioned
View Answer
Answer: b
Explanation: Both obj1 and obj2 have threads with different name that is “one” and “two” hence
obj1.t.equals(obj2.t) returns false.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
false
10. What will be the output of the following Java program?
1. class newthread extends Thread
2. {
3. Thread t;
4. newthread()
5. {
6. t1 = new Thread(this,"Thread_1");
7. t2 = new Thread(this,"Thread_2");
8. t1.start();
9. t2.start();
10. }
11. public void run()
12. {
13. t2.setPriority(Thread.MAX_PRIORITY);
14. System.out.print(t1.equals(t2));
15. }
16. }
17. class multithreaded_programing
18. {
19. public static void main(String args[])
20. {
21. new newthread();
22. }
23. }
a) true
b) false
c) truetrue
d) falsefalse
View Answer
Answer: d
Explanation: This program was previously done by using Runnable interface, here we have used
Thread class. This shows both the method are equivalent, we can use any of them to create a
thread.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
11. What requires less resources?
a) Thread
b) Process
c) Thread and Process
d) Neither Thread nor Process
View Answer
Answer: a
Explanation: Thread is a lightweight and requires less resources to create and exist in the
process. Thread shares the process resources.
12. What does not prevent JVM from terminating?
a) Process
b) Daemon Thread
c) User Thread
d) JVM Thread
View Answer
Answer: b
Explanation: Daemon thread runs in the background and does not prevent JVM from
terminating. Child of daemon thread is also daemon thread.
13. What decides thread priority?
a) Process
b) Process scheduler
c) Thread
d) Thread scheduler
View Answer
Answer: d
Explanation: Thread scheduler decides the priority of the thread execution. This cannot
guarantee that higher priority thread will be executed first, it depends on thread scheduler
implementation that is OS dependent.
14. What is true about time slicing?
a) Time slicing is OS service that allocates CPU time to available runnable thread
b) Time slicing is the process to divide the available CPU time to available runnable thread
c) Time slicing depends on its implementation in OS
d) Time slicing allocates more resources to thread
View Answer
Answer: b
Explanation: Time slicing is the process to divide the available CPU time to available runnable
thread.
15. Deadlock is a situation when thread is waiting for other thread to release acquired object.
a) True
b) False
View Answer
Answer: a
Explanation: Deadlock is java programming situation where one thread waits for an object lock
that is acquired by other thread and vice-versa.
16. What should not be done to avoid deadlock?
a) Avoid using multiple threads
b) Avoid hold several locks at once
c) Execute foreign code while holding a lock
d) Use interruptible locks
View Answer
Answer: c
Explanation: To avoid deadlock situation in Java programming do not execute foreign code
while holding a lock.
17. What is true about threading?
a) run() method calls start() method and runs the code
b) run() method creates new thread
c) run() method can be called directly without start() method being called
d) start() method creates new thread and calls code written in run() method
View Answer
Answer: d
Explanation: start() eventually calls run() method. Start() method creates thread and calls the
code written inside run method.
18. Which of the following is a correct constructor for thread?
a) Thread(Runnable a, String str)
b) Thread(int priority)
c) Thread(Runnable a, int priority)
d) Thread(Runnable a, ThreadGroup t)
View Answer
Answer: a
Explanation: Thread(Runnable a, String str) is a valid constructor for thread. Thread() is also a
valid constructor.
19. Which of the following stops execution of a thread?
a) Calling SetPriority() method on a Thread object
b) Calling notify() method on an object
c) Calling wait() method on an object
d) Calling read() method on an InputStream object
View Answer
Answer: b
Explanation: notify() wakes up a single thread which is waiting for this object.
20. Which of the following will ensure the thread will be in running state?
a) yield()
b) notify()
c) wait()
d) Thread.killThread()
View Answer
Answer: c
Explanation: wait() always causes the current thread to go into the object’s wait pool. Hence,
using this in a thread will keep it in running state.
21. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run simultaneously
c) It’s a process in which many different process are able to access same information
d) It’s a process in which a single process can access information from many sources
View Answer
Answer: b
Explanation: Multithreaded programming a process in which two or more parts of the same
process run simultaneously.
22. Which of these are types of multitasking?
a) Process based
b) Thread based
c) Process and Thread based
d) None of the mentioned
View Answer
Answer: c
Explanation: There are two types of multitasking: Process based multitasking and Thread based
multitasking.
23. Thread priority in Java is?
a) Integer
b) Float
c) double
d) long
View Answer
Answer: a
Explanation: Java assigns to each thread a priority that determines hoe that thread should be
treated with respect to others. Thread priority is integers that specify relative priority of one
thread to another.
24. What will happen if two thread of the same priority are called to be processed
simultaneously?
a) Anyone will be executed first lexographically
b) Both of them will be executed simultaneously
c) None of them will be executed
d) It is dependent on the operating system
View Answer
Answer: d
Explanation: In cases where two or more thread with same priority are competing for CPU
cycles, different operating system handle this situation differently. Some execute them in time
sliced manner some depending on the thread they call.
25. Which of these statements is incorrect?
a) By multithreading CPU idle time is minimized, and we can take maximum use of it
b) By multitasking CPU idle time is minimized, and we can take maximum use of it
c) Two thread in Java can have the same priority
d) A thread can exist only in two states, running and blocked
View Answer
Answer: d
Explanation: Thread exist in several states, a thread can be running, suspended, blocked,
terminated & ready to run.
26. What will be the output of the following Java code?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. System.out.println(t);
7. }
8. }
a) Thread[5,main]
b) Thread[main,5]
c) Thread[main,0]
d) Thread[main,5,main]
View Answer
Answer: d
Explanation: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
27. What is the priority of the thread in the following Java Program?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. System.out.println(t);
7. }
8. }
a) 4
b) 5
c) 0
d) 1
View Answer
Answer: b
Explanation: The output of program is Thread[main,5,main], in this priority assigned to the
thread is 5. It’s the default value. Since we have not named the thread they are named by the
group to they belong i:e main method.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
28. What is the name of the thread in the following Java Program?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. System.out.println(t);
7. }
8. }
a) main
b) Thread
c) System
d) None of the mentioned
View Answer
Answer: a
Explanation: The output of program is Thread[main,5,main], Since we have not explicitly named
the thread they are named by the group to they belong i:e main method. Hence they are named
‘main’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
29. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()
View Answer
Answer: c
Explanation: None.
30. 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()
View Answer
Answer: a
Explanation: None.
31. Which function of pre defined class Thread is used to check weather current thread being
checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()
View Answer
Answer: a
Explanation:isAlive() function is defined in class Thread, it is used for implementing
multithreading and to check whether the thread called upon is still running or not.
32. What will be the output of the following Java code?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. t.setName("New Thread");
7. System.out.println(t);
8. }
9. }
a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]
View Answer
Answer: d
Explanation: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]
33. What is the priority of the thread in output in the following Java program?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. t.setName("New Thread");
7. System.out.println(t.getName());
8. }
9. }
a) main
b) Thread
c) New Thread
d) Thread[New Thread,5,main]
View Answer
Answer: c
Explanation: The getName() function is used to obtain the name of the thread, in this code the
name given to thread is ‘New Thread’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread
34. What is the name of the thread in output in the following Java program?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. System.out.println(t.getPriority());
7. }
8. }
a) 0
b) 1
c) 4
d) 5
View Answer
Answer: d
Explanation: The default priority given to a thread is 5.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
35. What is the name of the thread in output in the following Java program?
1. class multithreaded_programing
2. {
3. public static void main(String args[])
4. {
5. Thread t = Thread.currentThread();
6. System.out.println(t.isAlive());
7. }
8. }
a) 0
b) 1
c) true
d) false
View Answer
Answer: c
Explanation: Thread t is seeded to currently program, hence when you run the program the
thread becomes active & code ‘t.isAlive’ returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
36. Which of these method is used to implement Runnable interface?
a) stop()
b) run()
c) runThread()
d) stopThread()
View Answer
Answer: b
Explanation: To implement Runnable interface, a class needs only to implement a single method
called run().
37. Which of these method is used to begin the execution of a thread?
a) run()
b) start()
c) runThread()
d) startThread()
View Answer
Answer: b
Explanation: None.
38. Which of these statement is incorrect?
a) A thread can be formed by implementing Runnable interface only
b) A thread can be formed by a class that extends Thread class
c) start() method is used to begin execution of the thread
d) run() method is used to begin execution of a thread before start() method in special cases
View Answer
Answer: d
Explanation: run() method is used to define the code that constitutes the new thread, it contains
the code to be executed. start() method is used to begin execution of the thread that is execution
of run(). run() itself is never used for starting execution of the thread.
39. What will be the output of the following Java code?
1. class newthread implements Runnable
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"My Thread");
7. t.start();
8. }
9. public void run()
10. {
11. System.out.println(t.getName());
12. }
13. }
14. class multithreaded_programing
15. {
16. public static void main(String args[])
17. {
18. new newthread();
19. }
20. }
a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer
Answer: a
Explanation: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
My Thread
40. What will be the output of the following Java code?
1. class newthread implements Runnable
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"My Thread");
7. t.start();
8. }
9. public void run()
10. {
11. System.out.println(t);
12. }
13. }
14. class multithreaded_programing
15. {
16. public static void main(String args[])
17. {
18. new newthread();
19. }
20. }
a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer
Answer: b
Explanation: None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main]
41. What will be the output of the following Java code?
1. class newthread implements Runnable
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"My Thread");
7. t.start();
8. }
9. }
10. class multithreaded_programing
11. {
12. public static void main(String args[])
13. {
14. new newthread();
15. }
16. }
a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer
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.
Output:
$ javac multithreaded_programing.java
The type newthread must implement the inherited abstract method
Runnable.run()
42. What will be the output of the following Java code?
1. class newthread implements Runnable
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"New Thread");
7. t.start();
8. }
9. public void run()
10. {
11. t.setPriority(Thread.MAX_PRIORITY);
12. System.out.println(t);
13. }
14. }
15. class multithreaded_programing
16. {
17. public static void main(String args[])
18. {
19. new newthread();
20. }
21. }
a) Thread[New Thread,0,main]
b) Thread[New Thread,1,main]
c) Thread[New Thread,5,main]
d) Thread[New Thread,10,main]
View Answer
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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,10,main]
43. What will be the output of the following Java code?
1. class newthread implements Runnable
2. {
3. Thread t;
4. newthread()
5. {
6. t1 = new Thread(this,"Thread_1");
7. t2 = new Thread(this,"Thread_2");
8. t1.start();
9. t2.start();
10. }
11. public void run()
12. {
13. t2.setPriority(Thread.MAX_PRIORITY);
14. System.out.print(t1.equals(t2));
15. }
16. }
17. class multithreaded_programing
18. {
19. public static void main(String args[])
20. {
21. new newthread();
22. }
23. }
a) true
b) false
c) truetrue
d) falsefalse
View Answer
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.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
44. Which of this method can be used to make the main thread to be executed last among all the
threads?
a) stop()
b) sleep()
c) join()
d) call()
View Answer
Answer: b
Explanation: By calling sleep() within main(), with long enough delay to ensure that all child
threads terminate prior to the main thread.
45. Which of this method is used to find out that a thread is still running or not?
a) run()
b) Alive()
c) isAlive()
d) checkRun()
View Answer
Answer: c
Explanation: The isAlive( ) method returns true if the thread upon which it is called is still
running. It returns false otherwise.
46. What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY?
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256
View Answer
Answer: c
Explanation: None.
47. Which of these method waits for the thread to terminate?
a) sleep()
b) isAlive()
c) join()
d) stop()
View Answer
Answer: c
Explanation: None.
48. Which of these method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()
View Answer
Answer: c
Explanation: The default value of priority given to a thread is 5 but we can explicitly change that
value between the permitted values 1 & 10, this is done by using the method setPriority().
49. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need access to a shared
resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information require
View Answer
Answer: a
Explanation: When two or more threads need to access the same shared resource, they need some
way to ensure that the resource will be used by only one thread at a time, the process by which
this is achieved is called synchronization
50. What will be the output of the following Java code?
1. class newthread extends Thread
2. {
3. newthread()
4. {
5. super("My Thread");
6. start();
7. }
8. public void run()
9. {
10. System.out.println(this);
11. }
12. }
13. class multithreaded_programing
14. {
15. public static void main(String args[])
16. {
17. new newthread();
18. }
19. }
a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error
View Answer
Answer: b
Explanation: Although we have not created any object of thread class still we can make a thread
pointing to main method, we can refer it by using this.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main].
51. What will be the output of the following Java code?
1. class newthread extends Thread
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"My Thread");
7. t.start();
8. }
9. public void run()
10. {
11. try
12. {
13. t.join()
14. System.out.println(t.getName());
15. }
16. catch(Exception e)
17. {
18. System.out.print("Exception");
19. }
20. }
21. }
22. class multithreaded_programing
23. {
24. public static void main(String args[])
25. {
26. new newthread();
27. }
28. }
a) My Thread
b) Thread[My Thread,5,main]
c) Exception
d) Runtime Error
View Answer
Answer: d
Explanation: join() method of Thread class waits for thread being called to finish or terminate,
but here we have no condition which can terminate the thread, hence code ‘t.join()’ leads to
runtime error and nothing will be printed on the screen.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
52. What will be the output of the following Java code?
1. class newthread extends Thread
2. {
3. Thread t;
4. newthread()
5. {
6. t = new Thread(this,"New Thread");
7. t.start();
8. }
9. public void run()
10. {
11. System.out.println(t.isAlive());
12. }
13. }
14. class multithreaded_programing
15. {
16. public static void main(String args[])
17. {
18. new newthread();
19. }
20. }
a) 0
b) 1
c) true
d) false
View Answer
Answer: c
Explanation: isAlive() method is used to check whether the thread being called is running or not,
here thread is the main() method which is running till the program is terminated hence it returns
true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
True
53. What will be the output of the following Java code?
1. class newthread extends Thread
2. {
3. Thread t1,t2;
4. newthread()
5. {
6. t1 = new Thread(this,"Thread_1");
7. t2 = new Thread(this,"Thread_2");
8. t1.start();
9. t2.start();
10. }
11. public void run()
12. {
13. t2.setPriority(Thread.MAX_PRIORITY);
14. System.out.print(t1.equals(t2));
15. }
16. }
17. class multithreaded_programing
18. {
19. public static void main(String args[])
20. {
21. new newthread();
22. }
23. }
a) true
b) false
c) truetrue
d) falsefalse
View Answer
Answer: d
Explanation: This program was previously done by using Runnable interface, here we have used
Thread class. This shows both the method are equivalent, we can use any of them to create a
thread.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse
54. 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
Ans : c
55. Which of the following constructor of class Thread is valid one?
Thread(Runnable threadOb, int priority)
Thread(int priority)
Thread(Runnable threadOb, String threadName)
Thread(String threadName, int priority)
None of these
56. Analyze the following code:
public abstract class Test implements Runnable{
public void doSomething() { };
}
a)The program will not compile because it does not implement the run() method.
b)The program will not compile because it does not contain abstract methods.
c)The program compiles fine.
d)None of the above
Answer : c
57. 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.
Answer : a
58. 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");
}
}
a) The program has a compilation error because t is defined in both the main()
method and the constructor Test().
b) The program compiles fine, but it does not run because you cannot use the
keyword this in the constructor.
c) The program compiles and runs and displays nothing.
d) The program compiles and runs and displays test.
Answer : d
59.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
Answer : C
60. 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");
}
}
a) run-test run-test
b) run-test
c) Compilation fails due to an error on line 4
d) Compilation fails due to an error on line 7
Answer : a
61.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
Answer : a
62.What notifyAll() method do?
a) Wakes up one threads that are waiting on this object's monitor
b) Wakes up all threads that are not waiting on this object's monitor
c) Wakes up all threads that are waiting on this object's monitor
d) None of the above
Answer : c
63. Which keyword when applied on a method indicates that only one thread
should execute the method at a time.
a) volatile
b) synchronized
c) native
d) static
Answer : b
64. 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.
Answer : c
65. What will be output of the following program code?
public class Test implements Runnable{
public void run(){
System.out.print("go");}
public static void main(String arg[]) {
Thread t = new Thread(new Test());
t.run();
t.run();
t.start();}}
a) Compilation fails.
b) An exception is thrown at runtime.
c) "go" is printed
d) "gogogo" is printed
Answer : d
No comments:
Post a Comment