public class Test107 implements Runnable
{
private int x;
private int y;
public static void main(String args[])
{
Test107 that = new Test107();
(new Thread(that)).start();
(new Thread(that)).start();
}
public synchronized void run()
{
for(int i = 0; i < 10; i++)
{
x++;
y++;
System.out.println("x = " + x + ", y = " + y); /* Line 17 */
}
}
}
A -
Compilation error.
B -
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
C -
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
D -
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
2-
What will be the output of the program?
public class Test
{
public static void main (String [] args)
{
final Foo f = new Foo();
Thread t = new Thread(new Runnable()
{
public void run()
{
f.doStuff();
}
});
Thread g = new Thread()
{
public void run()
{
f.doStuff();
}
};
t.start();
g.start();
}
}
class Foo
{
int x = 5;
public void doStuff()
{
if (x < 10)
{
// nothing to do
try
{
wait();
} catch(InterruptedException ex) { }
}
else
{
System.out.println("x is " + x++);
if (x >= 10)
{
notify();
}
}
}
}
A -
The code will not compile because of an error on notify(); of class Foo.
B -
The code will not compile because of some other error in class Test.
C -
An exception occurs at runtime.
D -
It prints "x is 5 x is 6".
3-
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
Thread x = new Thread(t);
x.start(); /* Line 7 */
}
public void run()
{
for(int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
A -
Compilation fails.
B -
1..2..3..
C -
0..1..2..3..
D -
0..1..2..
4-
Which statement is true?
A -
A static method cannot be synchronized.
B -
If a class has synchronized code, multiple threads can still access the nonsynchronized code.
C -
Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
D -
When a thread sleeps, it releases its locks.
5-
Which two can be used to create a new Thread?
Extend java.lang.Thread and override the run() method.
Extend java.lang.Runnable and override the start() method.
Implement java.lang.Thread and implement the run() method.
Implement java.lang.Runnable and implement the run() method.
Implement java.lang.Thread and implement the start() method.
A -
1 and 2
B -
2 and 3
C -
1 and 4
D -
3 and 4
6-
Which statement is true?
A -
If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
B -
If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
C -
If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
D -
If two threads are blocked in the wait method of one object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
7-
Which two statements are true?
Deadlock will not occur if wait()/notify() is used
A thread will resume execution as soon as its sleep duration expires.
Synchronization can prevent two objects from being accessed by the same thread.
The wait() method is overloaded to accept a duration.
The notify() method is overloaded to accept a duration.
Both wait() and notify() must be called from a synchronized context.
A -
1 and 2
B -
3 and 5
C -
4 and 6
D -
1 and 3
8-
The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
A -
public class MyRunnable extends Runnable{public void run(){}}
B -
public class MyRunnable extends Object{public void run(){}}
C -
public class MyRunnable implements Runnable{public void run(){}}
D -
public class MyRunnable implements Runnable{void run(){}}
9-
Which statement is true?
A -
The notifyAll() method must be called from a synchronized context.
B -
To call wait(), an object must own the lock on the thread.
C -
The notify() method is defined in class java.lang.Thread.
D -
The notify() method causes a thread to immediately release its locks.
10-
void start() {
A a = new A();
B b = new B();
a.s(b);
b = null; /* Line 5 */
a = null; /* Line 6 */
System.out.println("start completed"); /* Line 7 */
}
When is the B object, created in line 3, eligible for garbage collection?