Bird
Raised Fist0
Spring Bootframework~20 mins

@Scope for bean scope in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Bean Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output when using @Scope("prototype") on a Spring bean?

Consider a Spring bean annotated with @Scope("prototype"). What happens when you request this bean multiple times from the application context?

Spring Boot
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class MyBean {
}

// In main or test:
MyBean bean1 = context.getBean(MyBean.class);
MyBean bean2 = context.getBean(MyBean.class);
System.out.println(bean1 == bean2);
APrints false because each getBean call returns a new instance.
BPrints true because Spring caches the bean and returns the same instance.
CThrows a runtime exception because prototype scope is invalid here.
DPrints null because the bean is not created until explicitly initialized.
Attempts:
2 left
💡 Hint

Think about how prototype scope differs from singleton scope in Spring.

📝 Syntax
intermediate
1:30remaining
Which @Scope annotation value is correct to define a singleton bean in Spring?

Choose the correct way to specify singleton scope using the @Scope annotation in Spring.

A@Scope("single")
B@Scope("singleton")
C@Scope("global")
D@Scope("prototype")
Attempts:
2 left
💡 Hint

Singleton is the default scope but can be explicitly declared.

❓ state_output
advanced
2:00remaining
What is the output of this Spring bean with @Scope("request") in a non-web application?

Given a Spring bean annotated with @Scope("request"), what happens when you try to get this bean in a non-web Spring application?

Spring Boot
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("request")
public class RequestBean {
}

// In main or test:
RequestBean bean = context.getBean(RequestBean.class);
System.out.println(bean);
AThrows an exception because request scope requires a web context.
BReturns a singleton instance of the bean.
CReturns null because the bean is not created outside a web request.
DCreates a new instance but with default singleton behavior.
Attempts:
2 left
💡 Hint

Consider what 'request' scope means and where it is valid.

🔧 Debug
advanced
2:00remaining
Why does this Spring bean with @Scope("session") cause an error in a console app?

Examine the code below and explain why it causes an error when run in a console (non-web) Spring Boot application.

Spring Boot
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class SessionBean {
}

// In main or test:
SessionBean bean = context.getBean(SessionBean.class);
System.out.println(bean);
ASession scope defaults to prototype in non-web apps, so no error occurs.
BThe bean is null because session scope is not supported.
CThe bean is created as singleton ignoring the session scope.
DSession scope requires a web session, so it throws an exception in a non-web app.
Attempts:
2 left
💡 Hint

Think about what session scope means and where it is applicable.

🧠 Conceptual
expert
2:30remaining
How does Spring handle a prototype-scoped bean injected into a singleton bean?

Consider a singleton-scoped Spring bean that has a dependency on a prototype-scoped bean injected via constructor or field injection. What is the behavior of the prototype bean inside the singleton?

Spring Boot
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class PrototypeBean {
}

@Component
@Scope("singleton")
public class SingletonBean {
    private final PrototypeBean prototypeBean;
    public SingletonBean(PrototypeBean prototypeBean) {
        this.prototypeBean = prototypeBean;
    }
    public PrototypeBean getPrototypeBean() {
        return prototypeBean;
    }
}

// In main or test:
SingletonBean singleton = context.getBean(SingletonBean.class);
PrototypeBean p1 = singleton.getPrototypeBean();
PrototypeBean p2 = singleton.getPrototypeBean();
System.out.println(p1 == p2);
APrints null because prototype beans are not created automatically.
BPrints false because a new prototype bean is created each time getPrototypeBean() is called.
CPrints true because the prototype bean is created once and reused inside the singleton.
DThrows an exception because prototype beans cannot be injected into singletons.
Attempts:
2 left
💡 Hint

Think about when Spring creates prototype beans during injection.

Practice

(1/5)
1. What does the @Scope("prototype") annotation do in a Spring Boot bean?
easy
A. Creates a new bean instance per HTTP request
B. Creates only one shared instance of the bean for the entire application
C. Creates a new bean instance per HTTP session
D. Creates a new instance of the bean every time it is requested

Solution

  1. Step 1: Understand the prototype scope meaning

    The prototype scope means Spring creates a new bean instance each time it is requested from the container.
  2. Step 2: Compare with other scopes

    Unlike singleton which shares one instance, prototype always returns a fresh object.
  3. Final Answer:

    Creates a new instance of the bean every time it is requested -> Option D
  4. Quick Check:

    @Scope("prototype") = new instance each request [OK]
Hint: Prototype means new bean each request, singleton means shared [OK]
Common Mistakes:
  • Confusing prototype with singleton scope
  • Thinking prototype beans are shared
  • Mixing prototype with web scopes like session or request
2. Which of the following is the correct way to declare a singleton scoped bean using @Scope annotation in Spring Boot?
easy
A. @Scope("session")
B. @Scope("prototype")
C. @Scope("singleton")
D. @Scope("request")

Solution

  1. Step 1: Recall default and explicit singleton scope

    Singleton is the default scope in Spring, but explicitly it is declared as @Scope("singleton").
  2. Step 2: Verify other scopes

    prototype creates new instances, session and request are web scopes, not singleton.
  3. Final Answer:

    @Scope("singleton") -> Option C
  4. Quick Check:

    Singleton scope = @Scope("singleton") [OK]
Hint: Singleton scope is default but use @Scope("singleton") explicitly [OK]
Common Mistakes:
  • Using @Scope("prototype") for singleton beans
  • Confusing web scopes with singleton
  • Omitting @Scope and expecting prototype behavior
3. Consider this Spring Boot bean declaration:
@Component
@Scope("request")
public class UserRequestBean { }

What happens when this bean is injected into two different HTTP requests?
medium
A. Each request gets a new bean instance
B. Both requests share the same bean instance
C. Only the first request gets a bean, others get null
D. Bean is created once per session, shared across requests

Solution

  1. Step 1: Understand request scope behavior

    The request scope means Spring creates a new bean instance for each HTTP request.
  2. Step 2: Apply to two different requests

    Since the requests are different, each gets its own separate bean instance.
  3. Final Answer:

    Each request gets a new bean instance -> Option A
  4. Quick Check:

    @Scope("request") = new bean per HTTP request [OK]
Hint: Request scope means new bean per HTTP request [OK]
Common Mistakes:
  • Assuming request scope shares bean across requests
  • Confusing request scope with session scope
  • Thinking request scope behaves like singleton
4. Given this bean declaration:
@Component
@Scope("session")
public class ShoppingCart { }

What is the likely error if you try to inject this bean into a singleton scoped bean without special handling?
medium
A. No error, injection works normally
B. Runtime error due to incompatible scopes
C. Compilation error due to missing @Scope annotation
D. Bean is created once and shared across all sessions

Solution

  1. Step 1: Understand scope mismatch problem

    Injecting a session-scoped bean into a singleton bean causes a scope mismatch because singleton lives longer than session beans.
  2. Step 2: Result of mismatch without proxy

    Without proxy or special configuration, Spring throws a runtime error as it cannot inject a shorter-lived bean into a longer-lived one safely.
  3. Final Answer:

    Runtime error due to incompatible scopes -> Option B
  4. Quick Check:

    Session bean into singleton without proxy = runtime error [OK]
Hint: Injecting shorter scope bean into singleton causes runtime error [OK]
Common Mistakes:
  • Assuming injection always works regardless of scope
  • Ignoring need for proxyMode in such injections
  • Confusing compilation and runtime errors
5. You want a Spring Boot bean to create a new instance for each HTTP session but share the same instance within that session. Which @Scope annotation and configuration should you use?
hard
A. @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
B. @Scope("prototype")
C. @Scope("singleton")
D. @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)

Solution

  1. Step 1: Identify session scope for per-session bean

    The session scope creates one bean instance per HTTP session, which fits the requirement.
  2. Step 2: Use proxyMode for injection into singleton beans

    Adding proxyMode = ScopedProxyMode.TARGET_CLASS allows safe injection into other beans by creating a proxy that resolves the correct session instance.
  3. Final Answer:

    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) -> Option A
  4. Quick Check:

    Session scope + proxyMode = new instance per session shared inside session [OK]
Hint: Use session scope with proxyMode for per-session shared bean [OK]
Common Mistakes:
  • Using prototype scope which creates new instance every injection
  • Using singleton scope which shares one instance globally
  • Using request scope which creates new instance per HTTP request
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