Add interview module: 30 design pattern interview questions with tests (Java 25, Spring 7.0.10)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b7rnC7jppGGdHGESvy
This commit is contained in:
Claude
2026-10-28 18:10:09 +00:00
parent f5688a6b32
commit c51641b7db
48 changed files with 1617 additions and 1 deletions
+2
View File
@@ -0,0 +1,2 @@
openjdk version "25.0.5" 2026-10-20 LTS
tests: 31
+13
View File
@@ -0,0 +1,13 @@
Q1 eager: getInstance() == getInstance() -> true
Q2 naive lazy, 8 threads: distinct instances=more than 1 constructor runs=8
Q3 synchronized, 8 threads: distinct instances=1 constructor runs=1
Q4 DCL with volatile, 8 threads: distinct instances=1 constructor runs=1
Q4 DclSingleton.instance volatile? true
Q4 DclWithoutVolatile.instance volatile? false
Q5 holder before first getInstance(): constructor runs=0
Q5 holder after two getInstance() calls: constructor runs=1 same=true
Q6 serialisation round trip keeps the same instance? classic=false hardened(readResolve)=true enum=true
Q6 reflection newInstance() on classic private constructor returns the singleton? false
Q6 reflection on hardened constructor: InvocationTargetException caused by java.lang.IllegalStateException: already initialised
Q6 reflection on enum constructor: java.lang.IllegalArgumentException: Cannot reflectively create enum objects
Q7 Spring singleton scope: same bean twice from one context? true; same bean across two contexts? false
+10
View File
@@ -0,0 +1,10 @@
Q8 simple factory: button("dark") -> [ dark button ]
Q8 factory method: new DarkDialog().show() -> dialog with [ dark button ]
Q9 LIGHT factory -> button=light checkbox=light
Q9 DARK factory -> button=dark checkbox=dark
Q10 builder defaults: method=GET timeout=30; headers map mutable? false
Q10 build() without url -> NullPointerException: url is required
Q10 JDK HttpRequest.newBuilder(uri).build() default method=GET
Q11 original tags after both copies were edited: [java, added-via-shallow]
Q11 shallow.tags() == original.tags()? true; deep.tags() == original.tags()? false
Q11 ArrayList.clone() shares elements: original.get(0) is now "a!"
+17
View File
@@ -0,0 +1,17 @@
Q12 state: advance() x3 -> NEW -> PAID -> SHIPPED -> DELIVERED
Q12 state: advance() on DELIVERED -> IllegalStateException: already delivered
Q12 strategy: total(1000) with NONE=1000.0 then FESTIVAL=900.0
Q13 sort by length (Comparator strategy): [fig, pear, banana]
Q13 sort reversed alphabetical: [pear, fig, banana]
Q13 inline lambda strategy: total(200)=150.0
Q14 publishNaive, 2 listeners, first unsubscribes itself: delivered to [first] (no exception, second was skipped)
Q14 publishNaive, 3 listeners, first unsubscribes itself -> ConcurrentModificationException
Q14 publishSafe, two events, self-unsubscribing listener: [once, other, other]
Q14 java.util.Observable @Deprecated(since="9")
Q15 template method run(): [open, sales rows, close]
Q15 strategy version runWith(): [open, ad-hoc rows, close]
Q15 run() is final (subclasses cannot reorder the steps): true
Q16 valid request -> 200 ok via [auth, rate-limit, controller]
Q16 no token -> 401 via [auth]
Q17 after two commands: "Hello, world"; undo -> "Hello"; undo -> ""
Q18 2 + 3 * 4 via sealed switch=14 via Visitor=14
+12
View File
@@ -0,0 +1,12 @@
Q19 LazyProxy: RealStorage instances before first read=0, after two reads=1
Q19 ProtectionProxy: read("secret-1") -> SecurityException: denied: secret-1
Q19 Decorators stacked Logging(Caching(Real)): read("k") twice -> log=[read k, read k]
Q20 proxy.greet("Ankur") -> hello Ankur; intercepted=[greet]
Q20 Proxy.isProxyClass(proxy.getClass())=true; proxy instanceof Greeter.Impl=false
Q20 newProxyInstance with a class instead of an interface -> IllegalArgumentException (... is not an interface)
Q21 adapter: 98.6F read through CelsiusSensor -> 37.0 C
Q21 facade: watchMovie() -> [lights dimmed, projector on, surround sound on]
Q21 JDK adapter InputStreamReader(InputStream) first char: h
Q22 size() of a leaf=10, of sub dir=25, of root=35
Q23 "hello world" has 11 characters but the pool holds 8 Glyph objects
Q23 Integer.valueOf: 127==127 -> true, 128==128 -> false
+18
View File
@@ -0,0 +1,18 @@
Q24 getBean("connection") -> Connection
Q24 getBean("&connection") -> ConnectionFactoryBean
Q24 ApplicationContext is itself a BeanFactory? true
Q25 target with interface, defaults -> jdkDynamicProxy=true cglib=false
Q25 target with interface, proxyTargetClass -> jdkDynamicProxy=false cglib=true
Q25 target with no interface -> jdkDynamicProxy=false cglib=true
Q25 JDK proxy instanceof GreeterImpl? false; CGLIB proxy instanceof GreeterImpl? true
Q26 proxy.outer() returned 2; advice saw [outer] (inner() ran, but was never intercepted)
Q26 proxy.inner() called from outside; advice saw [inner]
Q26 final method on a CGLIB proxy: finalMethod() -> "name=null", advice saw []
Q27 publishEvent(OrderPlaced): listener ran on the caller's thread? true
Q28 TransactionTemplate success path: [begin, work, commit] returned done
Q28 TransactionTemplate when the callback throws: [begin, work, rollback]
Q29 Map<String, Payment> injected with keys [card, upi]
Q29 pay("upi", 500) -> upi:500
Q30 getBean(Ticket) twice from the context: same instance? false
Q30 prototype injected into a singleton: same Ticket on every call? true
Q30 prototype fetched via ObjectProvider: same Ticket on every call? false
@@ -0,0 +1,4 @@
Q18Broken.java:9: error: the switch expression does not cover all possible input values
return switch (e) {
^
1 error