Q001
NO.149 Which three annotation uses are valid? (Choose three.) A. Function<String, String> func = (@NonNull x) -> x.toUpperCase(); B. var v = "Hello" + (@Interned) "World" C. Function<String, String> func = (var @NonNull x) -> x.toUpperCase(); D. Function<String, String> func = (@NonNull var x) -> x.toUpperCase(); E. var myString = (@NonNull String) str; F. var obj = new @Interned MyObject(); Answer: DEF
A 的 x 沒有型別,是錯誤的。標註需寫在 var 或型別之前。
ps : @NonNull 是第三方框架,比如 javax,所以無法在這課程中測試。
Q002
NO.221 Given:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface AuthorInfo {
String author() default "";
String date();
String[] comments() default {};
}
Which two are correct? (Choose two.)
A.
@AuthorInfo(date = "1-1-2020", comments = {null})
class Hello {
public void func() {}
}
B.
class Hello {
@AuthorInfo(date = "1-1-2020", comments = "Hello")
public void func() {}
}
C.
class Hello {
@AuthorInfo
public void func() {}
}
D.
@AuthorInfo(date = "1-1-2020")
class Hello {
public void func() {}
}
E.
class Hello {
@AuthorInfo(date = "1-1-2020", author = "Gandhi", comments = "Hello")
public void func() { }
}
A. Option A
B. Option B
C. Option C
D. Option D
E. Option E
Answer: BE
@Target(ElementType.METHOD) 標示著註解只能用在方法上。
而 date 沒有 default,所以一定要寫。
