Sail E0 Webinar

MCQs

Total Questions : 86 | Page 4 of 9 pages
Question 31.


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 */
}
}
}
  1.    Compilation error.
  2.    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.
  3.    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.
  4.    Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
 Discuss Question
Answer: Option 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.

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);


Question 32.


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 + "..");
}
}
}
  1.    Compilation fails.
  2.    1..2..3..
  3.    0..1..2..3..
  4.    0..1..2..
 Discuss Question
Answer: Option D. -> 0..1..2..

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.


Question 33.
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?


  1.    Each String in the array lines will output, with a 1-second pause.
  2.    Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
  3.    Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
  4.    This code will not compile.
 Discuss Question
Answer: Option D. -> This code will not compile.

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.


Question 34.


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();
}
}
  1.    ABBCAD
  2.    ABCBCAD
  3.    CDADACB
  4.    Output determined by the underlying platform.
 Discuss Question
Answer: Option D. -> Output determined by the underlying platform.

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.


Question 35.


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();
}
}
  1.    It will print the numbers 0 to 19 sequentially
  2.    It will print the numbers 1 to 20 sequentially
  3.    It will print the numbers 1 to 20, but the order cannot be determined
  4.    The code will not compile.
 Discuss Question
Answer: Option B. -> It will print the numbers 1 to 20 sequentially

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.


Question 36.


What will be the output of the program?


public class WaitTest
{
public static void main(String [] args)
{
System.out.print("1 ");
synchronized(args)
{
System.out.print("2 ");
try
{
args.wait(); /* Line 11 */
}
catch(InterruptedException e){ }
}
System.out.print("3 ");
}
}
  1.    It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11.
  2.    1 2 3
  3.    1 3
  4.    1 2
 Discuss Question
Answer: Option D. -> 1 2

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.


Question 37.


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;
}
}
  1.    Synchronize the run method.
  2.    Wrap a synchronize(this) around the call to f.increase().
  3.    The existing code will cause a runtime exception.
  4.    Synchronize the increase() method
 Discuss Question
Answer: Option D. -> Synchronize the increase() method

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.


Question 38.


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();
}
}
  1.    DeadLock
  2.    It print 12 12 12 12
  3.    Compilation Error
  4.    Cannot determine output.
 Discuss Question
Answer: Option B. -> It print 12 12 12 12

The program will execute without any problems and print 12 12 12 12.

Question 39.


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();
}
}
  1.    Compile time Error: There is no start() method
  2.    Will print in this order: 1 1 2 2 3 3 4 4 5 5...
  3.    Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
  4.    Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
 Discuss Question
Answer: Option C. -> Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)


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()
);
}


Question 40.
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}


which of these will create and start this thread?


  1.    new Runnable(MyRunnable).start();
  2.    new Thread(MyRunnable).run();
  3.    new Thread(new MyRunnable()).start();
  4.    new MyRunnable().start();
 Discuss Question
Answer: Option C. -> new Thread(new MyRunnable()).start();

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.


Latest Videos

Latest Test Papers