0
0
Spring Bootframework~20 mins

Scheduled tasks with @Scheduled in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scheduled Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding fixedRate in @Scheduled
What is the behavior of a method annotated with @Scheduled(fixedRate = 5000) in a Spring Boot application?
Spring Boot
@Scheduled(fixedRate = 5000)
public void task() {
    System.out.println("Task executed");
}
AThe method runs continuously without any delay.
BThe method runs every 5 seconds, counting from the start of the previous execution.
CThe method runs once after 5 seconds delay and then stops.
DThe method runs every 5 seconds, counting from the end of the previous execution.
Attempts:
2 left
💡 Hint
Think about how fixedRate schedules tasks relative to the start time of the previous run.
📝 Syntax
intermediate
2:00remaining
Correct usage of cron expression in @Scheduled
Which of the following @Scheduled annotations uses a valid cron expression to run a task every day at 3:30 AM?
A@Scheduled(cron = "0 3 30 * * ?")
B@Scheduled(cron = "30 3 * * *")
C@Scheduled(cron = "0 30 3 * * *")
D@Scheduled(cron = "0 30 3 * * ?")
Attempts:
2 left
💡 Hint
Remember the cron format: second, minute, hour, day of month, month, day of week, and optional year.
🔧 Debug
advanced
2:00remaining
Why does @Scheduled method not run?
Given the following Spring Boot component, why does the scheduled method never execute?
Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class MyTask {

    @Scheduled(fixedDelay = 1000)
    public void run() {
        System.out.println("Running task");
    }
}
AThe method must be static to be scheduled.
BThe fixedDelay value must be in milliseconds, but 1000 is invalid.
CThe @EnableScheduling annotation is missing in the configuration.
DThe @Component annotation is not enough; @Service is required.
Attempts:
2 left
💡 Hint
Check if scheduling is enabled in the Spring Boot application.
state_output
advanced
2:00remaining
Effect of long-running @Scheduled method with fixedRate
Consider a method annotated with @Scheduled(fixedRate = 2000) that takes 5 seconds to complete. What happens when the application runs?
Spring Boot
@Scheduled(fixedRate = 2000)
public void longTask() throws InterruptedException {
    Thread.sleep(5000);
    System.out.println("Task done");
}
AThe method executions overlap; a new run starts every 2 seconds even if the previous is still running.
BThe method waits for the previous execution to finish before starting the next, so it runs every 5 seconds.
CThe method runs only once because the fixedRate is less than the execution time.
DThe scheduler throws an exception due to overlapping executions.
Attempts:
2 left
💡 Hint
Think about how fixedRate schedules tasks regardless of method duration.
🧠 Conceptual
expert
2:00remaining
Choosing between fixedDelay and fixedRate
Which statement best describes the difference between fixedDelay and fixedRate in @Scheduled annotations?
AfixedDelay schedules the next execution after the previous finishes; fixedRate schedules based on start time regardless of duration.
BfixedDelay schedules tasks at fixed intervals ignoring execution time; fixedRate waits for task completion before next run.
CfixedDelay and fixedRate behave identically but fixedDelay is deprecated.
DfixedDelay schedules tasks only once; fixedRate schedules repeated tasks.
Attempts:
2 left
💡 Hint
Consider when the next task starts relative to the previous task's start or end.