Bird
Raised Fist0
Spring Bootframework~20 mins

@EnableCaching annotation in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Caching Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when @EnableCaching is added to a Spring Boot application?
Consider a Spring Boot application with a service method annotated with @Cacheable. What is the effect of adding @EnableCaching to the main configuration class?
Spring Boot
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class AppConfig {
}
AIt automatically creates a cache store but does not enable caching on any methods.
BIt disables all caching in the application, forcing methods to always execute fully.
CIt activates Spring's annotation-driven cache management, enabling caching behavior on methods annotated with @Cacheable.
DIt only enables caching for methods annotated with @CachePut, ignoring @Cacheable annotations.
Attempts:
2 left
💡 Hint
Think about what @EnableCaching does in relation to @Cacheable methods.
📝 Syntax
intermediate
1:30remaining
Identify the correct way to enable caching in a Spring Boot application
Which of the following code snippets correctly enables caching in a Spring Boot application?
A
@Configuration
@EnableCaching
public class CacheConfig {}
B
@EnableCaching
public class CacheConfig {}
C
@Configuration
@EnableCache
public class CacheConfig {}
D
@Configuration
@EnableCaching(true)
public class CacheConfig {}
Attempts:
2 left
💡 Hint
Remember the exact annotation name and required annotations for configuration classes.
🔧 Debug
advanced
2:30remaining
Why does caching not work despite @EnableCaching being present?
Given the following code, why might caching not be working as expected? @Configuration @EnableCaching public class AppConfig {} @Service public class MyService { @Cacheable("items") public String getItem(String id) { return "Item" + id; } } Assume the application runs but caching does not reduce method calls.
AThe @EnableCaching annotation must be placed on the service class, not the configuration class.
BThe cache name "items" is invalid and must be registered explicitly in the cache manager.
CThe @Cacheable annotation requires a key attribute to work properly.
DThe service method is called from within the same class, so Spring's proxy does not intercept the call to apply caching.
Attempts:
2 left
💡 Hint
Think about how Spring proxies work and when caching applies.
🧠 Conceptual
advanced
2:00remaining
What is the role of @EnableCaching in Spring's caching abstraction?
Which statement best describes the role of @EnableCaching in a Spring application?
A@EnableCaching disables caching unless a custom cache manager bean is provided.
B@EnableCaching activates the processing of caching annotations and configures a cache manager if none is defined.
C@EnableCaching replaces the need to annotate methods with @Cacheable or other caching annotations.
D@EnableCaching automatically creates and manages all caches without any further configuration.
Attempts:
2 left
💡 Hint
Consider what enabling caching means for annotation processing and cache manager setup.
❓ state_output
expert
2:30remaining
What is the output count of method calls with @EnableCaching and @Cacheable?
Given this Spring Boot service: @Service public class CounterService { private int count = 0; @Cacheable("numbers") public int getNumber(int input) { count++; return input * 2; } public int getCount() { return count; } } And this test code: CounterService service = context.getBean(CounterService.class); service.getNumber(5); service.getNumber(5); service.getNumber(10); service.getNumber(5); int calls = service.getCount(); What is the value of calls after these calls if @EnableCaching is present?
A2
B1
C4
D3
Attempts:
2 left
💡 Hint
Remember that @Cacheable caches results by method parameters.

Practice

(1/5)
1. What is the main purpose of the @EnableCaching annotation in a Spring Boot application?
easy
A. To activate caching support so methods annotated with @Cacheable can cache results
B. To disable all caching mechanisms in the application
C. To configure database connections automatically
D. To enable security features for REST endpoints

Solution

  1. Step 1: Understand @EnableCaching role

    This annotation activates Spring's caching support, allowing caching annotations to work.
  2. Step 2: Connect with @Cacheable

    Methods annotated with @Cacheable store their results in cache only if caching is enabled.
  3. Final Answer:

    To activate caching support so methods annotated with @Cacheable can cache results -> Option A
  4. Quick Check:

    @EnableCaching enables caching [OK]
Hint: Remember: @EnableCaching turns caching ON [OK]
Common Mistakes:
  • Thinking @EnableCaching disables caching
  • Confusing it with database or security annotations
  • Assuming it caches data by itself without @Cacheable
