0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.