Menu
Topics Index
...
`

Threads1721


If you need explanation Read this topic

If you need Answer Take test on this topic

Threads1721
What is the output of the following program?
import java.util.concurrent.*;
public class CocurrencyTest {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(3);
        Future<Double> f = (Future<Double>) es.submit(new ClassB(2.0, 2));
        Future<Integer> f1 = es.submit(new ClassA(5));
        Future<Integer> f2 = es.submit(new ClassC(3));
        try {
            System.out.print(f1.get() + " ");
            f.get();
            System.out.println(f2.get());
        } catch (Exception e) {  }
    }
}
class ClassA implements Callable<Integer> {
    int E;
    public ClassA(int i) { E = i; }
    public Integer call() throws Exception {
        int result = 0;
        for (int i = 0; i < E; i++) {result += i; }
        return result;
    }
}
class ClassB implements Runnable {
    double s;
    double e;
    public ClassB(double s, double e) { this.s = s; this.e = e; }
    public void run() { System.out.print(Math.sqrt(s + e) + " "); }
}
class ClassC implements Callable<Integer> {
    int E;
    public ClassC(int i) { E = i;  }
    public Integer call() throws Exception {
        int result = 1;
        for (int i = 1; i < E; i++) { result *= i;  }
        return result;
    }
}

A. 2.0 10 6
B. 10 2.0 2
C. 2.0 10 2
D. Compiles and executes without any output
E. output cannot be determined.
F. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D is the ans..as sqrt(s+ e) gives nothing

Posted by Shabaaz Shaikz    2014-08-18 13:11:12


Ans is E. First it prints 2.0 (ClassB) and 10 (ClassA) and 2 and stops executing.

Posted by Nikhil Sulibhavi    2014-08-18 13:18:38


Ans c.
classB returns 2.0
sqrt of 2.0+2=sqrt of 4.0 n returns 2.0
class A returns 10
for loop iterates upto 4 so 1+2+3+4
class C returns 2
for loop iterates upto 2
1*2 =2

Posted by Maheshwari Natarajan    2014-08-18 14:57:51


answer b.
1st classA func f1.get cald so 10 gets printd.
2 nd f.get cald n 2.0 printed.
3rd f2.get method cald 2 printd

Posted by NagaRaj Nataraj    2014-08-18 15:04:35


Congratulations Nikhil Sulibhavi. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-08-19 04:32:45


© meritcampus 2019

All Rights Reserved.

Open In App