Bird
0
0

In a microservice using Java, what will be the output of this code if the environment variable PORT is set to 8080?

medium📝 Analysis Q5 of 15
Microservices - Configuration and Secrets Management
In a microservice using Java, what will be the output of this code if the environment variable PORT is set to 8080?
String port = System.getenv("PORT");
System.out.println("Service running on port: " + port);
AService running on port: null
BService running on port: PORT
CService running on port: 8080
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand System.getenv behavior

    System.getenv returns the string value of the environment variable if set.
  2. Step 2: Apply to given code

    Since PORT=8080, the output concatenates and prints 'Service running on port: 8080'.
  3. Final Answer:

    Service running on port: 8080 -> Option C
  4. Quick Check:

    Java getenv returns env var value = B [OK]
Quick Trick: Java getenv returns string or null if unset [OK]
Common Mistakes:
  • Expecting null output when variable is set
  • Confusing variable name with value
  • Thinking code won't compile

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Microservices Quizzes