Bird
Raised Fist0
Spring Bootframework~20 mins

Environment variables in configuration in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Environment Variable Mastery in Spring Boot
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Spring Boot resolve environment variables in configuration?
Given the application.properties file with the line app.name=${APP_NAME:DefaultApp}, what will be the value of app.name if the environment variable APP_NAME is not set?
AThe value will be '${APP_NAME:DefaultApp}' as a literal string.
BThe value will be 'DefaultApp' because the default is used when the environment variable is missing.
CThe value will be empty because the environment variable is missing and no value is set.
DSpring Boot will throw an error because the environment variable is not set.
Attempts:
2 left
💡 Hint
Look at how Spring Boot uses the syntax ${VAR:default} to provide fallback values.
📝 Syntax
intermediate
2:00remaining
Which syntax correctly injects an environment variable with no default in Spring Boot?
You want to inject the environment variable DB_PASSWORD into your Spring Boot configuration without a default value. Which syntax is correct?
A${DB_PASSWORD}
B${DB_PASSWORD:}
C${DB_PASSWORD?}
D${DB_PASSWORD:null}
Attempts:
2 left
💡 Hint
Check the standard Spring Boot syntax for environment variable placeholders.
🔧 Debug
advanced
2:30remaining
Why does this Spring Boot configuration fail to load the environment variable?
Consider this line in application.yml:
database: url: jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydb
The application fails to start with an error about unresolved placeholders. What is the most likely cause?
Spring Boot
database:
  url: jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydb
AThe port number 3306 is invalid and causes the error.
BThe syntax for default value in YAML is incorrect; it should use a different delimiter.
CSpring Boot does not support environment variables in YAML files.
DThe environment variable DB_HOST is not set and no default is provided, causing the error.
Attempts:
2 left
💡 Hint
Check if all environment variables used have defaults or are set.
🧠 Conceptual
advanced
2:30remaining
How does Spring Boot prioritize configuration sources for environment variables?
Spring Boot can get configuration values from multiple sources. Which order correctly describes how Spring Boot prioritizes environment variables compared to application.properties and command-line arguments?
AEnvironment variables > Command-line arguments > application.properties
Bapplication.properties > Environment variables > Command-line arguments
CCommand-line arguments > Environment variables > application.properties
Dapplication.properties > Command-line arguments > Environment variables
Attempts:
2 left
💡 Hint
Think about which source is most immediate when starting the app.
❓ state_output
expert
3:00remaining
What is the value of the injected field after running this Spring Boot component?
Given this Spring Boot component:
@Component
public class MyComponent {

  @Value("${MY_VAR:defaultValue}")
  private String myVar;

  public String getMyVar() {
    return myVar;
  }
}

If the environment variable MY_VAR is set to an empty string, what will getMyVar() return?
Spring Boot
@Component
public class MyComponent {

  @Value("${MY_VAR:defaultValue}")
  private String myVar;

  public String getMyVar() {
    return myVar;
  }
}
AAn empty string, because the environment variable is set but empty.
BThe string 'defaultValue', because empty environment variables are treated as missing.
Cnull, because the environment variable is empty and no value is assigned.
DA runtime error occurs because empty strings are invalid for @Value injection.
Attempts:
2 left
💡 Hint
Consider how Spring Boot treats environment variables set to empty strings versus unset variables.

Practice

(1/5)
1. What is the correct way to reference an environment variable named DB_HOST in a Spring Boot application.properties file?
easy
A. Use #DB_HOST# to reference the environment variable.
B. Use ${DB_HOST} to reference the environment variable.
C. Use env(DB_HOST) to reference the environment variable.
D. Use DB_HOST directly without any syntax.

Solution

  1. Step 1: Understand Spring Boot environment variable syntax

    Spring Boot uses ${VAR_NAME} syntax to inject environment variables into configuration files.
  2. Step 2: Match the syntax to the variable name

    For the variable DB_HOST, the correct syntax is ${DB_HOST}.
  3. Final Answer:

    Use ${DB_HOST} to reference the environment variable. -> Option B
  4. Quick Check:

    Environment variable syntax = ${VAR_NAME} [OK]
Hint: Remember: Spring Boot uses ${VAR_NAME} for env variables [OK]
Common Mistakes:
  • Using #VAR_NAME# instead of ${VAR_NAME}
  • Trying to call env(VAR_NAME) like a function
  • Using variable name without any syntax
