Which Singleton implementation is safe against reflection and serialization by default?
The JVM guarantees enum constants are instantiated once and cannot be created reflectively or by deserialisation.
SOLID and the Gang of Four patterns — intent, structure, trade-offs and real-world usage.
Pick an answer to see instantly whether you're right, with an explanation. Questions are shuffled every attempt.
The JVM guarantees enum constants are instantiated once and cannot be created reflectively or by deserialisation.
enum S { INSTANCE; }
System.out.println(S.INSTANCE == S.valueOf("INSTANCE"));valueOf returns the existing constant — there is only one.
An abstract factory has one method per product and guarantees products come from the same family.
A builder gives readable, validated construction of an immutable result.
new BufferedReader(new InputStreamReader(new FileInputStream(f))) is an example of…Each wrapper adds behaviour around the same abstraction. InputStreamReader also adapts bytes to characters.
Arrays.asList(array) letting an array be used as a List is an example of…It converts one interface (an array) into another the client expects (List).
@Transactional?A proxy intercepts calls and wraps them in begin/commit/rollback — which is also why self-invocation bypasses it.
Leaves and composites share one interface; composites delegate to their children recursively.
Integer a = 127, b = 127; a == b true?Values -128..127 are cached flyweights. For 128 you get two objects, so always use equals.
The java.sql abstraction and the driver implementations vary independently.
Each filter handles the request, passes it on, or stops the chain (e.g. returning 401).
States trigger transitions; with Strategy the client chooses the algorithm.
Commands can be stored, re-executed and reversed — often combined with Memento snapshots.
JdbcTemplate.query(sql, rowMapper) follows which pattern?The fixed algorithm (connection, statement, iteration, cleanup, exception translation) calls your supplied step.
They give type-safe dispatch over a closed hierarchy without accept/visit boilerplate.
Square extends Rectangle where setWidth also changes the height violates which principle?Code written for Rectangle breaks when given a Square, so it isn’t substitutable.
Robot class forced to implement eat() from a Worker interface signals a violation of…Split fat interfaces into role interfaces such as Workable and Feedable.
PaymentStrategy bean into a Map and choosing one at runtime applies which pattern and principle?New payment methods are new classes; the service never changes.
You scored 0 out of 18.