Bird
Raised Fist0
Spring Bootframework~20 mins

Cache configuration in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot cache example?
Consider a Spring Boot service method annotated with @Cacheable("books"). If the method is called twice with the same argument, what happens on the second call?
Spring Boot
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class BookService {
    @Cacheable("books")
    public String findBook(String isbn) {
        System.out.println("Fetching book for " + isbn);
        return "Book-" + isbn;
    }
}

// In main application or test:
// bookService.findBook("123");
// bookService.findBook("123");
AThe method prints "Fetching book for 123" twice and returns the same result both times.
BThe method throws a runtime exception because caching is not enabled by default.
CThe method prints "Fetching book for 123" once and returns cached result on the second call without printing.
DThe method returns null on the second call because cache is cleared automatically.
Attempts:
2 left
💡 Hint
Think about what @Cacheable does when the same argument is used multiple times.
📝 Syntax
intermediate
1:30remaining
Which cache configuration snippet correctly enables caching in Spring Boot?
Select the correct way to enable caching in a Spring Boot application.
A
@EnableCaching
@SpringBootApplication
public class App {}
B
@EnableCache
@SpringBootApplication
public class App {}
C
@EnableCaching
@Component
public class App {}
D
@Cacheable
@SpringBootApplication
public class App {}
Attempts:
2 left
💡 Hint
Look for the correct annotation to enable caching support.
🔧 Debug
advanced
2:30remaining
Why does the cache not store values in this Spring Boot example?
Given this code, why is the cache never hit and the method always executes?
Spring Boot
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Cacheable("users")
    public String getUser(int id) {
        System.out.println("Loading user " + id);
        return "User-" + id;
    }
}

// Called from the same class method:
// public void process() {
//     getUser(1);
//     getUser(1);
// }
ABecause the method parameter is a primitive type, caching does not work with primitives.
BBecause the cache name "users" is invalid and caching silently fails.
CBecause @Cacheable requires @CachePut to work properly in the same class.
DBecause the method is called from within the same class, Spring's proxy does not intercept the call, so caching is bypassed.
Attempts:
2 left
💡 Hint
Think about how Spring AOP proxies work and method calls inside the same class.
❓ state_output
advanced
2:00remaining
What is the size of the cache after these calls?
Given a cache named "products" and this sequence of calls, how many entries are stored in the cache?
Spring Boot
productService.getProduct(1);
productService.getProduct(2);
productService.getProduct(1);
productService.getProduct(3);
productService.getProduct(2);
A5 entries
B3 entries
C2 entries
D4 entries
Attempts:
2 left
💡 Hint
Remember that cache stores unique keys and repeated calls with same key do not add new entries.
🧠 Conceptual
expert
3:00remaining
Which statement about Spring Boot cache eviction is correct?
Consider a method annotated with @CacheEvict(value = "sessions", allEntries = true). What does this annotation do when the method is called?
AIt removes all entries from the "sessions" cache immediately after the method executes.
BIt removes only the cache entry matching the method's argument from "sessions" before the method executes.
CIt disables caching for the "sessions" cache permanently.
DIt clears the cache only if the method throws an exception.
Attempts:
2 left
💡 Hint
Focus on the meaning of allEntries=true in @CacheEvict.

Practice

(1/5)
1. What is the primary purpose of using cache configuration in a Spring Boot application?
easy
A. To temporarily store data and speed up application performance
B. To permanently save data to the database
C. To encrypt sensitive data before sending it
D. To manage user authentication and authorization

Solution

  1. Step 1: Understand caching concept

    Caching stores data temporarily to avoid repeated processing or fetching.
  2. Step 2: Identify caching benefit in Spring Boot

    It speeds up the app by reusing stored data instead of recalculating or fetching again.
  3. Final Answer:

    To temporarily store data and speed up application performance -> Option A
  4. Quick Check:

    Cache = Temporary storage for speed [OK]
Hint: Cache means temporary storage to speed up apps [OK]
Common Mistakes:
  • Confusing cache with permanent database storage
  • Thinking cache encrypts data
  • Mixing cache with security features
2. Which annotation is used to enable caching support in a Spring Boot application?
easy
A. @EnableCaching
B. @Cacheable
C. @CacheConfig
D. @CachePut

Solution

  1. Step 1: Recall Spring Boot caching setup

    To activate caching, Spring Boot requires a special annotation on a configuration class.
  2. Step 2: Identify the correct annotation

    @EnableCaching enables the caching mechanism globally in the app.
  3. Final Answer:

    @EnableCaching -> Option A
  4. Quick Check:

    @EnableCaching activates caching [OK]
Hint: Enable caching with @EnableCaching annotation [OK]
Common Mistakes:
  • Using @Cacheable to enable caching (it marks methods, not enable)
  • Confusing @CachePut with enabling caching
  • Thinking @CacheConfig enables caching globally
3. Given the code below, what will be the output when getUser(1) is called twice?
@Cacheable("users")
public User getUser(int id) {
    System.out.println("Fetching user from DB");
    return new User(id, "Name" + id);
}
medium
A. Fetching user from DB printed twice, two User objects returned
B. No output printed, both calls return null
C. Fetching user from DB printed once, but both calls create new User
D. Fetching user from DB printed once, second call returns cached User

Solution

  1. Step 1: Understand @Cacheable behavior

    The first call runs the method and caches the result; subsequent calls return cached data.
  2. Step 2: Analyze output for two calls

    "Fetching user from DB" prints only once on first call; second call uses cache, no print.
  3. Final Answer:

    Fetching user from DB printed once, second call returns cached User -> Option D
  4. Quick Check:

    @Cacheable caches after first call [OK]
Hint: First call runs method, next calls use cache [OK]
Common Mistakes:
  • Assuming method runs every time ignoring cache
  • Thinking cache returns null if not found
  • Confusing @Cacheable with @CachePut behavior
4. Identify the error in this cache configuration snippet:
@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public CacheManager cacheManager() {
        return new CacheManager();
    }
}
medium
A. Missing @Cacheable annotation on methods
B. CacheManager is an interface and cannot be instantiated directly
C. Missing @EnableCaching annotation
D. CacheManager bean method should be static

Solution

  1. Step 1: Check CacheManager instantiation

    CacheManager is an interface; you cannot create it with new CacheManager().
  2. Step 2: Correct way to create CacheManager bean

    You must return a concrete implementation like ConcurrentMapCacheManager.
  3. Final Answer:

    CacheManager is an interface and cannot be instantiated directly -> Option B
  4. Quick Check:

    CacheManager interface needs implementation [OK]
Hint: CacheManager is interface; use implementation class [OK]
Common Mistakes:
  • Trying to instantiate interface directly
  • Forgetting to add @EnableCaching
  • Thinking @Cacheable is needed on config class
5. You want to configure a cache in Spring Boot that stores user data and expires entries after 10 minutes. Which approach correctly combines cache configuration and expiration?
hard
A. Use @EnableCaching and define a ConcurrentMapCacheManager bean with TTL set to 10 minutes
B. Use @CachePut and manually remove cache entries after 10 minutes
C. Use @EnableCaching and configure a CaffeineCacheManager with expiry policy of 10 minutes
D. Use @Cacheable with a parameter to set expiration time to 10 minutes

Solution

  1. Step 1: Understand cache expiration support

    ConcurrentMapCacheManager does not support TTL (time to live) expiration by default.
  2. Step 2: Identify cache manager with TTL support

    CaffeineCacheManager supports expiration policies like 10-minute TTL.
  3. Step 3: Evaluate options

    @Cacheable does not accept expiration parameters; @CachePut updates cache but does not manage expiration automatically.
  4. Final Answer:

    Use @EnableCaching and configure a CaffeineCacheManager with expiry policy of 10 minutes -> Option C
  5. Quick Check:

    CaffeineCacheManager supports TTL expiration [OK]
Hint: Use CaffeineCacheManager for TTL expiration [OK]
Common Mistakes:
  • Assuming ConcurrentMapCacheManager supports TTL
  • Trying to set expiration via @Cacheable annotation
  • Using @CachePut to handle expiration manually
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