Programmer II Chapter 2: Annotations Flashcards
What types can annotation elements be declared with?
primitives String Class enum another annotation array of above types
Which annotations can be used in a shorthand form?
public @interface Sleep { int value(); String hours(); }
public @interface Wake { String hours(); }
Neither.
The first declaration contains two required elements, while the second annotation does not include an element named value().
Given an annotation below, which uses of it are legal?
public @interface Music {
String[] genres();
}
public class Giraffe { @Music(genres={"Rock and roll"}) String mostDisliked; @Music(genres="Classical") String favorite; }
Both.
The first annotation is considered the regular form, as it is clear the usage is for an array. The second annotation is the shorthand notation, where the array braces ({}) are dropped for convenience. Keep in mind that this is still providing a value for an array element; the compiler is just inserting the missing array braces for you
Which lines will compile?
public @interface BadAnnotation { String name() default new String(""); String address() default ""; String title() default null; }
only address()
name() does not compile because it is not a constant expression, while title() does not compile because it is null. Only address() compiles. Notice that while null is not permitted as a default value, the empty String “” is.
Will this compile?
public @interface Material {}
public @interface Fluffy { int cuteness(); public abstract int softness() default 11; protected Material material(); private String friendly(); final boolean isBunny(); }
material(), friendly(), isBunny() contain compilation errors
The elements cuteness() and softness() are both considered abstract and public, even though only one of them is marked as such. The elements material() and friendly() do not compile because the access modifier conflicts with the elements being implicitly public. The element isBunny() does not compile because, like abstract methods, it cannot be marked final.
What modifier is used to mark that an annotation element is required?
A. optional B. default C. required D. * E. None of the above
E.
In an annotation, an optional element is specified with the default modifier, followed by a constant value. Required elements are specified by not providing a default value. Therefore, the lack of the default term indicates the element is required. For this reason, option E is correct.
Which of the following lines of code do not compile? (Choose all that apply.)
1: import java.lang.annotation.Documented; 2: enum Color {GREY, BROWN} 3: @Documented public @interface Dirt { 4: boolean wet(); 5: String type() = "unknown"; 6: public Color color(); 7: private static final int slippery = 5; 8: }
A. Line 2 B. Line 3 C. Line 4 D. Line 5 E. Line 6 F. Line 7 G. All of the lines compile.
D, F.
Line 5 does not compile because = is used to assign a default value, rather than the default modifier. Line 7 does not compile because annotation and interface constants are implicitly public and cannot be marked private. The rest of the lines do not contain any compilation errors.
Which built-in annotations can be applied to an annotation declaration? (Choose all that apply.)
A. @Override B. @Deprecated C. @Document D. @Target E. @Repeatable F. @Functional
B, D, E.
The annotations @Target and @Repeatable are specifically designed to be applied to annotations, making options D and E correct. Option B is also correct, as @Deprecated can be applied to almost any declaration. Option A is incorrect because @Override can be applied only to methods. Options C and F are incorrect because they are not the names of built-in annotations.
Given an automobile sales system, which of the following information is best stored using an annotation?
A. The price of the vehicle
B. A list of people who purchased the vehicle
C. The sales tax of the vehicle
D. The number of passengers a vehicle is rated for
E. The quantity of models in stock
D.
Annotations should include metadata (data about data) that is relatively constant, as opposed to attribute data, which is part of the object and can change frequently. The price, sales, inventory, and people who purchased a vehicle could fluctuate often, so using an annotation would be a poor choice.On the other hand, the number of passengers a vehicle is rated for is extra information about the vehicle and unlikely to change once established. Therefore, it is appropriate metadata and best served using an annotation
Which of the following lines of code do not compile? (Choose all that apply.)
1: import java.lang.annotation.*; 2: class Food {} 3: @Inherited public @interface Unexpected { 4: public String rsvp() default null; 5: Food food(); 6: public String[] dessert(); 7: final int numberOfGuests = 5; 8: long startTime() default 0L; 9: }
A. Line 3 B. Line 4 C. Line 5 D. Line 6 E. Line 7 F. Line 8 G. All of the lines compile
B, C.
Line 4 does not compile because the default value of an element must be a non-null constant expression. Line 5 also does not compile because an element type must be one of the predefined immutable types: a primitive, String, Class, enum, another annotation, or an array of these types.The rest of the lines do not contain any compilation errors
Which annotations, when applied independently, allow the following program to compile? (Choose all that apply.)
import java.lang.annotation.*; @Documented @Deprecated public @interface Driver { int[] directions(); String name() default ""; } \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ class Taxi {}
A. @Driver B. @Driver(1) C. @Driver(3,4) D. @Driver({5,6}) E. @Driver(directions=7) F. @Driver(directions=8,9) G. @Driver(directions={0,1}) H. None of the above
E, G. The annotation declaration includes one required element, making option A incorrect. Options B, C, and D are incorrect because the Driver declaration does not contain an element named value(). If directions() were renamed in Driver to value(), then options B and D would be correct. The correct answers are options E and G. Option E uses the shorthand form in which the array braces ({}) can be dropped if there is only one element. Options C and F are not valid annotation uses, regardless of the annotation declaration. In this question, the @Documented and @Deprecated annotations have no impact on the solution.
Annotations can be applied to which of the following? (Choose all that apply.)
A. Class declarations B. Constructor parameters C. Local variable declarations D. Cast operations E. Lambda expression parameters F. Interface declarations G. None of the above
A, B, C, D, E, F.
Annotations can be applied to all of the declarations listed. If there is a type name used, an annotation can be applied.
Fill in the blanks with the correct answers that allow the entire program to compile. (Choose all that apply.)
@interface FerociousPack {
__________________; // m1
}
@Repeatable(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_) // m2 public @interface Ferocious {} @Ferocious @Ferocious class Lion {}
A. Ferocious value() on line m1. B. Ferocious[] value() on line m1. C. Object[] value() on line m1. D. @FerociousPack on line m2. E. FerociousPack on line m2. F. FerociousPack.class on line m2. G. None of the above. The code will not compile due to its use of the Lion class
B, F.
In this question, Ferocious is the repeatable annotation, while FerociousPack is the containing type annotation. The containing type annotation should contain a single value() element that is an array of the repeatable annotation type. For this reason, option B is correct. Option A would allow FerociousPack to compile, but not Ferocious. Option C is an invalid annotation element type.
The repeatable annotation needs to specify the class name of its containing type annotation, making option F correct. While it is expected for repeatable annotations to contain elements to differentiate its usage, it is not required. For this reason, the usage of @Ferocious is a valid marker annotation on the Lion class, making option G incorrect.
What properties must be true to use an annotation with an element value, but no element name? (Choose all that apply.)
A. The element must be named values().
B. The element must be required.
C. The annotation declaration must not contain any other elements.
D. The annotation must not contain any other values.
E. The element value must not be array.
F. None of the above
D.
To use an annotation with a value but not element name, the element must be declared with the name value(), not values(), making option A incorrect. The value() annotation may be required or optional, making option B incorrect. The annotation declaration may contain other elements, provided none is required, making option C incorrect. Option D is correct, as the annotation must not include any other values. Finally, option E is incorrect, as this is not a property of using a value() shorthand.
Which statement about the following code is correct?
import java.lang.annotation.*; @Target(ElementType.TYPE) public @interface Furry { public String[] value(); boolean cute() default true; } class Bunny { @Furry("Soft") public static int hop() { return 1; } }
A. The code compiles without any changes.
B. The code compiles only if the type of value() is changed to a String in the annotation declaration.
C. The code compiles only if cute() is removed from the annotation declaration.
D. The code compiles only if @Furry includes a value for cute().
E. The code compiles only if @Furry includes the element name for value.
F. The code compiles only if the value in @Furry is changed to an array.
G. None of the above
G.
Furry is an annotation that can be applied only to types. In Bunny, it is applied to a method; therefore, it does not compile. If the @Target value was changed to ElementType.METHOD (or @Target removed entirely), then the rest of the code would compile without issue. The use of the shorthand notation for specifying a value() of an array is correct.