Question 136

Given:
class Sum extends RecursiveAction { //line n1
static final int THRESHOLD_SIZE = 3;
int stIndex, lstIndex;
int [ ] data;
public Sum (int [ ]data, int start, int end) {
this.data = data;
this stIndex = start;
this. lstIndex = end;
}
protected void compute ( ) {
int sum = 0;
if (lstIndex - stIndex <= THRESHOLD_SIZE) {
for (int i = stIndex; i < lstIndex; i++) {
sum += data [i];
}
System.out.println(sum);
} else {
new Sum (data, stIndex + THRESHOLD_SIZE, lstIndex).fork( );
new Sum (data, stIndex,
Math.min (lstIndex, stIndex + THRESHOLD_SIZE)
).compute ();
}
}
}
and the code fragment:
ForkJoinPool fjPool = new ForkJoinPool ( );
int data [ ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
fjPool.invoke (new Sum (data, 0, data.length));
and given that the sum of all integers from 1 to 10 is 55.
Which statement is true?
  • Question 137

    Given:

    and the code fragment:

    What is the result?
  • Question 138

    Given the code fragments:

    and

    What is the result?
    France
  • Question 139

    Given the code fragments:
    class Caller implements Callable<String> {
    String str;
    public Caller (String s) {this.str=s;}
    public String call()throws Exception { return str.concat ("Caller");}
    }
    class Runner implements Runnable {
    String str;
    public Runner (String s) {this.str=s;}
    public void run () { System.out.println (str.concat ("Runner"));}
    }
    and
    public static void main (String[] args) InterruptedException, ExecutionException {
    ExecutorService es = Executors.newFixedThreadPool(2);
    Future f1 = es.submit (new Caller ("Call"));
    Future f2 = es.submit (new Runner ("Run"));
    String str1 = (String) f1.get();
    String str2 = (String) f2.get();//line n1
    System.out.println(str1+ ":" + str2);
    }
    What is the result?
  • Question 140

    Given the code fragment:

    Assume that dbURL, userName, and password are valid.
    Which code fragment can be inserted at line n1 to enable the code to print Connection Established?