2. Which of the following is the correct syntax to provide a default value for an environment variable PORT in application.properties?
easy
A. server.port=${PORT:8080}
B. server.port=${PORT:-8080}
C. server.port=${PORT=8080}
D. server.port=$PORT?8080

Solution

  1. Step 1: Recall default value syntax in Spring Boot

    Spring Boot uses ${VAR_NAME:defaultValue} to provide a default if the environment variable is missing.
  2. Step 2: Identify the correct syntax among options

    server.port=${PORT:8080} uses ${PORT:8080}, which is the correct way to set default port 8080.
  3. Final Answer:

    server.port=${PORT:8080} -> Option A
  4. Quick Check:

    Default value syntax = ${VAR_NAME:default} [OK]
Hint: Use colon : after variable name for default values [OK]
Common Mistakes:
  • Using = instead of : for default values
  • Using shell-style ${VAR:-default} which is invalid here
  • Using $VAR without braces or default
3. Given the following application.properties snippet:
app.name=${APP_NAME:MyApp}
app.port=${APP_PORT:9090}

If the environment variables APP_NAME is set to TestApp and APP_PORT is not set, what will be the values of app.name and app.port at runtime?
medium
A. app.name=TestApp and app.port=9090
B. app.name=MyApp and app.port=9090
C. app.name=TestApp and app.port=null
D. app.name=null and app.port=9090

Solution

  1. Step 1: Check environment variable APP_NAME

    APP_NAME is set to 'TestApp', so app.name uses this value, ignoring the default.
  2. Step 2: Check environment variable APP_PORT

    APP_PORT is not set, so app.port uses the default value 9090.
  3. Final Answer:

    app.name=TestApp and app.port=9090 -> Option A
  4. Quick Check:

    Set var overrides default; unset uses default [OK]
Hint: Set env var overrides default; unset uses default [OK]
Common Mistakes:
  • Assuming default is used even if env var is set
  • Assuming null if env var is missing without default
  • Confusing variable names or values
4. Consider this application.properties line:
database.url=jdbc:mysql://${DB_HOST}:${DB_PORT:3306}/mydb

When running the app, it throws an error because DB_HOST is not set. What is the best fix?
medium
A. Ignore the error; Spring Boot will use a fallback automatically.
B. Remove ${DB_HOST} and hardcode the host.
C. Set a default value for DB_HOST like ${DB_HOST:localhost}.
D. Change syntax to ${DB_HOST:-localhost} to fix it.

Solution

  1. Step 1: Identify cause of error

    DB_HOST is not set and no default is provided, causing Spring Boot to fail resolving the variable.
  2. Step 2: Provide a default value using correct syntax

    Use ${DB_HOST:localhost} to supply a default host if the environment variable is missing.
  3. Final Answer:

    Set a default value for DB_HOST like ${DB_HOST:localhost}. -> Option C
  4. Quick Check:

    Missing env var needs default to avoid errors [OK]
Hint: Always provide defaults for required env vars to avoid errors [OK]
Common Mistakes:
  • Using shell syntax ${VAR:-default} which Spring Boot does not support
  • Assuming Spring Boot auto-fills missing env vars
  • Hardcoding values reduces flexibility
5. You want to configure a Spring Boot app to read a nested environment variable for a Redis URL with fallback defaults. Which application.properties line correctly uses environment variables REDIS_HOST and REDIS_PORT with defaults localhost and 6379 respectively?
hard
A. redis.url=redis://$REDIS_HOST:$REDIS_PORT
B. redis.url=redis://${REDIS_HOST:-localhost}:${REDIS_PORT:-6379}
C. redis.url=redis://${REDIS_HOST}${REDIS_PORT}
D. redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379}

Solution

  1. Step 1: Recall Spring Boot default value syntax

    Spring Boot uses ${VAR_NAME:default} to provide defaults for environment variables.
  2. Step 2: Check each option for correct syntax and completeness

    redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379} correctly uses ${REDIS_HOST:localhost} and ${REDIS_PORT:6379} with colon syntax and includes the colon separator between host and port.
  3. Final Answer:

    redis.url=redis://${REDIS_HOST:localhost}:${REDIS_PORT:6379} -> Option D
  4. Quick Check:

    Use colon : for defaults; include separators explicitly [OK]
Hint: Use ${VAR:default} and include separators like : explicitly [OK]
Common Mistakes:
  • Using shell-style :- instead of : for defaults
  • Omitting colon between host and port
  • Using $VAR without braces or defaults
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard