Page: 2
9. Multi-Threading
Which statement is true? A. Compilation fails. B. An exception is thrown at runtime. C. Synchronizing the run() method would make the class thread-safe. D. The data in variable "x" are protected from concurrent access problems. E. Declaring the doThings() method as static would make the class thread-safe. F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe. Answer: E Q: 04 Given: 1. public class Threads3 implements Runnable { 2. public void run() { 3. System.out.print("running"); 4. } 5. public static void main(String[] args) { 6. Thread t = new Thread(new Threads3()); 7. t.run(); 8. t.run(); 9. t.start(); 10. } 11. } What is the result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes and prints "running". D. The code executes and prints "runningrunning". E. The code executes and prints "runningrunningrunning". Answer: E Q: 05 Given: public class NamedCounter { private final String name; private int count; public NamedCounter(String name) { this.name = name; } public String getName() { return name; } public void increment() { count++; } public int getCount() { return count; } public void reset() { count = 0; } } Which three changes should be made to adapt this class to be used safely by multiple threads? (Choose three.) A. declare reset() using the synchronized keyword B. declare getName() using the synchronized keyword C. declare getCount() using the synchronized keyword D. declare the constructor using the synchronized keyword E. declare increment() using the synchronized keyword Answer: A, C, E Q: 06 Given: 7. void waitForSignal() { 8. Object obj = new Object(); 9. synchronized (Thread.currentThread()) { 10. obj.wait(); 11. obj.notify(); 12. } 13. } Which statement is true? A. This code may throw an InterruptedException. B. This code may throw an IllegalStateException. C. This code may throw a TimeoutException after ten minutes. D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()". E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally. F. A call to notify()or notifyAll() from another thread may cause this method to complete normally. Answer: B Q: 07 Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.) A. new Thread() { public void run() { doStuff(); } }; B. new Thread() { public void start() { doStuff(); } }; C. new Thread() { public void start() { doStuff(); } }.run(); D. new Thread() { public void run() { doStuff(); } }.start(); E. new Thread(new Runnable() { public void run() { doStuff(); } }).run(); F. new Thread(new Runnable() { public void run() { doStuff(); } }).start(); Answer: D, F Q: 08 Given: 1. public class TestOne implements Runnable { 2. public static void main (String[] args) throws Exception { 3. Thread t = new Thread(new TestOne()); 4. t.start(); 5. System.out.print("Started"); 6. t.join(); 7. System.out.print("Complete"); 8. } 9. public void run() { 10. for (int i = 0; i < 4; i++) { 11. System.out.print(i); 12. } 13. } 14. } What can be a result? A. Compilation fails. B. An exception is thrown at runtime. C. The code executes and prints "StartedComplete". D. The code executes and prints "StartedComplete0123". E. The code executes and prints "Started0123Complete". Answer: E
Page: 2
1
2
3
4
5
6
7
8
9
10
|