0
0
Spring Bootframework~20 mins

@Configuration and @Bean in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot configuration?
Given the following Spring Boot configuration class, what will be printed when the application context is started and the messageService bean is used to print the message?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public String messageService() {
        return "Hello from @Bean!";
    }
}

// In main application or test:
// System.out.println(context.getBean("messageService"));
AHello from @Bean!
Bnull
CCompilation error due to missing @ComponentScan
DRuntime error: NoSuchBeanDefinitionException
Attempts:
2 left
💡 Hint
Remember that @Bean methods define beans in the Spring context and return the object to be managed.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a @Bean method in a @Configuration class?
Select the option that correctly defines a Spring bean method inside a @Configuration class.
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    // Choose the correct method below
}
A
@Bean
public String greeting() {
    return "Hi!";
}
B
@Bean
public String greeting(String name) {
    return "Hi " + name;
}
C
@Bean
public void greeting() {
    System.out.println("Hi!");
}
D
public String greeting() {
    return "Hi!";
}
Attempts:
2 left
💡 Hint
A @Bean method must return an object to be managed by Spring and have no parameters unless they are other beans.
🔧 Debug
advanced
2:00remaining
Why does this @Bean method cause a runtime error?
Consider this @Configuration class: @Configuration public class Config { @Bean public Integer number() { return Integer.parseInt("abc"); } } What happens when the Spring context starts?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public Integer number() {
        return Integer.parseInt("abc");
    }
}
ABean 'number' is created with value 0
BBean 'number' is created with null value
CApplication context fails to start with NumberFormatException
DCompilation error due to invalid parse
Attempts:
2 left
💡 Hint
Parsing "abc" as an integer is invalid and throws an exception.
🧠 Conceptual
advanced
2:00remaining
What is the role of @Configuration in Spring Boot?
Choose the best description of what @Configuration does in a Spring Boot application.
A@Configuration is used to inject dependencies into beans
B@Configuration marks a class as a source of bean definitions for the application context
C@Configuration automatically scans all packages for components
D@Configuration replaces the need for @Component annotations
Attempts:
2 left
💡 Hint
Think about what Spring does with classes annotated with @Configuration.
state_output
expert
2:00remaining
What is the value of the bean 'counter' after context initialization?
Given this configuration class: @Configuration public class CounterConfig { private int count = 0; @Bean public Integer counter() { return ++count; } @Bean public Integer anotherCounter() { return ++count; } } What is the value of the bean named 'counter' when retrieved from the context?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CounterConfig {
    private int count = 0;

    @Bean
    public Integer counter() {
        return ++count;
    }

    @Bean
    public Integer anotherCounter() {
        return ++count;
    }
}
AThrows an exception due to bean initialization order
B2
C0
D1
Attempts:
2 left
💡 Hint
Each @Bean method is called once during context initialization in some order.