Welcome Guest | Sign in | Register

Home > Java Programming > Threads > Questions and Answers

01. public class Threads5 {

public static void main (String[] args) {
new Thread(new Runnable() {
public void run() {
System.out.print("bar");
}}).start();
}
}

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

Answer and Explanation

Answer: The code executes normally and prints "bar".

Explanation:
just a normal flow of execution.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 12. public class Starter extends Thread {
private int x = 2;
public static void main(String[] args) throws Exception {
new Starter().makeItSo();
}
public Starter() {
x = 5;
start();
}
public void makeItSo() throws Exception {
join();
x = x - 1;
System.out.println(x);
}
public void run() { x *= 2; }
}

What is the output if the main() method is run?
A. 4 B. 5
C. 8 D. 9
E. Compilation fails.

Answer and Explanation

Answer: 9

Explanation:
from main() control will go to Starter() method there x value will become 5. From Starter() it will go to run() here value will become 10. From run() control will go to makeItSo() method here the value of x will be subtracted by 1, so ans is 9. Option D) is correct.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. What will happen when you attempt to compile and run the following code ?

1. public class Test extends Thread{
2. public static void main(String argv[]){
3. Test t = new Test();
4. t.run();
5. t.start();
6. }
7. public void run(){
8. System.out.println("run-test");
9. }
10. }
A. run-test run-test B. run-test
C. Compilation fails due to an error on line 4 D. Compilation fails due to an error on line 7

Answer and Explanation

Answer: run-test run-test

Explanation:
t.run() Legal, but does not start a new thread , it is like a method call of class Test BUT t.start() create a thread and call run() method.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. What is the output for the below code ?

class A implements Runnable{
public void run(){
System.out.println("run-a");
}
}
1. public class Test {
2. public static void main(String... args) {
3. A a = new A();
4. Thread t = new Thread(a);
5. t.start();
6. t.start();
7. }
8. }
A. run-a B. run-a run-a
C. Compilation fails with an error at line 6 D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: Compilation succeed but Runtime Exception

Explanation:
Once a thread has been started, it can never be started again. 2nd time t.start() throws
java.lang.IllegalThreadStateException.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the output for the below code ?

class A implements Runnable{
public void run(){
try{
for(int i=0;i<4;i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName());
}
}catch(InterruptedException e){
}
}
}
public class Test {
public static void main(String argv[]) throws Exception{
A a = new A();
Thread t = new Thread(a,"A");
Thread t1 = new Thread(a,"B");
t.start();
t.join();
t1.start();
}
}
A. A A A A B B B B B. A B A B A B A B
C. Output order is not guaranteed D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: A A A A B B B B

Explanation:
t.join(); means Threat t must finish before Thread t1 start.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. What is the output for the below code ?

public class B {
public synchronized void printName(){
try{
System.out.println("printName");
Thread.sleep(5*1000);
}catch(InterruptedException e){
}
}
public synchronized void printValue(){
System.out.println("printValue");
}
}
public class Test extends Thread{
B b = new B();
public static void main(String argv[]) throws Exception{
Test t = new Test();
Thread t1 = new Thread(t,"t1");
Thread t2 = new Thread(t,"t2");
t1.start();
t2.start();
}
public void run(){
if(Thread.currentThread().getName().equals("t1")){
b.printName();
}else{
b.printValue();
}
}
}
A. print : printName , then wait for 5 seconds then print : printValue B. print : printName then print : printValue
C. print : printName then wait for 5 minutes then print : printValue D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: print : printName , then wait for 5 seconds then print : printValue

Explanation:
There is only one lock per object, if one thread has picked up the lock, no other thread can pick up the lock until the first thread releases the lock. printName() method acquire the lock for 5 seconds, So other threads cannot access the object. If one synchronized method of an instance is executing then other synchronized method of the same instance
should wait.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. What is the output for the below code ?

public class B {
public static synchronized void printName(){
try{
System.out.println("printName");
Thread.sleep(5*1000);
}catch(InterruptedException e){
}
}
public synchronized void printValue(){
System.out.println("printValue");
}
}
public class Test extends Thread{
B b = new B();
public static void main(String argv[]) throws Exception{
Test t = new Test();
Thread t1 = new Thread(t,"t1");
Thread t2 = new Thread(t,"t2");
t1.start();
t2.start();
}
public void run(){
if(Thread.currentThread().getName().equals("t1")){
b.printName();
}else{
b.printValue();
}
}
}
A. print : printName , then wait for 5 seconds then print : printValue B. print : printName then print : printValue
C. print : printName then wait for 5 minutes then print : printValue D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer: print : printName then print : printValue

Explanation:
There is only one lock per object, if one thread has picked up the lock, no other thread can pick up the lock until the first thread releases the lock. In this case printName() is static , So lock is in class B not instance b, both method (one static and other no-static) can run simultaneously. A static synchronized method and a non static synchronized method will not block each other.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. What is the output for the below code ?

class A extends Thread{
int count = 0;
public void run(){
System.out.println("run");
synchronized (this) {
for(int i =0; i < 50 ; i++){
count = count + i;
}
notify();
}
}
}
public class Test{
public static void main(String argv[]) {
A a = new A();
a.start();
synchronized (a) {
System.out.println("waiting");
try{
a.wait();
}catch(InterruptedException e){
}
System.out.println(a.count);
}
}

A. waiting run 1225 B. waiting run 0
C. waiting run and count can be anything D. Compilation fails

Answer and Explanation

Answer: waiting run 1225

Explanation:
a.wait(); put thread on wait until not get notifed. A thread gets on this waiting list by
executing the wait() method of the target object. It doesn't execute any further
instructions until the notify() method of the target object is called. A thread to call wait() or notify(), the thread has to be the owner of the lock for that object.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which of the following statements about this code are true?

class A extends Thread{
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}
}
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.check(new A(){});
}
public void check(A a){
a.start();
}
}
A. 0 0 B. Compilation error, class A has no start method
C. 0 1 D. Compilation succeed but runtime exception

Answer and Explanation

Answer: 0 1

Explanation:
class A extends Thread means the anonymous instance that is passed to check() method has a start method which then calls the run method.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10.  What will happen when you attempt to compile and run the following code?

class A implements Runnable{
public void run(){
System.out.println("run-A");
}
}
1. public class Test {
2. public static void main(String argv[]){
3. A a = new A();
4. Thread t = new Thread(a);
5. System.out.println(t.isAlive());
6. t.start();
7. System.out.println(t.isAlive());
8. }
9. }
A. false run-A true B. false run-A false
C. true run-A true D. Compilation fails due to an error on line 7

Answer and Explanation

Answer: false run-A true

Explanation:
Once the start() method is called, the thread is considered to be alive.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



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