Bird
Raised Fist0
Spring Bootframework~20 mins

@Async for async methods in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a Spring Boot method annotated with @Async returns void?
Consider a Spring Boot service method annotated with @Async that returns void. What is the behavior when this method is called from another bean?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async
    public void asyncVoidMethod() {
        System.out.println("Start asyncVoidMethod");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("End asyncVoidMethod");
    }
}
AThe method runs asynchronously in a separate thread, and the caller does not wait for it to finish.
BThe method runs synchronously, blocking the caller until it finishes.
CThe method throws an exception because @Async cannot be used with void return type.
DThe method runs asynchronously but the caller receives a Future object to track completion.
Attempts:
2 left
💡 Hint
Think about how @Async handles methods without a return value.
❓ state_output
intermediate
2:00remaining
What is the output order when calling two @Async methods sequentially?
Given two @Async methods called one after another in a Spring Boot application, what is the expected order of their printed output?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async
    public void firstAsync() {
        System.out.println("First start");
        try { Thread.sleep(500); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
        System.out.println("First end");
    }

    @Async
    public void secondAsync() {
        System.out.println("Second start");
        try { Thread.sleep(300); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
        System.out.println("Second end");
    }
}

// In some caller bean
asyncService.firstAsync();
asyncService.secondAsync();
AFirst start, First end, Second start, Second end
BSecond start, Second end, First start, First end
CSecond start, First start, First end, Second end
DFirst start, Second start, Second end, First end
Attempts:
2 left
💡 Hint
Remember that @Async methods run in separate threads and may overlap.
📝 Syntax
advanced
2:00remaining
Which option correctly enables @Async support in a Spring Boot application?
To use @Async annotation in Spring Boot, which configuration is required to enable asynchronous processing?
A@EnableAsync annotation on a configuration class
B@AsyncConfiguration annotation on the main application class
CAdding async=true in application.properties
DUsing @EnableScheduling annotation on a service class
Attempts:
2 left
💡 Hint
Look for the official annotation that activates async processing.
🔧 Debug
advanced
2:00remaining
Why does @Async not work when calling an async method from the same class?
In Spring Boot, a method annotated with @Async is called from another method in the same class, but it runs synchronously. Why?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async
    public void asyncMethod() {
        System.out.println("Async method running");
    }

    public void callerMethod() {
        asyncMethod();
    }
}
AThe @Async annotation requires the method to be public, and asyncMethod is private.
BSpring proxies do not intercept calls within the same class, so @Async is ignored.
CThe asyncMethod must return a Future for @Async to work.
DThe @Async annotation only works on methods in interfaces, not classes.
Attempts:
2 left
💡 Hint
Think about how Spring creates proxies for @Async methods.
🧠 Conceptual
expert
3:00remaining
What is the effect of returning CompletableFuture from an @Async method?
In Spring Boot, if an @Async method returns a CompletableFuture, what advantage does this provide compared to returning void?
AIt disables the asynchronous behavior and runs the method in the caller thread.
BIt forces the method to run synchronously to produce the CompletableFuture.
CIt allows the caller to track completion and get the result asynchronously.
DIt causes a runtime error because @Async methods cannot return CompletableFuture.
Attempts:
2 left
💡 Hint
Think about how CompletableFuture can be used to handle async results.

Practice

(1/5)
1.

What is the main purpose of using @Async in a Spring Boot application?

easy
A. To secure methods with authentication
B. To make methods run faster by optimizing CPU usage
C. To automatically retry failed methods
D. To run methods in the background without blocking the main thread

Solution

  1. Step 1: Understand what @Async does

    @Async allows methods to run in a separate thread, so they don't block the main thread.
  2. Step 2: Identify the main benefit

    This helps keep the app responsive by running long tasks in the background.
  3. Final Answer:

    To run methods in the background without blocking the main thread -> Option D
  4. Quick Check:

    Background execution = To run methods in the background without blocking the main thread [OK]
Hint: Async means background task, not faster or secure [OK]
Common Mistakes:
  • Thinking @Async speeds up method execution
  • Confusing @Async with security annotations
  • Assuming @Async retries failed methods automatically
2.

Which annotation must be added to a Spring Boot configuration class to enable @Async support?

