Bird
Raised Fist0
Spring Bootframework~20 mins

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

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

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

  1. 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.
  2. 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.
  3. Final Answer:

    To run a method automatically at fixed intervals or times -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    @Scheduled(fixedRate = 5000) -> Option D
  4. 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

  1. Step 1: Understand fixedDelay behavior

    fixedDelay = 3000 means the method runs, then waits 3 seconds after it finishes before running again.
  2. 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.
  3. Final Answer:

    "Hello World" prints every 3 seconds after method finishes -> Option C
  4. 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

  1. Step 1: Check fixedRate attribute type

    The fixedRate attribute expects a long number (milliseconds), not a string. Using quotes causes a syntax error.
  2. 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.
  3. Final Answer:

    fixedRate value should be a number, not a string -> Option A
  4. 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

  1. Step 1: Understand cron format in Spring

    Spring cron has 6 fields: second, minute, hour, day of month, month, day of week.
  2. 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.
  3. Final Answer:

    @Scheduled(cron = "0 30 8 ? * MON") -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard