Which is the Best Java Web Framework ?

Java - Interview Questions and Answers on Annotations

Q1.  Annotations were introduced with which version of Java ?

Ans. Java 5.

Q2.  Explain Annotations ?

Ans. 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 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.

Q3.  Give an Example of Annotations ?

Ans. Suppose that a software group traditionally starts the body of every class with comments providing important information:
public class Generation3List extends Generation2List {

   // Author: Samle Author
   // Date: 3/15/2012
   // Current revision: 1
   // Last modified: 6/12/2014
   // By: Sample Author
   // Reviewers: Tom Hilton

   // class code goes here

}

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

@interface ClassPreamble {
   String author();
   String date();
   int currentRevision() default 1;
   String lastModified() default "N/A";
   String lastModifiedBy() default "N/A";
   // Note use of array
   String[] reviewers();
}

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:

@ClassPreamble (
   author = "Samle Author",
   date = "3/15/2012",
   currentRevision = 1,
   lastModified = " 6/12/2014",
   lastModifiedBy = "Sample Author",
   // Note array notation
   reviewers = {"Tom", "Hilton"}
)

Q4.  What are few of the Annotations pre defined by Java?

Ans. @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 

@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 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.


Q5.  What are meta Annotations ?

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

Q6.  Name few meta-annotations ?

Ans. 

@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.

Q7.  Which annotations are used in Hibernate ?

Ans. @Entity
@Table
@Id 
@Column
@Temporal
@Basic
@Enumerated
@Access
@Embeddable
@Lob
@AttributeOverride
@Embedded
@GeneratedValue
@ElementCollection
@JoinTable
@JoinColumn
@CollectionId
@GenericGenerator
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
@NotFound

Q8.  What entries we make in the hibernate config file if we are not using hbm files but Annotations ?

Ans. We configure Entity classes having annotated mappings.

Q9.  What is the difference between these 2 annotations ?

@Entity

@Entity ( name="EMPLOYEES" )

Ans. The first annotation will try to map the Class with the Table as of 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". 

Q10.  "What is the difference between these 2 annotations ?

@Entity ( name ="EMPLOYEES")

@Entity 
@Table ( name=""EMPLOYEES"" )

@Entity ( name="EMP")
@Table ( name="EMPLPYEES" )

Ans. First Annotation will set the Entity name as EMPLOYEES and hence will try to map with the same Table name. 

The second annotation will make the Entity mapped to table EMPLOYEES irrespective of the Entity Name ( which is class name in this case ).

Third Annotations will set the different names for Enitity and Table and will explicitly map them.

Q11.  What are the different ID generating strategies using @GeneratedValue annotation ?

Ans. Auto , Identity , Sequence and Table.

Q12.  What are the annotations used in Junit  with Junit4 ?

Ans. @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. 

@Ignores

The Ignore annotation can be used when you want temporarily disable the execution of a specific test.

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

Ans. Using annotation Test with the argument as expected exception.

@Test (expected = Exception.class)

Q14.  How should we ignore or avoid executing set of tests ?

Ans. 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.

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

Ans. We can either increase their visibility and mark them with annotation @VisibleForTesting or can use reflection to individually test those methods.

Q16.  What is the @FunctionalInterface annotation ?

Ans. 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.

Q17.  Which of the following annotation is used to avoid executing Junits ?

 a. @explicit
 b. @ignore
 c. @avoid
 d. @NoTest

Ans. @ignore

Q18. Which is the Parent Class of Annotation class?

Ans.Object

Q19. What is the package name for Annotation class?

Ans.java.text




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.