Bird
Raised Fist0
Spring Bootframework~20 mins

Environment-based profiles in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Boot Profiles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does Spring Boot select active profiles at runtime?

Consider a Spring Boot application with multiple profiles defined: dev, test, and prod. If the application is started with the JVM argument -Dspring.profiles.active=test, what will be the active profile during runtime?

ANo profile is active because JVM arguments do not affect Spring profiles.
BOnly the <code>test</code> profile is active.
CAll profiles <code>dev</code>, <code>test</code>, and <code>prod</code> are active.
DThe <code>prod</code> profile is active by default, ignoring the JVM argument.
Attempts:
2 left
💡 Hint

Think about how Spring Boot reads the spring.profiles.active property.

📝 Syntax
intermediate
2:00remaining
Which YAML snippet correctly activates the 'prod' profile in Spring Boot?

Given the following YAML configuration, which option correctly sets the active profile to prod?

Spring Boot
spring:
  profiles:
    active: ???
Aactive: prod
Bactive: 'prod'
Cactive: [prod]
D
active:
  - prod
Attempts:
2 left
💡 Hint

Remember how simple string values are set in YAML.

🔧 Debug
advanced
2:00remaining
Why does the 'dev' profile not activate despite being set in application.properties?

Given these two files:

application.properties:
spring.profiles.active=dev

application-dev.properties:
logging.level.root=DEBUG

The application logs do not show DEBUG level logs. What is the most likely cause?

AThe logging level property is misspelled in <code>application-dev.properties</code>.
BThe <code>application-dev.properties</code> file is not named correctly.
CThe <code>spring.profiles.active</code> property is overridden by an environment variable or JVM argument.
DSpring Boot does not support profile-specific properties files.
Attempts:
2 left
💡 Hint

Check if any external settings override the active profile.

❓ state_output
advanced
2:00remaining
What is the output of this Spring Boot profile conditional bean registration?

Consider this Spring Boot component:

@Component
@Profile("prod")
public class ProdService {
    public String getMessage() {
        return "Production Service";
    }
}

If the active profile is dev, what happens when the application tries to get ProdService bean?

ASpring throws a <code>NoSuchBeanDefinitionException</code> because <code>ProdService</code> is not loaded.
BSpring creates <code>ProdService</code> bean anyway ignoring the profile.
CSpring creates a proxy <code>ProdService</code> bean that returns null.
DSpring logs a warning but creates the bean.
Attempts:
2 left
💡 Hint

Think about how @Profile controls bean creation.

🧠 Conceptual
expert
3:00remaining
How does Spring Boot merge properties from multiple active profiles?

If a Spring Boot application activates two profiles simultaneously: dev and featureX, and both have application-dev.properties and application-featureX.properties files with overlapping keys, how does Spring Boot resolve the final property values?

AOnly properties from the first profile listed in <code>spring.profiles.active</code> are used.
BProperties from all profiles are merged, and the first profile listed overrides later ones.
CSpring Boot throws an error if multiple profiles define the same property.
DProperties from the last profile listed in <code>spring.profiles.active</code> override earlier ones.
Attempts:
2 left
💡 Hint

Consider the order of profiles in the spring.profiles.active property.

Practice

(1/5)
1.

What is the main purpose of using profiles in a Spring Boot application?

easy
A. To separate configuration for different environments like development and production
B. To speed up the application startup time
C. To enable multi-threading in the application
D. To automatically generate database schemas

Solution

  1. Step 1: Understand what profiles do

    Profiles allow you to define different settings for different environments, such as development, testing, or production.
  2. Step 2: Match purpose with options

    Only To separate configuration for different environments like development and production describes separating configuration for different environments, which is the main use of profiles.
  3. Final Answer:

    To separate configuration for different environments like development and production -> Option A
  4. Quick Check:

    Profiles = environment-specific settings [OK]
