What is the purpose of the @Scheduled annotation in Spring Boot?
The @Scheduled annotation is used to mark a method to be executed automatically at fixed intervals or specific times, enabling scheduled tasks in a Spring Boot application.
Click to reveal answer
beginner
How do you enable scheduling support in a Spring Boot application?
You enable scheduling by adding the @EnableScheduling annotation to a configuration class or the main application class.
Click to reveal answer
intermediate
What are the common parameters of the @Scheduled annotation?
Common parameters include fixedRate (runs method at fixed intervals), fixedDelay (runs method after a fixed delay from last completion), and cron (runs method based on a cron expression).
Click to reveal answer
intermediate
Explain the difference between fixedRate and fixedDelay in @Scheduled.
fixedRate runs the method at regular intervals regardless of how long the method takes, while fixedDelay waits until the previous execution finishes and then waits the specified delay before running again.
Click to reveal answer
advanced
What is a cron expression in the context of @Scheduled?
A cron expression is a string that defines a schedule using fields for seconds, minutes, hours, day of month, month, day of week, and optionally year, allowing precise control over when a task runs.
Click to reveal answer
Which annotation must be added to enable scheduling in Spring Boot?
A@EnableScheduling
B@Scheduled
C@EnableAsync
D@EnableTask
✗ Incorrect
You must add @EnableScheduling to enable scheduling support. @Scheduled marks the methods to run on schedule.
What does fixedRate in @Scheduled do?
ARuns the method at fixed intervals regardless of execution time
BRuns the method after a fixed delay from last completion
CRuns the method only once
DRuns the method based on a cron expression
✗ Incorrect
fixedRate runs the method at regular intervals, ignoring how long the method takes.
Which parameter of @Scheduled allows scheduling with a cron expression?
AfixedDelay
Bdelay
CfixedRate
Dcron
✗ Incorrect
The cron parameter accepts a cron expression to schedule tasks precisely.
If you want a method to run 5 seconds after the last execution finishes, which @Scheduled parameter do you use?
AfixedRate = 5000
BfixedDelay = 5000
Ccron = "*/5 * * * * *"
Ddelay = 5000
✗ Incorrect
fixedDelay waits 5 seconds after the last execution finishes before running again.
Where do you place the @Scheduled annotation?
AOn a configuration class
BOn the main application class
COn a method to be scheduled
DOn a field variable
✗ Incorrect
@Scheduled is placed on the method that should run on a schedule.
Describe how to set up a scheduled task in Spring Boot using @Scheduled.
Think about enabling scheduling and marking methods.
You got /4 concepts.
Explain the difference between fixedRate and fixedDelay in scheduling tasks.
Consider timing relative to method execution.
You got /2 concepts.
Practice
(1/5)
1. What is the main purpose of the @Scheduled annotation in Spring Boot?
easy
A. To run a method automatically at fixed intervals or times
B. To mark a method as a REST endpoint
C. To inject dependencies into a class
D. To configure database connections
Solution
Step 1: Understand the role of @Scheduled
The @Scheduled annotation is used to mark methods that should run automatically on a schedule without manual calls.
Step 2: Compare with other options
Options A, C, and D relate to other Spring features like REST, dependency injection, and database setup, not scheduling.
Final Answer:
To run a method automatically at fixed intervals or times -> Option A
Quick Check:
@Scheduled = automatic method run [OK]
Hint: Remember: @Scheduled means automatic timed method run [OK]
Common Mistakes:
Confusing @Scheduled with REST or dependency injection annotations
Thinking @Scheduled configures databases
Assuming @Scheduled requires manual method calls
2. Which of the following is the correct way to schedule a method to run every 5 seconds using @Scheduled?
easy
A. @Scheduled(fixedDelay = 5000)
B. @Scheduled(rate = 5)
C. @Scheduled(cron = "0 */5 * * * *")
D. @Scheduled(fixedRate = 5000)
Solution
Step 1: Understand fixedRate vs fixedDelay
fixedRate runs the method every specified milliseconds regardless of method duration. fixedDelay waits after method finishes before next run.
Step 2: Check the options
@Scheduled(fixedRate = 5000) uses fixedRate = 5000 which means run every 5 seconds. @Scheduled(fixedDelay = 5000) uses fixedDelay which waits after method ends. @Scheduled(rate = 5) uses invalid attribute rate. @Scheduled(cron = "0 */5 * * * *") uses a cron expression which runs every 5 minutes.
Final Answer:
@Scheduled(fixedRate = 5000) -> Option D
Quick Check:
fixedRate = 5000 means every 5 seconds [OK]
Hint: Use fixedRate for exact interval runs in milliseconds [OK]
Common Mistakes:
Using 'rate' instead of 'fixedRate'
Confusing fixedDelay with fixedRate
Incorrect cron syntax for simple intervals
3. Given the code below, what will be printed and how often?
@Scheduled(fixedDelay = 3000)
public void printMessage() {
System.out.println("Hello World");
}
medium
A. Method runs only once after 3 seconds delay
B. "Hello World" prints every 3 seconds regardless of method duration
C. "Hello World" prints every 3 seconds after method finishes
D. Code will cause a compilation error
Solution
Step 1: Understand fixedDelay behavior
fixedDelay = 3000 means the method runs, then waits 3 seconds after it finishes before running again.
Step 2: Analyze the print output frequency
Since the method prints "Hello World" each time it runs, it will print every 3 seconds after the previous run completes.
Final Answer:
"Hello World" prints every 3 seconds after method finishes -> Option C
Quick Check:
fixedDelay = wait after method ends [OK]
Hint: fixedDelay waits after method ends before next run [OK]
Common Mistakes:
Confusing fixedDelay with fixedRate
Thinking method runs only once
Assuming compilation error without reason
4. Identify the error in this scheduled task code:
@Scheduled(fixedRate = "5000")
public void task() {
System.out.println("Running task");
}
medium
A. fixedRate value should be a number, not a string
B. Method must return a value
C. Missing @EnableScheduling annotation on class
D. Method name cannot be 'task'
Solution
Step 1: Check fixedRate attribute type
The fixedRate attribute expects a long number (milliseconds), not a string. Using quotes causes a syntax error.
Step 2: Verify other options
Method can be void, so no return needed. Missing @EnableScheduling is a configuration issue but not a syntax error here. Method name 'task' is valid.
Final Answer:
fixedRate value should be a number, not a string -> Option A
Quick Check:
fixedRate expects number, not string [OK]
Hint: fixedRate and fixedDelay need numbers, no quotes [OK]
Common Mistakes:
Putting quotes around fixedRate or fixedDelay values
Assuming method must return a value
Confusing configuration errors with syntax errors
5. You want a method to run every Monday at 8:30 AM using @Scheduled. Which cron expression is correct?
hard
A. @Scheduled(cron = "30 8 * * 1")
B. @Scheduled(cron = "0 30 8 ? * MON")
C. @Scheduled(cron = "0 8 30 * * MON")
D. @Scheduled(cron = "0 30 8 * * 1")
Solution
Step 1: Understand cron format in Spring
Spring cron has 6 fields: second, minute, hour, day of month, month, day of week.
Step 2: Analyze each option
@Scheduled(cron = "0 30 8 ? * MON"): "0 30 8 ? * MON" means at second 0, minute 30, hour 8, any day of month (?), any month (*), on Monday (MON). This matches 8:30 AM Monday. @Scheduled(cron = "30 8 * * 1") is missing seconds field and uses 5 fields. @Scheduled(cron = "0 8 30 * * MON") has hour and minute swapped. @Scheduled(cron = "0 30 8 * * 1") uses day of week as 1 which is Sunday in some systems, MON is clearer.
Final Answer:
@Scheduled(cron = "0 30 8 ? * MON") -> Option B
Quick Check:
Spring cron: sec min hour day month weekday [OK]
Hint: Spring cron has 6 fields; use '?' for day of month if using day of week [OK]
Common Mistakes:
Using 5-field cron instead of 6-field in Spring
Swapping hour and minute positions
Using numeric day of week without knowing system mapping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.