Step 1: Analyze the code flow when callService() fails
If callService() returns null (failure), the code fetches cached data as fallback.
Step 2: Determine the returned value
The fallback cached data is returned instead of null or error.
Final Answer:
Cached data as fallback -> Option A
Quick Check:
Fallback cached data returned on failure [OK]
Hint: Null response triggers fallback to cached data [OK]
Common Mistakes:
Assuming error message is returned
Thinking null is returned directly
Confusing empty string with fallback data
4. A microservice uses this code snippet for graceful degradation:
try {
data = fetchFromService()
} catch (Exception e) {
data = null
}
return data.toString()
What is the main problem with this code?
medium
A. It does not handle exceptions properly
B. It returns null.toString() causing a runtime error
C. It always returns an empty string
D. It retries the service call infinitely
Solution
Step 1: Understand exception handling and return statement
If fetchFromService() fails, data is set to null, then data.toString() is called.
Step 2: Identify the error caused by calling toString() on null
Calling toString() on null causes a runtime NullPointerException or similar error.
Final Answer:
It returns null.toString() causing a runtime error -> Option B
Quick Check:
Calling toString() on null causes error [OK]
Hint: Calling method on null causes runtime error [OK]
Common Mistakes:
Ignoring null check before toString()
Assuming exception is handled fully
Thinking it retries infinitely
5. You design a microservice system where the payment service may fail. To apply graceful degradation, which approach is best?
hard
A. Return a simplified confirmation without payment details and log failure for retry
B. Block the entire order process until payment service recovers
C. Send an error response to the user immediately without fallback
D. Remove the payment service and process orders without payment
Solution
Step 1: Understand graceful degradation for critical service failure
When payment service fails, system should still respond with limited info, not block or error out.
Step 2: Evaluate options for best graceful degradation
Return a simplified confirmation without payment details and log failure for retry returns simplified confirmation and logs failure for retry, maintaining user experience and system reliability.
Final Answer:
Return a simplified confirmation without payment details and log failure for retry -> Option A