MCQs
What will be the output of the program?
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 */
}
}
}
Both threads are operating on the same instance variables. Because the code is synchronized
the first thread will complete before the second thread begins. Modify line 17 to print the thread names:
System.out.println(Thread.currentThread().getName() + " x = " + x + ", y = " + y);
The thread MyThread will start and loop three times (from 0 to 2).
Option A is incorrect because the Thread class implements the Runnable interface; therefore,
in line 7, Thread can take an object of type Thread as an argument in the constructor.
Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and
ends with a value of 2.
class Test
{
public static void main(String [] args)
{
printAll(args);
}
public static void printAll(String[] lines)
{
for(int i = 0; i < lines.length; i++)
{
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
the static method Thread.currentThread() returns a reference to the currently
executing Thread object. What is the result of this code?
D. The sleep() method must be enclosed in a try/catch block, or the method printAll() must
declare it throws the InterruptedException.
A is incorrect, but it would be correct if the InterruptedException was dealt with.
B is incorrect, but it would still be incorrect if the InterruptedException was dealt with because
all Java code, including the main() method, runs in threads.
C is incorrect. The sleep() method is static, so even if it is called on an instance, it still always
affects the currently executing thread.
What will be the output of the program?
class Happy extends Thread
{
final StringBuffer sb1 = new StringBuffer();
final StringBuffer sb2 = new StringBuffer();
public static void main(String args[])
{
final Happy h = new Happy();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("A");
h.sb2.append("B");
System.out.println(h.sb1);
System.out.println(h.sb2);
}
}
}.start();
new Thread()
{
public void run()
{
synchronized(this)
{
h.sb1.append("D");
h.sb2.append("C");
System.out.println(h.sb2);
System.out.println(h.sb1);
}
}
}.start();
}
}
Can you guarantee the order in which threads are going to run? No you can't. So how do
you know what the output will be? The output cannot be determined.
What will be the output of the program?
public class ThreadDemo
{
private int count = 1;
public synchronized void doSomething()
{
for (int i = 0; i < 10; i++)
System.out.println(count++);
}
public static void main(String[] args)
{
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
a1.start();
a2.start();
}
}
class A extends Thread
{
ThreadDemo demo;
public A(ThreadDemo td)
{
demo = td;
}
public void run()
{
demo.doSomething();
}
}
You have two different threads that share one reference to a common object.
The updating and output takes place inside synchronized code.
One thread will run to completion printing the numbers 1-10.
The second thread will then run to completion printing the numbers 11-20.
1 and 2 will be printed, but there will be no return from the wait call because no other thread
will notify the main thread, so 3 will never be printed. The program is essentially frozen at line
11.
A is incorrect; IllegalMonitorStateException is an unchecked exception so it doesn't have to be
dealt with explicitly.
B and C are incorrect; 3 will never be printed, since this program will never terminate because
it will wait forever.
What will be the output of the program?
public class SyncTest
{
public static void main (String [] args)
{
Thread t = new Thread()
{
Foo f = new Foo();
public void run()
{
f.increase(20);
}
};
t.start();
}
}
class Foo
{
private int data = 23;
public void increase(int amt)
{
int x = data;
data = x + amt;
}
}
Option D is correct because synchronizing the code that actually does the increase will protect
the code from being accessed by more than one thread at a time.
Option A is incorrect because synchronizing the run() method would stop other threads from
running the run() method (a bad idea) but still would not prevent other threads with other runnables
from accessing the increase() method.
Option B is incorrect for virtually the same reason as Aâ€â€synchronizing the code that calls the
increase() method does not prevent other code from calling the increase() method.
What will be the output of the program?
class s implements Runnable
{
int x, y;
public void run()
{
for(int i = 0; i < 1000; i++)
synchronized(this)
{
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");
}
public static void main(String args[])
{
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}
The program will execute without any problems and print 12 12 12 12.
What will be the output of the program?
class s1 implements Runnable
{
int x = 0, y = 0;
int addX() {x++; return x;}
int addY() {y++; return y;}
public void run() {
for(int i = 0; i < 10; i++)
System.out.println(addX() + " " + addY());
}
public static void main(String args[])
{
s1 run1 = new s1();
s1 run2 = new s1();
Thread t1 = new Thread(run1);
Thread t2 = new Thread(run2);
t1.start();
t2.start();
}
}
Both threads are operating on different sets of instance variables. If you modify the
code of the run() method to print the thread name it will help to clarify the output:
public void run()
{
for(int i = 0; i < 10; i++)
System.out.println(
Thread.currentThread().getName() + ": " + addX() + " " + addY()
);
}
Because the class implements Runnable, an instance of it has to be passed to the Thread
constructor, and then the instance of the Thread has to be started.
A is incorrect. There is no constructor like this for Runnable because Runnable is an interface,
and it is illegal to pass a class or interface name to any constructor.
B is incorrect for the same reason; you can't pass a class or interface name to any constructor.
D is incorrect because MyRunnable doesn't have a start() method, and the only start() method
that can start a thread of execution is the start() in the Thread class.