Hint: Profiles separate configs by environment, like dev vs prod [OK]
Common Mistakes:
  • Thinking profiles speed up startup
  • Confusing profiles with threading
  • Assuming profiles auto-generate schemas
2.

Which of the following is the correct way to activate a profile named dev in application.properties?

easy
A. profiles.active=dev
B. spring.profiles.active=dev
C. spring.active.profile=dev
D. spring.profile=dev

Solution

  1. Step 1: Recall the correct property key

    The correct property to activate profiles is spring.profiles.active.
  2. Step 2: Compare options

    Only spring.profiles.active=dev uses the exact correct key and value format.
  3. Final Answer:

    spring.profiles.active=dev -> Option B
  4. Quick Check:

    Activate profile with spring.profiles.active [OK]
Hint: Use spring.profiles.active to set active profile [OK]
Common Mistakes:
  • Using spring.active.profile instead
  • Omitting 'spring' prefix
  • Using singular 'profile' instead of 'profiles'
3.

Given the following Spring Boot code snippet, what will be printed if the active profile is prod?

@Component
@Profile("dev")
public class DevService {
    public DevService() {
        System.out.println("DevService active");
    }
}

@Component
@Profile("prod")
public class ProdService {
    public ProdService() {
        System.out.println("ProdService active");
    }
}
medium
A. ProdService active
B. DevService active
C. Both DevService active and ProdService active
D. No output printed

Solution

  1. Step 1: Identify active profile

    The active profile is prod, so only beans with @Profile("prod") will be created.
  2. Step 2: Determine which constructor runs

    Only ProdService constructor runs, printing "ProdService active".
  3. Final Answer:

    ProdService active -> Option A
  4. Quick Check:

    @Profile("prod") runs only with prod profile [OK]
Hint: Only beans matching active profile run constructors [OK]
Common Mistakes:
  • Assuming both beans run regardless of profile
  • Confusing dev and prod profiles
  • Thinking no output prints if profile mismatches
4.

Consider this Spring Boot configuration snippet in application.properties:

spring.profiles.active=dev,prod

What is the problem with this configuration?

medium
A. Multiple profiles can be active simultaneously, so no problem
B. Profiles should be separated by semicolon, not comma
C. Activating both dev and prod profiles together can cause conflicts
D. spring.profiles.active cannot be set in application.properties

Solution

  1. Step 1: Understand multiple active profiles

    Spring Boot allows multiple profiles active at once, but dev and prod usually have conflicting settings.
  2. Step 2: Identify the issue

    Activating both dev and prod together can cause unexpected behavior or conflicts in configuration.
  3. Final Answer:

    Activating both dev and prod profiles together can cause conflicts -> Option C
  4. Quick Check:

    Dev and prod active together causes config conflicts [OK]
Hint: Avoid activating dev and prod profiles simultaneously [OK]
Common Mistakes:
  • Thinking comma separator is invalid
  • Believing multiple profiles never cause issues
  • Assuming spring.profiles.active can't be set in properties
5.

You want to create a bean that only loads when test profile is active and another bean that loads for all profiles except test. Which annotation setup is correct?

hard
A. @Profile("test") on first bean, no annotation on second bean
B. No need for @Profile, beans load automatically
C. @Profile("test") on first bean, @Profile("prod") on second bean
D. @Profile("test") on first bean, @Profile("!test") on second bean

Solution

  1. Step 1: Use @Profile("test") for test-only bean

    This ensures the first bean loads only when the test profile is active.
  2. Step 2: Use @Profile("!test") for all except test

    The negation !test means the second bean loads when test is not active.
  3. Final Answer:

    @Profile("test") on first bean, @Profile("!test") on second bean -> Option D
  4. Quick Check:

    Use @Profile("!profile") to exclude a profile [OK]
Hint: Use @Profile("!test") to exclude test profile [OK]
Common Mistakes:
  • Not using @Profile on second bean causes it to always load
  • Using @Profile("prod") limits second bean to prod only
  • Assuming beans load automatically without profiles
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