Sie sind auf Seite 1von 19

Internal Exam, J2SE 1.

5 Multithreading
Max Marks: 42 Convener: Pankaj Chadha Important Instructions This Question Paper contains a total of 42 questions. All questions are mandatory. All the questions are objective type and may have more than one option correct. Each question carries 1 mark. There is no negative marking. An answer to the question is considered to be correct only if all correct option(s) are chosen by student. Students need to score minimum 50% marks to pass this exam. Max Time: 4 Hrs.

Question 1.Which is the correct way to start a new thread? a. b. c. d. e. Just create a Create a new Create a new Create a new Create a new new Thread object. The thread will start automatically. Thread object and call the method begin(). Thread object and call the method start(). Thread object and call the method run(). Thread object and call the method resume().

Question 2. When extending the Thread class to provide a thread's behavior, which method should be overridden? A) begin() B) start() C) run() D) resume() E) behavior()

Question 3. Which statements are true? a. The class Thread is abstract. b. The class Thread implements Runnable.

c. Classes implementing the Runnable interface must define a method named start. d. Calling the method run() on an object implementing Runnable will create a new thread. e. A program terminates when the last non-daemon thread ends.

Question 4. What will be the result of attempting to compile and run the following program? public class MyClass extends Thread { public MyClass(String s) { msg = s; } String msg; public void run() { System.out.println(msg); } public static void main(String[] args) { new MyClass("Hello"); new MyClass("World"); } } a. The program will fail to compile. b. The program will compile without errors and will print Hello and World, in that order, every time the program is run. c. The program will compile without errors and will print a never-ending stream of Hello and World. d. The program will compile without errors and will print Hello and World when run, but the order is unpredictable. e. The program will compile without errors and will simply terminate without any output when run.

Question 5. What will be the result of attempting to compile and run the following program?

public class WaitTest { public static void main(String [] args) { System.out.print("1 ") ; synchronized(args) { System.out.print("2 " ) ; try { args.wait(); } catch(InterruptedException e){} } System.out.print("3 "); } } A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt in try-catch block B. 1 2 3 C. 1 3 D. 1 2 E. At runtime, it throws an IllegalMonitorStateException when trying to wait. F. It will fail to compile because it has to be synchronized on the this object.

Question 6. Which statement is true? A) No two threads can concurrently execute synchronized methods on the same object. B) Methods declared synchronized should not be recursive, since the object lock will not allow new invocations of the method. C) Synchronized methods can only call other synchronized methods directly. D) Inside a synchronized method, one can assume that no other threads are currently executing any other methods in the same class.

Question 7. Given the following program, which statement is true?

public class MyClass extends Thread { static Object lock1 = new Object(); static Object lock2 = new Object(); static volatile int i1, i2, j1, j2, k1, k2; public void run() { while (true) { doit(); check(); } } void doit() { synchronized(lock1) { i1++; } j1++; synchronized(lock2) { k1++; k2++; } j2++; synchronized(lock1) { i2++; } } void check() { if (i1 != i2) System.out.println("i"); if (j1 != j2) System.out.println("j"); if (k1 != k2) System.out.println("k"); } public static void main(String[] args) { new MyClass().start();

new MyClass().start(); } } A) The program will fail to compile. B) One cannot be certain whether any of the letters i, j, and k will be printed during execution. C) One can be certain that none of the letters i, j, and k will ever be printed during execution. D) One can be certain that the letters i and k will never be printed during execution. E) One can be certain that the letter k will never be printed during execution.

Question 8. Which one of these events will cause a thread to die? A) The method sleep() is called. B) The method wait() is called. C) Execution of the start() method ends. D) Execution of the run() method ends. E) Execution of the thread's constructor ends.

Question 9. Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU? A. B. C. D. After object B is notified, or after two seconds. After the lock on B is released, or after two seconds. Two seconds after object B is notified. Two seconds after lock B is released.

