Search Header Logo

Concurrency

Authored by Jose Diaz

Computers

1st Grade

Used 1+ times

Concurrency
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

9 questions

Show all answers

1.

MULTIPLE SELECT QUESTION

3 mins • 1 pt

Which of the following options correctly create an ExecutorService instance?. Please select 2 options.

var es = ExecutorService.getInstance();

var es = new ExecutorService();

var es = Executors.newFixedThreadPool(2);

var es = Executor.getSingleThreadExecutor();

var es = Executors.newVirtualThreadPerTaskExecutor();

2.

MULTIPLE SELECT QUESTION

3 mins • 1 pt

Which of the following code fragments will you use to create an ExecutorService?. Please select 3 options.

ExecutorService

Executor

Executors

.newSingleThreadExecutor();

.newVirtualThreadPerTaskExecutor();

3.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Which of the following statements correctly create a virtual thread? (Assume that r refers to a Runnable instance.). Please select 1 option.

Thread t = new VirtualThread(r);

Thread t = new VirtualThread(r);

VirtualThread t = Thread.ofVirtual();

Thread t = Thread.ofVirtual();

Thread t = Thread.ofVirtual().unstarted(r);

4.

MULTIPLE SELECT QUESTION

3 mins • 1 pt

Given:

ReadWriteLock rl = new ReentrantReadWriteLock();

Lock rlock = rl.readLock();

Lock wlock = rl.writeLock();

Identify correct statements regarding code that appears after the above code. (Consider each option individually.). Please select 3 options.

rlock.lock();
System.out.println("read lock acquired");
wlock.lock();
System.out.println("write lock acquired");

This code will print both the statements.

wlock.lock();
System.out.println("write lock acquired");
rlock.lock();
System.out.println("read lock acquired");

This code will print both the statements.

rlock.lock();
System.out.println("read lock acquired");
rlock.lock();
System.out.println("read lock acquired again");

This code will print both the statements.

rlock.lock();
rlock.lock();
rlock.unlock();
wlock.lock();
System.out.println("write lock acquired");

This code will print the statement.

rlock.lock();
try{
    wlock.tryLock();
    wlock.unlock();
    System.out.println("write lock acquired ");
}catch(Exception e) {
    e.printStackTrace();
}

This code will print an exception stack trace.

5.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

You have a collection (say, an ArrayList) which is read by multiple reader threads and which is modified by a single writer thread. The collection allows multiple concurrent reads but does not tolerate concurrent read and write. Which of the following strategies will you use to obtain best performance?. Please select 1 option.

synchronize all access to the collection.

make the collection variable final.

make the collection variable final and volatile.

Wrap the collection into its synchronized version using Collections.synchronizedCollection().

Encapsulate the collection into another class and use ReadWriteLock to manage read and write access

6.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

What will the following code print?

AtomicInteger ai = new AtomicInteger();

Stream<Integer> stream = Stream.of(11, 11, 22, 33).parallel();

stream.filter(

e->{ ai.incrementAndGet();

return e%2==0;

});

System.out.println(ai); Please select 1 option.

0

Any number between 1 to 4.

4

Any number between 0 to 3

7.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

If a synchronized method ends up throwing an exception to the caller, the lock acquired by the thread associated with this method due to the usage of the synchronized keyword is released automatically. Please select 1 option.

true

false

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?