Java Spring Multiple Choice Questions & Answers (MCQs) on “AspectJ Annotation”.
1. Which tag informs the spring container about the use of AspectJ annotation?
a) aop:aspectj-autowire
b) aop:aspectj-name
c) aop:aspectj-autoproxy
d) none of the mentioned
Answer: c
Clarification: To enable AspectJ annotation support in the Spring IoC container, you only have to define an empty XML element aop:aspectj-autoproxy in your bean configuration file.
2. Which of the following is advice supported by Aspect Annotation?
a) @Before
b) @After
c) @AfterReturning
d) All of the mentioned
Answer: d
Clarification: AspectJ supports five types of advice annotations: @Before, @After, @AfterReturning, @AfterThrowing, and @Around.
3. An advice is an action which comes into play at pointcuts.
a) True
b) False
Answer: b
Clarification: A pointcut is an expression to match a set of join points, while an advice is the action to take at a particular join point.
4. Which advice is executed once a joint point finishes?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer: b
Clarification: An after advice is executed after a join point finishes, whenever it returns a result or throws an exception abnormally.
5. Which advice is executed only when joint point returns or throws an exception?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
Answer: c
Clarification: If you would like to perform logging only when a join point returns, you should replace the after advice with an after returning advice.
6. Which advice combines all advices into one?
a) @Before
b) @After
c) @AfterThrowing
d) None of the mentioned
Answer: d
Clarification: It gains full control of a join point, so you can combine all the actions of the preceding advices into one single advice. You can even control when, and whether, to proceed with the original join point execution.
7. An advice can access the current join point information by declaring an argument of type org.aspectj.lang.AdvicePoint in the advice method signature.
a) True
b) False
Answer: b
Clarification: An advice can access the current join point information by declaring an argument of type org.aspectj.lang.JoinPoint in the advice method signature.
8. Which interface is implemented to specify precedence of aspects?
a) Ordered
b) ApplicationAspect
c) AspectPointcut
d) None of the mentioned
Answer: a
Clarification: The precedence of aspects can be specified either by implementing the Ordered interface.
9. Alternative annotative way to specify precedence of aspects?
a) @Order
b) @Aspect
c) @PointCut
d) None of the mentioned
Answer: a
Clarification: The precedence of aspects can be specified either by implementing the Ordered interface or @Order Annotation.
10. Method which returns the highest priority of aspect’s join point?
a) getHighestPriority
b) getOrder
c) getHighOrder
d) getPriority
Answer: b
Clarification: The lower value returned by the getOrder() method represents higher priority.
11. what will be the output of the code snippet
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class AfterThrowingExample { @AfterThrowing( pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()", throwing="ex") public void doRecoveryActions(DataAccessException e) { throw new IllegalArgumentException(); // ...[/expand] } }
a) Runtime Error
b) IllegalArgumentException
c) BeanCreation Exception
d) None of the mentioned
Answer: c
Clarification: The throwing keyword in pointcut annotation doesn’t matches with the method’s parameter exception.
12. Which instantiation model is supported by AspectJ?
a) perthis
b) pertarget
c) none of the mentioned
d) all of the mentioned
Answer: d
Clarification: Spring supports AspectJ perthis and pertarget instantiation models.
13. Which instantiation model is supported by AspectJ?
a) perthis
b) pertarget
c) none of the mentioned
d) all of the mentioned
Answer: c
Clarification: percflow, percflowbelow, and pertypewithin are not currently supported.
14. Which tag in XML is used to declare @Before advice’s method?
a) aop:before
b) aop:after
c) aop:afterthrow
d) None of the mentioned
Answer: a
Clarification: Before advice runs before a matched method execution. It is declared inside an aop:aspect using the aop:before element.
15. Which tag in XML is used to declare @Before advice’s method?
a) aop:before
b) aop:after-returning
c) aop:afterthrow
d) None of the mentioned
Answer: b
Clarification:- After returning advice runs when a matched method execution completes normally. It is declared inside an aop:aspect in the same way as before advice.