A timeout argument can be passed to which of the following methods?
1. wait()
2. notify()
3. sleep()
4. join()
5. yield()
6. notifyAll()
Answer:wait(),sleep(),join()
wait(long timeout)-Causes current thread to wait until either another thread invokes the notify() method orthe notifyAll() method for this object, or a specified amount of time has elapsed.
join(long millis)-It tellsthe current thread to wait until either the other thread to complete or time has elapsed.
sleep(long millis)-causes the current thread to wait for a specified amount of time.
2)
Which kind of variable would you prefer to synchronize on?
- A member variable of a primitive type
- A member variable that is an object reference
- A method local variable that is a reference to an instance that is created within the method
- None of the above
Answer:A member variable that is an object reference
Primitives don't have locks; therefore, they can not be used to synchronize threads. A method local variablethat is a reference to an instance that is created within the method should not be used to synchronize threads,because each thread has its own instance of the object and lock.
3)
Which of the following instance methods should only be called by a thread that holds the lock of the instance on which the method is invoked?
1. join
2. notify
3. notifyAll
4. run
5. wait
Answer:wait(),notify(),notifyAll()
wait(),notify(),notifyAll() declared in java.lang.Object should be called within a synchronized block or synchronized function.
4)
Which of the following is a checked exception?
1. IllegalMonitorStateException
2. IllegalThreadStateException
3. IllegalArgumentException
4. InterruptedException
5, None of the above
Answer:InterruptedException
wait(),sleep() and join() have throws clause for java.lang.InterrupedException.So all these functions should put in a try-catch block ,else compile time error .
5)
Which of the following is a true statement?
- The process of executing a synchronized method requires the thread to acquire a lock.
- Any overriding method of a synchronized method is implicitly synchronized.
- If any method in a class is synchronized, then the class itself must also be declared using the synchronized modifier.
- If a thread invokes a static synchronized method on an instance of class A, then the thread must acquire the lock of that instance of class A.
Answer:The process of executing a synchronized method requires the thread to acquire a lock.
The synchronized modifier can not be applied to a class. A method that overrides a synchronized method does not have to be synchronized. If a thread invokes a synchronized instance method on an instance of class A, then the
thread must acquire the lock of that instance of class A. The same is not true for synchronized static methods.A synchronized static method is synchronized on the lock for the Class object that represents the class for which the method is a member.
6)
Which of the following are true statements?
1. program will terminate only when all daemon threads stop running
2. A program will terminate only when all user threads stop running
3. A daemon thread always runs at Thread.MIN_PRIORITY
4. A thread inherits its daemon status from the thread that created it
5. daemon status of a thread can be changed at any time using the Thread.setDaemon method
Answer:2 & 4
We can set the Daemon status only before starting the thread else it will generate java.lang.IllegalThreadState Exception.We can change the daemon thread priority at any time.
7)
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A(); a.run(); System.out.print(a.i);
}}
What is the result of attempting to compile and run the program?
- Prints Nothing
- Prints 0 or 1
- Prints 0
- Prints 1
Answer:Prints 1
The a.run() method was called instead of a.start(); so the entire program runs as a single thread, and a.run() is guaranteed to complete before the print statement is called.
8)
class AnException extends Exception {}
class A extends Thread {
public void run() throws AnException {
System.out.print("A"); throw new AnException();
}}
class B {
public static void main (String[] args) {
A a = new A(); a.start(); System.out.print("B");
}}
What is the result of attempting to compile and run the program?
- Prints A
- Prints B
- Prints AB
- Compile-time error
Answer:Compile-time error
The Runnable.run method does not have a throws clause; so any implementation of run can not throw a checked exception.
9)
class A extends Thread { String[] sa;
public A(String[] sa) {this.sa = sa;}
public void run() {synchronized (sa) {System.out.print(sa[0] + sa[1] + sa[2]);}}}
class B { private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {Thread t1 = new A(sa); t1.start();
sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}}}
What is the result of attempting to compile and run the program?
- Prints ABC
- Prints XYZ
- Compile-time error
- Run-time error
Answer:Prints ABC
The block inside the main method is synchronized on the String array object sa. Inside the block, a new thread t1 is started and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa. Thread t1 will continue to block until the synchronized block in the B.main method runs to completion. At that time, the contents of the String array object have all been updated.
10)
class A extends Thread {
public void run() {System.out.print("A");}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
a.start(); // 1
}
}
What is the result of attempting to compile and run the program?
- The program compiles and runs without error
- The second attempt to start thread t1 is successful
- Compile-time error at marker 1
- An IllegalThreadStateException is thrown at run-time
Answer: An IllegalThreadStateException is thrown at run-time
Invoking the start method on a thread that has already been started will generate an IllegalThreadStateException.
11)
Which of the following is used to force each thread to reconcile its working copy of a variable with the master copy in main memory?
- synchronized
- transient
- volatile
- native
Answer :volatile
A field might be shared between two or more threads. Each thread is allowed to maintain a working copy of the field. If the threads do not reconcile the working copies then each might be working with a different value.The volatile modifier is used to force each thread to reconcile its working copy of the field with the master copy in main memory.
No comments:
Post a Comment