Java OCA OCP Practice Question 2279

Question

Which one of the following options correctly makes use of Callable that will compile without any errors?.

A.  import java.util.concurrent.Callable;

class CallableTask implements Callable {
    public int call() {
        System.out.println("In Callable.call()");
        return 0;
    }/* ww  w.j av  a 2s.c o  m*/
}

B.   import java.util.concurrent.Callable;

class CallableTask extends Callable {
    public Integer call() {
        System.out.println("In Callable.call()");
        return 0;
    }
}

C.   import java.util.concurrent.Callable;

class CallableTask implements Callable<Integer> {
    public Integer call() {
        System.out.println("In Callable.call()");
        return 0;
    }
}

D.   import java.util.concurrent.Callable;

class CallableTask implements Callable<Integer> {
    public void call(Integer i) {
        System.out.println("In Callable.call(i)");
    }
}


C.

Note

In option a), the call() method has the return type int, which is incompatible with the return type expected for overriding the call method and so will not compile.

In option B), the extends keyword is used, which will result in a compiler.

option C) correctly defines the Callable interface providing the type parameter <Integer>.

the same type parameter Integer is also used in the return type of the call() method that takes no arguments, so it will compile without errors.

In option D), the return type of call() is void and the call() method also takes a parameter of type Integer.

hence, the method declared in the interface Integer call() remains unimplemented in the CallableTask class, so the program will not compile.




PreviousNext

Related