Welcome Guest | Sign in | Register

Home > Java Programming > Threads > Questions and Answers

01. What will be the output of the program?

class MyThread extends Thread
{
MyThread()
{
System.out.print(" MyThread");
}
public void run()
{
System.out.print(" bar");
}
public void run(String s)
{
System.out.println(" baz");
}
}
public class TestThreads
{
public static void main (String [] args)
{
Thread t = new MyThread()
{
public void run()
{
System.out.println(" foo");
}
};
t.start();
}
}
A. foo B. MyThread foo
C. MyThread bar D. foo bar

Answer and Explanation

Answer: MyThread foo

Explanation:
Option B is correct because in the first line of main we're constructing an instance of an anonymous inner class extending from MyThread. So the MyThread constructor runs and prints "MyThread". The next statement in main invokes start() on the new thread instance, which causes the overridden run() method (the run()method defined in the anonymous inner class) to be invoked, which prints "foo"

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
A. Prints "Inside Thread Inside Thread" B. Prints "Inside Thread Inside Runnable"
C. Does not compile D. Throws exception at runtime

Answer and Explanation

Answer: Prints "Inside Thread Inside Thread"

Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of the Thread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will be the output of the program?
class s1 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("A");
System.out.println("B");
}
}
}
class Test120 extends Thread
{
public void run()
{
for(int i = 0; i < 3; i++)
{
System.out.println("C");
System.out.println("D");
}
}
public static void main(String args[])
{
s1 t1 = new s1();
Test120 t2 = new Test120();
t1.start();
t2.start();
}
}
A. Compile time Error There is no start() method B. Will print in this order AB CD AB...
C. Will print but not be able to predict the Order D. Will print in this order ABCD...ABCD...

Answer and Explanation

Answer: Will print but not be able to predict the Order

Explanation:
We cannot predict the order in which threads are going to run.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What is the name of the method used to start a thread execution?
A. init(); B. start();
C. run(); D. resume();

Answer and Explanation

Answer: start();

Explanation:
Option B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Option A is wrong. There is no init() method in the Thread class.
Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. Which two are valid constructors for Thread? 

1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority) 
A. 1 and 3 B. 2 and 4
C. 1 and 2 D. 2 and 5

Answer and Explanation

Answer: 1 and 2

Explanation:
(1) and (2) are both valid constructors for Thread.
(3), (4), and (5) are not legal Thread constructors, although (4) is close. If you reverse the arguments in (4), you'd have a valid constructor.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Which statement is true?
A. A static method cannot be synchronized. B. If a class has synchronized code, multiple threads can still access the non synchronized 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.

Answer and Explanation

Answer: If a class has synchronized code, multiple threads can still access the non synchronized code.

Explanation:
B is correct because multiple threads are allowed to enter nonsynchronized code, even within a class that has some synchronized methods.
A is incorrect because static methods can be synchronized; they synchronize on the lock on the instance of class java.lang.Class that represents the class type.
C is incorrect because only methods—not variables—can be marked synchronized.
D is incorrect because a sleeping thread still maintains its locks.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Which two statements are true?

1. Deadlock will not occur if wait()/notify() is used
2. A thread will resume execution as soon as its sleep duration expires.
3. Synchronization can prevent two objects from being accessed by the same thread.
4. The wait() method is overloaded to accept a duration.
5. The notify() method is overloaded to accept a duration.
6. 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

Answer and Explanation

Answer: 4 and 6

Explanation:
Statements (4) and (6) are correct. (4) is correct because the wait() method is overloaded to accept a wait duration in milliseconds. If the thread has not been notified by the time the wait duration has elapsed, then the thread will move back to runnable even without having been notified.
(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08.  Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
};

new Thread() {
public void start() { doStuff(); }
};
B. new Thread() {
public void run() { doStuff(); }
}.start();

new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
C. new Thread() {
public void start() { doStuff(); }
}.run();

new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
D. new Thread() {
public void run() { doStuff(); }
}.start();

new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();

Answer and Explanation

Answer: new Thread() {
public void run() { doStuff(); }
}.start();


new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. public class Threads3 implements Runnable {
public void run() {
System.out.print("running");
}
public static void main(String[] args) {
Thread t = new Thread(new Threads3());
t.run();
t.run();
t.start();
}
}

What is the result?
A. Compilation fails. B. An exception is thrown at runtime.
C. The code executes and prints "running". D. The code executes and prints "runningrunning".
E. The code executes and prints "runningrunningrunning".

Answer and Explanation

Answer: The code executes and prints "runningrunningrunning".

Explanation:
whenever you create a thread , you can start by using start() method then it will search for run() method in thread class, and you can call run() method directly from main() method, then also it will go to run() method , so ans is e)…

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. public class Threads4 {
public static void main (String[] args) {
new Threads4().go();
}
public void go() {
Runnable r = new Runnable() {
public void run() {
System.out.print("foo"); }
};
Thread t = new Thread(r);
t.start();
t.start(); }
}

What is the result?
A. Compilation fails. B. An exception is thrown at runtime.
C. The code executes normally and prints "foo". D. The code executes normally, but nothing is printed.

Answer and Explanation

Answer: An exception is thrown at runtime.

Explanation:
from go() method we are trying to create an object of Runnable interface. Actually we cannot create an object of runable interface, if you try to create then it will throw an compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.