Practice
Player abstraction for the Snake and Ladder game, should Player be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements
ThePlayerhas common state such as current position on the board and methods like move().Step 2: Choose abstraction type
An abstract class allows sharing state and default method implementations, which interfaces (pre-Java 8) cannot provide.Final Answer:
Option B -> Option BQuick Check:
Abstract class holds shared state and behavior [check]
Transaction class uses a private constructor and exposes a static synchronized getInstance() method to create transaction objects. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from private constructor and static synchronized getInstance()
This is a classic Singleton pattern implementation ensuring only one instance per JVM with thread-safe lazy initialization.Step 2: Eliminate other patterns
Factory Method involves subclassing and delegation, not private constructors. Builder separates construction steps, unrelated here. Singleton pattern, because it ensures only one Transaction instance exists system-wide. is wrong because Singleton is about single instance, but synchronized getInstance() is the key for thread safety.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
getInstance() method that returns the single instance. Which design pattern is applied here and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls instance creation, which matches Singleton pattern characteristics.Step 2: Explain why Singleton fits this use case
Singleton ensures a single instance with thread-safe access via synchronization, suitable for shared resources like configuration or logging.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Solution
Step 1: Analyze shared code in states
Elevator states often share common fields (e.g., reference to context) and helper methods, which favors an abstract class.Step 2: Evaluate interface limitations
Interfaces cannot hold state or provide default implementations (prior to Java 8), so they are less suitable if shared code exists.Final Answer:
Option B -> Option BQuick Check:
Shared code and state -> abstract class [check]
FollowService, the method followUser() checks if the follower already follows the followee before adding. Without synchronization, which exact thread interleaving causes a double-follow entry?Solution
Step 1: Identify the check-then-act race condition
Both threads callisFollowing()and get false before either adds the follower, due to lack of synchronization.Step 2: Resulting double addition
Both threads proceed to add the follower, causing duplicate entries.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap -> both see false -> both add follower [check]