Question 10. What can be guaranteed by calling the method yield()? A) All lower priority threads will be granted CPU time. B) The current thread will sleep for some time while some other threads run. C) The current thread will not continue until other threads have terminated.

D) The thread will wait until it is notified. E) None of the above.

Question 11. Where is the notify() method defined? a. Thread b. Object c. Applet d. Runnable

Question 12. How can the priority of a thread be set? a. By using the setPriority() method in the class Thread. b. By passing the priority as a parameter to the constructor of the thread. c. Both of the above. d. None of the above.

Question 13. Which statements are true about locks? a. A thread can hold more than one lock at a time. b. Invoking wait() on a Thread object will relinquish all locks held by the thread. c. Invoking wait() on an object whose lock is held by the current thread will relinquish the lock. d. Invoking notify() on a object whose lock is held by the current thread will relinquish the lock. e. Multiple threads can hold the same lock at the same time.

Question 14. What will be the result of invoking the wait() method on an object without ensuring that the current thread holds the lock of the object? a. The code will fail to compile. b. Nothing special will happen. c. An IllegalMonitorStateException will be thrown if the wait() method is called while the current thread does not hold the lock of the object. d. The thread will be blocked until it gains the lock of the object.

Question 15. Which of these are plausible reasons why a thread might be alive, but still not be running? a. The thread is waiting for some condition as a result of a wait() call. b. The execution has reached the end of the run() method. c. The thread is waiting to acquire the lock of an object in order to execute a certain method on that object. d. The thread does not have the highest priority and is currently not executing. e. The thread is sleeping as a result of a call to the sleep() method.

Question 16. Where is the Thread class defined? A) java.lang B) java.util C) java.io D) java.net E) java.thread

Question 17. Where is the Runnable interface defined? A) java.lang B) java.util C) java.io D) java.net

Question 18. What is true about Threads? A) A thread is independent sequential path of execution within a program. B) Threads make the runtime environment asynchronous, allowing different tasks to be performed concurrently. C) A thread is an execution context, which is composed of three main parts; a virtual CPU, the code that CPU executes, and the data on which the code works. D) Code can be shared by multiple threads, independent of data.

E) Data can be shared by multiple threads, independent of code

Question 19. What is true about Thread states? A) A thread acquires runnable state if the thread was preempted by a higher priority thread. B) A thread acquires runnable state if the thread is newly created. C) A thread acquires runnable state if the thread is newly created and its start() method is called. D) A thread acquires running state only if the thread was in runnable state. E) Only a running thread goes to blocked (suspended) state. F) A thread upon reaching the dead state can be run again. G) A thread can be present in an un-known state. H) A thread acquires blocked state if its yield() method is called. I) A thread acquires blocked state if its sleep() method is called.

Question 20. Given the following code, what is the result of this code? class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); t.start() ; System.out.print("one "); t.start(); System.out.print("two "); } public void run() { System.out.print("Thread "); } }

A. B. C. D.

Compilation fails. An exception occurs at runtime. Thread one Thread two The output cannot be determined.

Question 21. What will be the result of attempting to compile and run the following program? public class Thread1 extends Thread { public static void main(String... ar) throws Exception { System.out.print(" In-Main "); Thread t1=new Thread(new Thread1()); t1.start(); t1.join(); Thread t2=new Thread(new Thread1()); t2.start(); t2.join(); for(int i=0;i<1;i++) { System.out.print(" Bye-Main } } public void run() { System.out.print(" In-Run "); for(int i=0;i<1;i++) { System.out.print(" Running } }

");

");

} A) Compilation fails. B) An exception occurs at runtime.

C) In-Main In-Run Running In-Run Bye-Main Running D) In-Main In-Run Running Bye-Main In-Run Running E) In-Main In-Run Running In-Run Running Bye-Main

Question 22. What will be the result of attempting to compile and run the following program?

