Annotations Interview Questions:-
1. Explain Annotations ?
Annotations a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.Annotations are created by using @ sign. Annotations have a number of uses, among them:
. Information for the compiler - Annotations can be used by the compiler to detect errors or suppress warnings.
. Compile-time and deployment-time processing - Software tools can process annotation information to generate code, XML files, and so forth.
. Runtime processing - Some annotations are available to be examined at runtime.
2. What are few of the Annotations pre defined by Java?
@Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the
@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.
@SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate.
@SafeVarargs annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its varargsparameter. When this annotation type is used, unchecked warnings relating to varargs usage are suppressed.
@FunctionalInterface annotation, introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.
3. What are meta Annotations ?
Annotations that apply to other annotations are called meta-annotations.
4. Name few meta-annotations ?
@Retention annotation specifies how the marked annotation is stored:
@Documented annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
@Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to.
@Inherited annotation indicates that the annotation type can be inherited from the super class. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, the class' superclass is queried for the annotation type. This annotation applies only to class declarations.
@Repeatable annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use. For more information, see Repeating Annotations.
5. Which annotations are used in Hibernate ?
6. What are the annotations used in Junit with Junit4 ?
@Test The Test annotation indicates that the public void method to which it is attached can be run as a test case.
@Before The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.
@BeforeClass The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class.
@After The After annotation indicates that this method gets executed after execution of each test.
@AfterClass The AfterClass annotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.
@Ignore The Ignore annotation can be used when you want temporarily disable the execution of a specific test.
7. What is the @FunctionalInterface annotation ?
This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.
8. How should we ignore or avoid executing set of tests ?
We can remove @Test from the respective test so as to avoid its execution. Alternatively we can put @Ignore annotation on the Junit file if we want to ignore all tests in a particular file.
9. What is marker annotations?
Marker annotation does not contains any member declarations. By looking at presence of the annotation, we can do some specific actions.
By using isAnnotationPresent() method, we can check whether the annotation is present or not.