|
1. Which of the following are valid ways of creating and starting a thread?
A. new Runnable().start();
B. Runnable r = new Runnable();
new Thread(r).run();
C. Runnable r = new Runnable() { public void run() {}};
new Thread(r).start();
D. new Thread(new Runnable()).start();
E. Thread t = new Thread();
t.run();
2. Consider the following code.
1. public class MyRunnable implements Runnable {
2. public void run() {
3. System.out.println(“Just checking…”);
4. }
5. }
Which of the following can print the message “Just checking…” after execution?
A. Thread t = new Thread(new MyRunnable()); t.start();
B. Runnable r = new MyRunnable(); r.start();
C. Runnable r = new MyRunnable(); r.run();
D. Thread t = new Thread(new MyRunnable()); t.run();
E. new MyRunnable().run();
3. Consider the following code that creates a thread object using a runnable object.
Runnable r = new MyRunnable();
Thread t = new Thread(r);
Which of the following can be the valid definitions for class MyRunnable for the preceding code to compile successfully?
A. public class MyRunnable extends Runnable { public void run() {} }
B. public class MyRunnable implements Runnable { public int run(){} }
C. public class MyRunnable implements Runnable { protected void run(){} }
D. public class MyRunnable extends Thread implements Runnable { public void start(){} }
E. public class MyRunnable implements Runnable { public void run(){} }
F. public class MyRunnable implements Runnable { public void run();}
4. Consider the following code.
1. package com.example.threads;
2.
3. public class MyThread extends Thread {
4. public void run() {
5. for (int i = 0; i < 5; i++) {
6. System.out.println("Running 1...");
7. }
8. }
9.
10. public void run(int n) {
11. for (int i = 0; i < n; i++) {
12. System.out.println("Running 2...");
13. }
14. }
15.
16. public static void main(String[] args) {
17. MyThread t1 = new MyThread();
18. t1.run();
19. }
20. }
What is the result of executing the Java application MyThread?
A. “Running 1...” is printed five times followed by “Running 2...” five times.
B. “Running 1...” is printed five times.
C. This code will fail to compile at line 10.
D. This code will fail to compile at line 18.
E. The output will have “Running 1...” five times and “Running 2...” five times. However, there order cannot be predicted.
F. This class will compile but will fail to run because the thread is not started before invoking run().
5. Which of the following statements true about the given code? (Choose all the correct answers)
1. package com.example.threads;
2. public class TopPriorityJob extends Thread {
3. public void run() {
4. System.out.println("Doing something very important");
5. while(true) {
6. //hog the CPU
7. }
8. }
9. public static void main(String[] args) {
10. TopPriorityJob ob1 = new TopPriorityJob();
11. TopPriorityJob job2 = new TopPriorityJob();
12. TopPriorityJob job3 =j new TopPriorityJob();
13. job1.setPriority(10);
14. job2.setPriority(8);
15. job3.setPriority(6);
16. job1.start();
17. job2.start();
18. job3.start();
19. }
20. }
A. When this Java application runs, only the thread job1 will ever get a chance to execute. job2 and job3 will never get a chance to execute.
B. This code will not compile as the setPriority() can be called on only the currently executing thread.
C. When this Java application runs, job2 or job3 may get a chance to execute if they are started before the thread job1.
D. Each of these three threads will take time slots for execution and will give up their turn of execution after a specified time. Since job1 has highest priority, it gets to run longest.
E. How the three threads will execute cannot be said with a guarantee. It depends on the implementation of thread scheduler and JVM.
6. Consider the following classes.
1. package com.example.threads;
2.
3. class BullockCart implements Runnable {
4. public void run() {
5. int i= 0;
6. while(i++ < 50) {
7. System.out.println("going really slow");
8. }
9. }
10. }
11.
12. public class Jetliner implements Runnable {
13. Thread jetlinerThread ;
14. Thread bullockCartThread;
15.
16. public void run() {
17. System.out.println("going really fast");
18. bullockCartThread.yield();
19. System.out.println("done");
20. }
21.
22. public static void main(String[] args) {
23. Jetliner j = new Jetliner();
24. j.jetlinerThread = new Thread(j);
25. j.bullockCartThread = new Thread(new BullockCart());
26. j.jetlinerThread.start();
27. j.bullockCartThread.start();
28. }
29. }
Which of the following statements are true when application Jetliner is executed?
A. The message “going really fast” may not be immediately followed by “done” in the output due to the call at line 16.
B. The call at line 16 makes sure that the message “going really fast” will be always immediately followed by “done” in the output.
C. This code will not compile as you cannot call bullockCartThread.yield() in jetlinerThread’s run method.
D. Once bullockCartThread starts executing, it will give up its turn only after printing the message “going really slow” 50 times.
E. Since jetlinerThread is started first, it is guaranteed to finish before the bullockCartThread.
7. Consider the following class.
1. public class Garfield implements Runnable {
2. public void run() {
3. try {
4. Thread.sleep(3000);
5. } catch (InterruptedException e) {}
6. System.out.println("Good morning everyone!");
7. }
8.
9. public static void main(String[] args) {
10. Garfield g = new Garfield();
11. new Thread(g).start();
12. }
13. }
Assuming the thread is never interrupted, which of the following statements are true about the Garfield class?
A. This application will fail to compile at line 10.
B. Whenever new thread started on g executes, its execution will stop at line 4 for at most 3 seconds and then message “Good morning everyone!” is printed.
C. Whenever new thread started on g executes, its execution will stop at line 4 and exactly after 3 seconds the message “Good morning everyone!” is printed.
D. Whenever new thread started on g executes, its execution will stop at line 4 for at least 3 seconds. Sometime after that the message “Good morning everyone!” is printed.
E. The message “Good morning everyone!” is never printed because the new thread started on g will not wake up unless it is interrupted.
8. A Java program creates three threads t1, t2 and t3. They all work on a common object someObject. After sometime during their execution, they all are waiting for notification in someObject’s waiting pool. However, someObject wants to notify only t1 and t3 but not t2. How it can be done?
14. t1.notify(); t3.notify();
15. t1.notify(); t3.notify(); t2.wait();
16. someObject.notify(t1); someObject.notify(t3);
17. someObject.notifyAll();
18. someObject.notifyAll(t1,t3);
19. It is impossible to do so.
9. Consider the following MainThread class.
1. package com.example.threads;
2. public class MainThread extends Thread {
3. private int i;
4. public static void main(String [] args) {
5. MainThread t1 = new MainThread();
6. MainThread t2 = new MainThread();
7. t1.printIt();
8. t2.printIt();
9. t1.start();
10. t2.start();
11. }
12. public void run() {
13. System.out.print(i++ + " ");
14. }
15. public void printIt() {
16. System.out.print(i++ + " ");
17. }
18. }
What will be printed when MainThread is executed?
A. 0 1 2 3
B. 1 0 3 2
C. 0 0 1 1
D. 0 1 0 1
E. The output cannot be predicted.
10. Which of the following calls on object myObj invoke the methods of Object class?
A. myObj.notify();
B. myObj.synchronized();
C. myObj.yield();
D. myObj.wait(1000);
E. myObj.wait(1000,0);
F. myObj.sleep(1000);
G. myObj.notifyAll();
11. Which of the following methods can be invoked on the object of type Thread?
A. running()
B. start()
C. wait()
D. wakeup()
E. notify()
F. run()
G. waiting()
12. Which of the following statements are true about synchronization?
A. A Java object has multiple locks. Each thread executing a synchronized code on an object acquires one from the object.
B. When a thread invokes wait() on object, it immediately releases that object’s lock.
C. If a class has synchronized methods, multiple threads must first acquire a lock on its object before invoking any methods of that class.
D. You must protect the common variables from concurrent access by declaring them using the synchronized keyword.
E. When a thread yields, it automatically goes to sleep until a high priority threads are present in thread pool.
13. Which of the following statements are true about the wait and notify mechanism and methods?
A. The thread waiting for notification holds the lock all the time it is waiting.
B. The thread sleeping for some predefined time holds the lock all the time it is sleeping.
C. To call wait(), an object must own the lock of the thread that is going to wait.
D. The notifyAll() is a static method of Object class.
E. The notify() method causes a thread to immediately release its locks.
F. You need to call the notify() method in synchronized block but not the notifyAll() method.
G. The method notifyAll()notifies all waiting threads regardless of the object they’re waiting on.
14. If a thread t1 calls wait() on an object myObject as myObject.wait(10000); in a properly synchronized code. What will happen to the thread t1 after this call?
A. t1 will start executing as soon as it is notified or exactly after ten seconds.
B. t1 will immediately start executing if the lock on myObject is released within ten seconds.
C. t1 comes out of waiting state and enters the ready-to-run state when either it is notified or when the ten seconds are elapsed since it started waiting.
D. t1 keep waiting until someone calls myObject.notifyAll().
E. Ten seconds after t1 started waiting, it notifies all the waiting threads on someObject.
15. Imagine that we have a GUI application program for editing image files. This application has two threads, one thread ( called the paintThread) is responsible for painting the images and the other (processInputsThread) for handling the user inputs. The paintThread takes a lot of time to paint the image and is extremely slow. On the other hand, the processInputsThread quickly process the user inputs. If the JVM we are using respects thread priorities while thread scheduling, what would be the better way of setting priorities of these threads to make the GUI application responsive to the user inputs even while painting a large image?
A. paintThread.setPriority(Thread.NORM_PRIORITY);
B. paintThread.setPriority(1);
C. paintThread.setPriority(10);
D. paintThread.setPriority(0);
E. paintThread.setPriority(Thread.MIN_PRIORITY);
F. paintThread.setPriority(5);