0
0
Spring Bootframework~20 mins

@After and @AfterReturning in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring AOP After Advice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Behavior of @After advice in Spring AOP
Consider a Spring AOP aspect with an @After advice on a method that throws an exception. What will happen when the advised method throws an exception?
Spring Boot
public class MyAspect {
    @After("execution(* com.example.service.*.*(..))")
    public void afterAdvice() {
        System.out.println("After advice executed");
    }
}

// The target method throws a RuntimeException.
AThe @After advice will not execute if the method throws an exception.
BThe @After advice will execute even if the method throws an exception.
CThe @After advice will execute only if the method returns normally without exception.
DThe @After advice will execute before the method is called.
Attempts:
2 left
💡 Hint
Think about whether @After runs regardless of method outcome.
component_behavior
intermediate
2:00remaining
Return value availability in @AfterReturning advice
In Spring AOP, which of the following is true about the return value in an @AfterReturning advice?
Spring Boot
public class MyAspect {
    @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
    public void afterReturningAdvice(Object result) {
        System.out.println("Returned: " + result);
    }
}
AThe return value is always null in @AfterReturning advice.
BThe return value is available even if the method throws an exception.
CThe return value is available only if the method completes normally without exception.
DThe return value is available before the method executes.
Attempts:
2 left
💡 Hint
Consider when a method actually returns a value.
📝 Syntax
advanced
2:00remaining
Correct syntax for @AfterReturning with returning attribute
Which of the following @AfterReturning advice declarations is syntactically correct in Spring AOP?
A
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = retVal)
public void adviceMethod(Object retVal) {}
B
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", return = "retVal")
public void adviceMethod(Object retVal) {}
C
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "retVal")
public void adviceMethod() {}
D
@AfterReturning(pointcut = "execution(* com.example.*.*(..))", returning = "retVal")
public void adviceMethod(Object retVal) {}
Attempts:
2 left
💡 Hint
Check the attribute name and parameter matching.
🔧 Debug
advanced
2:00remaining
Why does @AfterReturning advice not execute?
Given this aspect code, why does the @AfterReturning advice never print its message? public class MyAspect { @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result") public void afterReturningAdvice(String result) { System.out.println("Returned: " + result); } } The target method returns an Integer.
AThe advice method parameter type String does not match the actual return type Integer.
BThe pointcut expression is incorrect and does not match the method.
CThe @AfterReturning advice only works for void methods.
DThe advice method must have no parameters.
Attempts:
2 left
💡 Hint
Check the parameter type of the advice method compared to the return type.
lifecycle
expert
3:00remaining
Order of execution: @After, @AfterReturning, and exception thrown
Consider a Spring AOP aspect with both @After and @AfterReturning advices on the same method. The method throws an exception. Which advice(s) will execute?
Spring Boot
public class MyAspect {
    @After("execution(* com.example.service.*.*(..))")
    public void afterAdvice() {
        System.out.println("After advice executed");
    }

    @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
    public void afterReturningAdvice(Object result) {
        System.out.println("AfterReturning advice executed with: " + result);
    }
}
AOnly the @After advice executes; @AfterReturning does not execute because of the exception.
BNeither advice executes because the method threw an exception.
COnly the @AfterReturning advice executes; @After does not execute.
DBoth @After and @AfterReturning advices execute regardless of exception.
Attempts:
2 left
💡 Hint
Recall when each advice runs relative to exceptions.