easy
A. @EnableAsync
B. @EnableScheduling
C. @AsyncSupport
D. @EnableAsyncSupport

Solution

  1. Step 1: Recall the enabling annotation for async

    Spring Boot requires @EnableAsync on a configuration class to activate async processing.
  2. Step 2: Confirm the exact annotation name

    The correct annotation is @EnableAsync, not any other variant.
  3. Final Answer:

    @EnableAsync -> Option A
  4. Quick Check:

    Enable async = @EnableAsync [OK]
Hint: Enable async with @EnableAsync, not similar names [OK]
Common Mistakes:
  • Using @EnableScheduling instead of @EnableAsync
  • Guessing annotation names without checking docs
  • Confusing @Async with enabling annotations
3.

Consider this Spring Boot method annotated with @Async:

  @Async
  public CompletableFuture<String> fetchData() {
    return CompletableFuture.completedFuture("Done");
  }

What will be the output if you call fetchData().get()?

medium
A. It blocks until the async task completes and returns "Done"
B. It throws a NullPointerException
C. It returns "Done" immediately
D. It returns null because async methods don't return values

Solution

  1. Step 1: Understand CompletableFuture with @Async

    The method returns a CompletableFuture that completes immediately with "Done".
  2. Step 2: Calling get() on CompletableFuture

    Calling get() blocks the caller until the async task finishes and then returns the result.
  3. Final Answer:

    It blocks until the async task completes and returns "Done" -> Option A
  4. Quick Check:

    CompletableFuture.get() blocks and returns result [OK]
Hint: CompletableFuture.get() waits for async result [OK]
Common Mistakes:
  • Assuming async methods return immediately without blocking on get()
  • Thinking async methods can't return values
  • Confusing exceptions with null returns
4.

Given this Spring Boot service method:

  @Async
  public void process() {
    System.out.println("Start");
    throw new RuntimeException("Error");
  }

What happens when process() is called?

medium
A. The exception is thrown on the caller thread immediately
B. The exception is logged but does not affect the caller thread
C. The method runs synchronously and throws the exception normally
D. The exception is ignored silently

Solution

  1. Step 1: Understand exception handling in @Async methods

    Exceptions in async methods run in a separate thread and do not propagate to the caller.
  2. Step 2: What happens to the exception?

    The exception is logged by Spring but does not crash or throw on the caller thread.
  3. Final Answer:

    The exception is logged but does not affect the caller thread -> Option B
  4. Quick Check:

    Async exceptions logged, caller unaffected [OK]
Hint: Async exceptions don't throw on caller thread [OK]
Common Mistakes:
  • Expecting exceptions to propagate to caller immediately
  • Thinking async methods run synchronously
  • Assuming exceptions are ignored silently
5.

You want to run two independent tasks asynchronously and wait concurrently for both to finish before proceeding. Which approach using @Async and CompletableFuture is correct?

  @Async
  public CompletableFuture<String> taskOne() { ... }

  @Async
  public CompletableFuture<String> taskTwo() { ... }

  public void runTasks() throws Exception {
    CompletableFuture<String> future1 = taskOne();
    CompletableFuture<String> future2 = taskTwo();
    // What to do here?
  }
hard
A. Just call taskOne() and taskTwo() without waiting
B. Call future1.get() and future2.get() sequentially to wait
C. Use CompletableFuture.allOf(future1, future2).join() to wait both
D. Call future1.complete() and future2.complete() manually

Solution

  1. Step 1: Understand waiting for multiple async tasks

    Calling get() sequentially works but is inefficient and blocks one after another.
  2. Step 2: Use CompletableFuture.allOf for parallel waiting

    CompletableFuture.allOf(future1, future2).join() waits for both tasks to complete concurrently.
  3. Final Answer:

    Use CompletableFuture.allOf(future1, future2).join() to wait both -> Option C
  4. Quick Check:

    Wait all async tasks with allOf().join() [OK]
Hint: Use allOf().join() to wait multiple async tasks together [OK]
Common Mistakes:
  • Calling get() sequentially causing unnecessary blocking
  • Not waiting at all and proceeding too early
  • Trying to manually complete futures which is incorrect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard