LearnFrenzy
Sign in Take a free test

Concurrency and Executors · Exercise 3 of 4

Concurrency and Executors Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Java program misuses a java.util.concurrent API in a way javac rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(2);
        Future<Integer> f = pool.submit(() -> 6 * 7);
        System.out.println(f.get());
        pool.shutdown();
    }
}
A submit cannot take a lambda
B Future.get throws the checked InterruptedException and ExecutionException, which main neither catches nor declares
C Future must be Future<int>
D shutdown must come first
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.Callable;

public class Main {
    public static void main(String[] args) throws Exception {
        Callable<Integer> task = () -> {
            int total = 0;
            for (int i = 1; i <= 10; i++) total += i;
        };
        System.out.println(task.call());
    }
}
A call() cannot be invoked directly
B Callable<Integer>.call must return an Integer, but the block body has no return statement; add return total;
C total must be Integer
D Lambdas cannot contain loops
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.ExecutorService;

public class Main {
    public static void main(String[] args) {
        ExecutorService pool = new ExecutorService();
        pool.submit(() -> System.out.println("hi"));
    }
}
A submit needs a Callable
B ExecutorService is an interface; create one with a factory such as Executors.newFixedThreadPool(4)
C ExecutorService must be imported from java.lang
D pool must be final
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        Callable<String> job = () -> "done";
        pool.execute(job);
        pool.shutdown();
    }
}
A Callable cannot return String
B execute accepts only a Runnable; use submit(job) to run a Callable and get a Future
C Single-thread executors cannot run Callables
D shutdown must be called first
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.locks.ReentrantLock;

public class Main {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        if (lock.lock()) {
            try { System.out.println("in"); } finally { lock.unlock(); }
        }
    }
}
A unlock must come first
B lock() returns void (it waits until the lock is acquired); use tryLock(), which returns a boolean, if you need a condition
C ReentrantLock needs a fairness argument
D try-finally is not allowed with locks
Question page & discussion
Question 6 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    public static void main(String[] args) {
        AtomicInteger hits = new AtomicInteger();
        hits++;
        System.out.println(hits);
    }
}
A AtomicInteger needs an initial value
B AtomicInteger is an object, not a number – operators like ++ do not apply; call hits.incrementAndGet()
C hits must be volatile
D println cannot print atomics
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        CompletableFuture<String> f = CompletableFuture.supplyAsync(() -> System.out.println("loading"));
        System.out.println(f.join());
    }
}
A join cannot print
B supplyAsync needs a Supplier that returns a value, but println returns void; return a String, or use runAsync for a task with no result
C CompletableFuture needs an executor
D Strings cannot be supplied asynchronously
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.CompletableFuture;

public class Main {
    public static void main(String[] args) {
        CompletableFuture<Integer> length = CompletableFuture.supplyAsync(() -> "learnfrenzy").thenApply(String::toUpperCase);
        System.out.println(length.join());
    }
}
A toUpperCase cannot be a method reference
B thenApply(String::toUpperCase) produces a CompletableFuture<String>, not <Integer>; use thenApply(String::length) or change the type
C supplyAsync cannot return a String
D join returns void
Question page & discussion
Question 9 Easy

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.concurrent.Semaphore;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Semaphore slots = new Semaphore();
        slots.acquire();
        System.out.println("got a slot");
    }
}
A acquire needs an argument
B Semaphore has no no-arg constructor; pass the number of permits, e.g. new Semaphore(3)
C Semaphore is an interface
D main cannot throw InterruptedException
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

Main.java
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws Exception {
        List<Callable<Integer>> tasks = List.of(() -> 1, () -> 2);
        try (ExecutorService pool = Executors.newFixedThreadPool(2)) {
            List<Integer> results = pool.invokeAll(tasks);
            System.out.println(results);
        }
    }
}
A invokeAll needs a timeout
B invokeAll returns List<Future<Integer>>, not List<Integer>; call get() on each Future to obtain the values
C Callables cannot be put in a List
D try-with-resources cannot hold an executor
Question page & discussion
Up next · Exercise 4Concurrency and Executors Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links