Bird
0
0

Given this configuration class, what will be the output when retrieving the bean named "calculator" and calling add(2, 3)?

medium📝 component behavior Q4 of 15
Spring Boot - Spring Annotations
Given this configuration class, what will be the output when retrieving the bean named "calculator" and calling add(2, 3)?
@Configuration
public class AppConfig {
  @Bean
  public Calculator calculator() {
    return new Calculator();
  }
}

public class Calculator {
  public int add(int a, int b) { return a + b; }
}
A5
B23
C0
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand bean creation

    The calculator bean is created by the method returning a new Calculator instance.
  2. Step 2: Method call behavior

    Calling add(2, 3) returns the sum 5 as defined in the Calculator class.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Bean method returns Calculator; add(2,3) = 5 [OK]
Quick Trick: Bean methods return objects used at runtime [OK]
Common Mistakes:
  • Confusing string concatenation with addition
  • Expecting errors from simple bean methods
  • Misreading method return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes