Bird
0
0

What will be the output of this pseudocode if the API key is missing?

medium📝 Analysis Q5 of 15
Microservices - Authentication and Authorization
What will be the output of this pseudocode if the API key is missing?
function validateKey(request) {
  key = request.headers['Authorization']
  if (!key) {
    return 'No API key provided'
  }
  if (key == 'valid-key') {
    return 'Access granted'
  }
  return 'Access denied'
}
print(validateKey({headers: {}}))
ANo API key provided
BAccess denied
CAccess granted
DError: missing header
Step-by-Step Solution
Solution:
  1. Step 1: Check if key exists in headers

    The headers object is empty, so key is undefined or falsey.
  2. Step 2: Follow conditional for missing key

    The code returns 'No API key provided' when key is missing.
  3. Final Answer:

    No API key provided -> Option A
  4. Quick Check:

    Missing key returns 'No API key provided' [OK]
Quick Trick: Missing keys trigger specific error messages [OK]
Common Mistakes:
MISTAKES
  • Assuming missing key equals access denied
  • Expecting runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes