Spring Questions and Answers – AspectJ Annotation

This set of Java Spring Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: 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.
advertisement
advertisement

4. Which advice is executed once a joint point finishes?
a) @Before
b) @After
c) @AfterReturning
d) @AfterThrowing
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: b
Explanation: An advice can access the current join point information by declaring an argument of type org.aspectj.lang.JoinPoint in the advice method signature.
advertisement

8. Which interface is implemented to specify precedence of aspects?
a) Ordered
b) ApplicationAspect
c) AspectPointcut
d) None of the mentioned
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: The precedence of aspects can be specified either by implementing the Ordered interface or @Order Annotation.
advertisement

10. Method which returns the highest priority of aspect’s join point?
a) getHighestPriority
b) getOrder
c) getHighOrder
d) getPriority
View Answer

Answer: b
Explanation: 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();
	    // ...
	  }
 
    }

a) Runtime Error
b) IllegalArgumentException
c) BeanCreation Exception
d) None of the mentioned
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation:- 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.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.