Java OCA OCP Practice Question 3222

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 Main implements Callable {
            public int call() {
                    System.out.println("In Callable.call()");
                    return 0;
            }/*from   ww  w. j a va  2 s . com*/
    }
B.  import java.util.concurrent.Callable;

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

C.  import java.util.concurrent.Callable;

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

D.   import java.util.concurrent.Callable;

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


C.

Note

The Callable interface is defined as follows:

public interface Callable<V> {
    V call() throws Exception;
}

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 (since Callable is an interface, the implements keyword should be used).

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 Main class and so the program will not compile.).




PreviousNext

Related