Practice
Piece abstraction be an abstract class or an interface, and why?Solution
Step 1: Analyze shared state among pieces
All pieces share common state such as position on the board and color, which is best encapsulated in a base class.Step 2: Evaluate abstract class vs interface
Abstract classes allow shared state and common method implementations, while interfaces primarily declare behavior without state.Final Answer:
Option D -> Option DQuick Check:
Shared state -> abstract class preferred over interface [check]
TrafficLightController class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits traffic light controller
Only one controller should coordinate the traffic lights to avoid conflicting states; multiple instances would cause inconsistent signals.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
File and Directory classes, should FileSystemEntity be an abstract class or an interface? Why?Solution
Step 1: Identify shared state and behavior
Both File and Directory share common state such as name, permissions, timestamps, so a base class holding this state is needed.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance fields; abstract classes can hold state and provide common method implementations, making abstract class the correct choice.Final Answer:
Option D -> Option DQuick Check:
Shared state -> abstract class, not interface [check]
Post entity for Facebook, should Post be an abstract class or an interface, considering it has state like content and timestamp, and behavior like edit() and delete()? Why?Solution
Step 1: Analyze state and behavior requirements
Post has shared state (content, timestamp) and behavior (edit, delete) that benefit from default implementations.Step 2: Abstract class vs interface capabilities
Abstract classes can hold state and provide method implementations; interfaces cannot hold state (pre-Java 8) and only declare methods.Final Answer:
Option A -> Option AQuick Check:
State + behavior with default code -> Abstract class [check]
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available, then context switches before marking it occupied.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available and proceeds to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> both threads see available -> double-booking [check]
