Bird
0
0

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

medium📝 component behavior Q13 of 15
Spring Boot - Docker and Deployment

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");
    }
}
AProdService active
BDevService active
CBoth DevService active and ProdService active
DNo output printed
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes