0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.