public class Thread2 { public static void main(String... ar) throws Exception { System.out.print(" In-Main "); Thread t1=new Thread(new Thread2()); t1.start(); t1.join(); for(int i=0;i<1;i++) { System.out.print(" Bye-Main } } public void run() { System.out.print(" In-Run "); for(int i=0;i<1;i++) { System.out.print(" Running } }

");

");

A. B. C. D. E. F.

Compilation fails. An exception occurs at runtime. In-Main In-Run Running In-Run Bye-Main Running In-Main In-Run Running Bye-Main In-Run Running In-Main In-Run Running Bye-Main None of the above

Question 23. What will be the result of attempting to compile and run the following program? public class Messager implements Runnable { public static void main(String[] args) { new Thread(new Messager("Wallace")).start() ; new Thread(new Messager("Gromit")).start(); } private String name;

public Messager(String name) { this.name = name; } public void run() { message(1); message(2); } private synchronized void message(int n) { System.out.print(name + "-" + n + " "); } } What can be the nearest possible answer (as answer may vary for every execution)

A. B. C. D. E.

Wallace-1 Wallace-2 Gromit-1 Wallace-1 Gromit-2 Wallace-2 Gromit-1 Wallace-1 Gromit-1 Gromit-2 Wallace-2 Gromit-1 Gromit-2 Gromit-2 Wallace-1 Gromit-1 Wallace-2

F. The code does not compile. G. An error occurs at run time.

Question 24. We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any other combination. Which of the following method definitions could be added to the Letters class to make this guarantee? public class Letters extends Thread { private String name; public Letters(String name) { this.name = name; } public void write () { System.out.print(name); System.out.print(name); } public static void main(String[] args) { new Letters("X").start(); new Letters("Y").start(); } }

A. B. C. D. E.

public void run() { write(); } public synchronized void run() { write(); } public static synchronized void run() { write(); } public void run() { synchronized(this) { write(); } } public void run() { synchronized(Letters.class) { write(); } } F. public void run () { synchronized (System.out) { write (); } } G. public void run() { synchronized(System.out.class) { write(); } }

Question 25. Which statement is NOT true? A) yield() method always takes a running thread to blocked state. B) sleep(long ms) method always takes a running thread to blocked state. C) join() method always takes the current thread to blocked state, waiting for termination of the thread on which join() is called. D) wait() method is present in java.lang.Object class E) notify() method is present in java.lang.Object class

Question 26.Which is the correct syntax for implementing the Runnable interface? A) public class <class_name> extends <superclass_name>, Runnable B) public class <class_name> implements Runnable C) public class <class_name> extends Runnable implements <superclass_name> D) public class <class_name> extends <superclass_name> implements Runnable

Question 27. Which of the following is NOT a method used for inter-thread communication? A) B) C) D) wait() notify() sleep() yield()

Question 28. Which of the following is the correct option for starting a thread? A) th1.start(); Thread th1 = new Thread (this); B) Thread th1 = new Thread (this); C) Thread.start(); D) Thread th1 = new Thread(this); th1.start();

Question 29. What is the correct prototype of the wait () method? A) B) C) D) private wait() throws InterruptedException public final void wait() throws InterruptedException public void wait() throws Exception private final void wait() throws InterruptedException.

Question 30. Harry is working in XYZ Ltd as a system administrator and has been assigned a task to keep the record of the memory in use, free memory and total memory available. At the end of each day, he is required to remove all the threads which are no longer required. Which of the following option must be used by him? A) B) C) D) Call to remove() method Call to destroy() method. Assigning kill value to the thread object. Assigning null value to the thread object.

Question 31.Consider the following statements: Statement A: A thread must own the monitor of the object to call notify() and notifyAll() method. Statement B: A thread can become the owner of an object by executing a synchronized instance method of that object. Statement C: sleep() is the only method used to suspend the thread. Statement D: synchronized keyword is used to ensure that no two threads can access the same object simultaneously. A) Statement A, Statement B, and Statement C are True AND Statement D is False. B) Statement A and Statement B are True and Statement C AND Statement D are False. C) Statement A, Statement B and Statement D are True AND Statement C is False. D) Statement Statements C and D are True AND Statement A and Statement B are False. Question 32. Adwick has developed an application implementing inter thread communication in it. However, the threads which were turned to the waiting mode are not brought in action yet. Which of the following method can be used by him to bring those threads in action? A) isAlive() B) resume() C) notify()

D) yield()

Question 33.Which of the following is NOT a valid state of a thread? A) B) C) D) Running Blocked Listening Dead

Question 34.Consider the following statements on threads: Statement A: You can create a new thread by extending the Thread class. Statement B: You can create a new thread by implementing the Runnable interface. Select the correct option for the preceding statements. A) B) C) D) Both Statement A and Statement B are false Statement A is true while Statement B is false. Statement A is false while Statement B is true. Both Statement A and Statement B are true.

Question 35. Select the correct statement that sets the priority of a thread among a group of threads to maximum. A) B) C) D) threadObj.getPriority(THREAD.MAX_PRIORITY); threadObj.setPriority(MAX_PRIORITY); threadObj.setPriority(THREAD.PRIORITY); threadObj.setPriority(THREAD.MAX_PRIORITY);

Question 36. Consider the following code: public class Example extends Thread { public static void main(String args[]) { for(int i=0;i<3;i++) new Example().start(); } public void run() { System.out.println("Hello"); }

} Select the correct option for the preceding code. A) The code will execute with the following output: Hello B) The code will execute without any output. C) The code will execute with the following output: Hello Hello Hello D) The code will not execute.

Question 37. In a Java multithreading program, under what circumstances will you call the yield() method of a thread? A) B) C) D) To To To To give other runnable threads a chance to execute. start a new thread. terminate the current thread. stop the current thread for a specific time.

Question 38.Consider the following code: public class Example extends Thread { public static void main(String argv[]) { Example obj = new Example(); obj.run(); } public void start() { { } } } for (int i = 0; i <5; i++) System.out.println("Hello");

A) The code will display the following output: Hello B) The code will display the following output: Hello Hello Hello Hello Hello C) The code will not display any output. D) The code will display the following output: Hello Hello Hello Hello

Question 39. Consider the following statement: Thread.sleep(x); Select the correct option for the value x. A) An int value that specifies the number of seconds for which the thread must be made inactive. B) A long value that specifies the number of milliseconds for which the thread must be made inactive. C) A float value that specifies the number of seconds for which the thread must be made inactive. D) A float value that specifies the number of milliseconds for which the thread must be made inactive.

Question 40. Consider the following statements: Statement A: To start a thread you must call the constructor of the Thread class. Statement B: The Thread class implements the Runnable interface. Select the correct options for the preceding statements. A) Statement A is true while Statement B is false. B) Both Statement A and Statement B are true. C) Statement A is false while Statement B is true.

D) Both Statement A and Statement B are false.

Question 41. Consider the following code: public class Example { public static void main(String args[]) throws InterruptedException { String info[] = { "Java", "C++" }; for (int i = 0; i < info.length; i++) { Thread.sleep(4000); System.out.println(info[i]); }

} } A) B) C) D) The The The The

code code code code

will will will will

throw an exception of type InterruptedException. display Java and C++. display only Java. display only C++.

Question 42. A Java multithreading program contains an operation() method. You need to ensure that only a single thread can access the operation() method at a time. Which of the following code will you use? A) synchronized(this) public void operation() { /*Method Body*/ } B) public void operation() { synchronized(this) /*Method Body*/ } C) public void synchronized operation() { /*Method Body*/

} D) public synchronized void operation() { /*Method Body*/ }

!! End of Exam !!

Das könnte Ihnen auch gefallen