2. Which of the following is the correct way to enable caching in a Spring Boot application?
easy
A. @CacheEnable
B. @EnableCache
C. @EnableCaching
D. @CachingEnable

Solution

  1. Step 1: Recall the exact annotation name

    The correct annotation to enable caching is @EnableCaching.
  2. Step 2: Check other options for typos

    Options B, C, and D are incorrect because they are not valid Spring annotations.
  3. Final Answer:

    @EnableCaching -> Option C
  4. Quick Check:

    Correct annotation spelling is @EnableCaching [OK]
Hint: Look for exact spelling: EnableCaching, not EnableCache [OK]
Common Mistakes:
  • Using incorrect annotation names with similar words
  • Missing the capital 'C' in Caching
  • Confusing with other Spring annotations
3. Given the following Spring Boot code snippet, what will be the output when getData() is called twice?
@EnableCaching
@Service
public class DataService {
  private int count = 0;

  @Cacheable("dataCache")
  public String getData() {
    count++;
    return "Call number: " + count;
  }
}
medium
A. ["Call number: 1", "Call number: 1"]
B. ["Call number: 2", "Call number: 2"]
C. ["Call number: 0", "Call number: 1"]
D. ["Call number: 1", "Call number: 2"]

Solution

  1. Step 1: Understand caching effect on method calls

    The first call to getData() runs the method, increments count to 1, and caches the result.
  2. Step 2: Analyze second call behavior

    The second call returns the cached result without running the method again, so count stays 1.
  3. Final Answer:

    ["Call number: 1", "Call number: 1"] -> Option A
  4. Quick Check:

    Cache returns same result on repeated calls [OK]
Hint: Cached method runs once; repeated calls return cached result [OK]
Common Mistakes:
  • Assuming count increments on every call despite caching
  • Ignoring that @Cacheable caches method output
  • Confusing cache key usage
4. Identify the error in the following Spring Boot configuration that prevents caching from working:
@Configuration
public class CacheConfig {
  @Bean
  public CacheManager cacheManager() {
    return new ConcurrentMapCacheManager("items");
  }
}
medium
A. Cache name "items" is invalid
B. Missing @EnableCaching annotation on configuration class
C. CacheManager bean should return null
D. CacheManager must be annotated with @Service

Solution

  1. Step 1: Check caching activation

    Defining a CacheManager bean is not enough; caching must be enabled with @EnableCaching.
  2. Step 2: Verify other options

    CacheManager returning null or invalid cache names are incorrect; @Service is unrelated here.
  3. Final Answer:

    Missing @EnableCaching annotation on configuration class -> Option B
  4. Quick Check:

    Enable caching with @EnableCaching [OK]
Hint: Always add @EnableCaching to activate caching [OK]
Common Mistakes:
  • Forgetting to add @EnableCaching annotation
  • Misunderstanding CacheManager bean role
  • Wrong annotation usage on CacheManager
5. You want to cache results of a method that takes a user ID and returns user details. You also want to clear the cache when user data updates. Which combination of annotations and configuration correctly achieves this in Spring Boot?
hard
A. Use @EnableCaching on service class, @CachePut("users") on get method, and no annotation on update method
B. Use @CacheEvict on config class, @EnableCaching on update method, and no caching on get method
C. Use @Cacheable("users") on config class, @EnableCaching on get method, and @CacheEvict on update method
D. Use @EnableCaching on config class, @Cacheable("users") on get method, and @CacheEvict(value = "users", key = "#userId") on update method

Solution

  1. Step 1: Enable caching globally

    Add @EnableCaching on a configuration or main class to activate caching support.
  2. Step 2: Cache method results and evict on update

    Use @Cacheable("users") on the method fetching user details to cache results. Use @CacheEvict(value = "users", key = "#userId") on the update method to clear cache for that user.
  3. Final Answer:

    Use @EnableCaching on config class, @Cacheable("users") on get method, and @CacheEvict(value = "users", key = "#userId") on update method -> Option D
  4. Quick Check:

    Enable caching + cache get + evict on update [OK]
Hint: Enable caching, cache gets, evict on updates [OK]
Common Mistakes:
  • Not enabling caching globally with @EnableCaching
  • Using @CachePut instead of @CacheEvict for clearing cache
  • Placing annotations on wrong classes or methods
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