🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Spring Boot Caching: all 10 questions

Quiz: Spring Boot Caching · Question 3 of 10

What does this print? (code)

What does this print?

import java.util.*;

public class Main {
    static int dbCalls = 0;
    static Map<Integer, Integer> cache = new HashMap<>();

    static int fee(int courseId) {
        return cache.computeIfAbsent(courseId, id -> { dbCalls++; return id * 1000; });
    }

    public static void main(String[] args) {
        int total = fee(2) + fee(3) + fee(2) + fee(2);
        System.out.println(total + " " + dbCalls);
    }
}
  1. A.9000 4
  2. B.9000 2
  3. C.5000 2
  4. D.9000 3
Answer & explanation

Correct answer: 9000 2

Fees: 2000 + 3000 + 2000 + 2000 = 9000; only ids 2 and 3 are computed once each.

Can you get the rest right without peeking?

Take the timed Quiz: Spring Boot Caching quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →