0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.