Annotation Interview Questions

What is Annotation?

Annotations are nothing but meta-data added to the Java Elements. Annotations define a java type which is similar to classes, Enums, Interfaces and they can be applied to several Java Elements.

Explain Annotations.

Annotations is a form of metadata which provides data about a program but itself is not part of the program. Annotations have no direct effect on the operation of the code they annotate. Annotations have a number of uses, of which some of them are as follows :

  • Information for the compiler — Annotations can be used by the compiler either 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 on.

  • Runtime processing — Some annotations are available to be examined at runtime.

Learn more in our Video Tutorial Section

What are few of the Annotations that are predefined 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 a field with the 'Deprecated annotation'.

  • '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 which would be generated otherwise.

  • 'SafeVarargs annotation', when applied to a method or a 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'.

Annotations were introduced with which version of Java ?

Annotations were introduced with 5th version of Java.

Give an Example of Annotation.

Let's say for instance a software group traditionally starts the body of every class with comments providing important information:

To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is :

  • The annotation type definition looks similar to an interface definition where the keyword interface is preceded by the at sign (@) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.

  • The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.

  • After the annotation type is defined, you can use annotations of that type, with the values filled in, like this :

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 (though this is not true by default). When the user query's the annotation type and if the class has no annotation for this type, the class's superclass is queried for the annotation type. This annotation applies only to class declarations.

  • 'Repeatable annotation' that was introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same 'declaration' or 'type' in use. For more information, refer 'Repeating Annotations'.

What is meta annotations ?

Annotations that apply to other annotations are called "meta-annotations".

Which annotations are used in Hibernate ?
  • Entity

  • Table

  • Id

  • Column

  • Temporal

  • Basic

  • Enumerated

  • Access

  • Embeddable

  • Lob

  • AttributeOverride

  • Embedded

  • GeneratedValue

  • ElementCollection

  • JoinTable

  • JoinColumn

  • CollectionId

  • GenericGenerator

  • OneToOne

  • OneToMany

  • ManyToOne

  • ManyToMany

  • NotFound

What is the difference between the below given two annotations ?

  1. Entity

  2. Entity (name="EMPLOYEES")

The first annotation will try to map the Class with the Table having the same name as Class whereas the second annotation will specify the Entity name as "EMPLOYEES" and hence will try to map with Table Name "EMPLOYEES".

Which are the annotations used in Junit with Junit4 ?
  • Test Annotation
    The 'Test annotation' indicates that the public void method to which it is attached can be run as a test case.

  • Before Annotation
    The 'Before annotation' indicates that this method must be executed before each test in the class, so as to execute some pre-conditions necessary for the test.

  • BeforeClass Annotation
    The 'BeforeClass annotation' indicates that the static method to which it is attached must be executed once and before all tests in the class.

  • After Annotation
    The 'After annotation' indicates that this method gets executed after execution of each test.

  • AfterClass Annotation
    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 Annotation
    The 'Ignore annotation' can be used when you want temporarily disable the execution of a specific test.

What are the different ID generating strategies that are used with the @GeneratedValue annotation ?

Auto , Identity , Sequence and Table are some of the strategies that are used with the @GeneratedValue annotation.

How to create a Junit to make sure that the tested method throws an exception ?

Using an annotation Test with the argument gives us an expected exception.

    Test (expected = Exception.class)
How should we ignore or avoid executing set of tests ?

We can remove @Test from the respective test in order 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.

How can we test methods individually which are not visible or declared private ?

We can either increase the test method's visibility and mark them with annotation @VisibleForTesting or we can use reflection to individually test those methods.

What is the @FunctionalInterface annotation ?
  • This is an informative annotation which specify that the interface is a functional interface. A 'Functional 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.

What is the package name for Annotation class?

java.text is the package name for an annotation class.

Which is the Parent Class of Annotation class?

'Object' is said to be the parent class of annotation class.

How annotations are retrieved or read?

Annotations that are once defined and used in some class, can be read using 'reflection package' methods like getAnnotations(). We have to first obtain the reference to the class which contains or uses the Annotations and then we can write code as shown below:

Learn more about Java