- Home
- Programming
- OCP Java SE8
26.
Which of the following is required for all valid lambda expressions?
- A.()
- B.->
- C.{}
- D.Parameter data type(s)
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
The lambda expression s -> true is valid, making Options A, C, and D incorrect. Parentheses () are not required on the left-hand side if there is only one variable. Brackets {} are not required if the right-hand side is a single expression. Parameter data types are only required if the data type for at least one parameter is specified, otherwise none are required. The remaining choice, the arrow operator -> , is required for all lambda expressions, making Option B the correct answer. |
27.
What is the output of the following application?
package holiday;
enum DaysOff {
Thanksgiving, PresidentsDay, ValentinesDay
}
public class Vacation {
public static void main(String... unused) {
final DaysOff input = DaysOff.Thanksgiving;
switch(input) {
default:
case DaysOff.ValentinesDay:
System.out.print("1");
case DaysOff.PresidentsDay:
System.out.print("2");
}
}
}
package holiday;
enum DaysOff {
Thanksgiving, PresidentsDay, ValentinesDay
}
public class Vacation {
public static void main(String... unused) {
final DaysOff input = DaysOff.Thanksgiving;
switch(input) {
default:
case DaysOff.ValentinesDay:
System.out.print("1");
case DaysOff.PresidentsDay:
System.out.print("2");
}
}
}
- A.1
- B.2
- C.12
- D.None of the above
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The application contains a compilation error. The case statements incorrectly use the enum name as well as the value, such as DaysOff.ValentinesDay . Since the type of the enum is determined by the value of the variable in the switch statement, the enum name is not allowed and throws a compilation error when used. For this reason, Option D is correct. If the enum name DaysOff was removed, the application would output 12 , since the lack of any break statements causes multiple blocks to be reached, and Option C would have been the correct answer. |
28.
Fill in the blanks: A functional interface must contain or inherit ____________and
may optionally include____________ .
- A.at least one abstract method, the @Override annotation
- B.exactly one method, static methods
- C.exactly one abstract method, the @FunctionalInterface annotation
- D.at least one static method, at most one default method
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
A functional interface must include exactly one abstract method, either by inheritance or declared directly. It may also have any number, including zero, of default or static methods. For this reason, both parts of Option D are incorrect. The first part of Option A is incorrect because more than one abstract method disqualifies it as a functional interface. The first part of Option B is incorrect because the method must be abstract ; that is to say, any method will not suffice. Finally, Option C is the correct answer. The first part of the sentence defines what it means to be a functional interface. The second part refers to the optional @FunctionalInterface annotation. It is considered a good practice to add this annotation to any functional interfaces you define because the compiler will report a problem if you define an invalid interface that does not have exactly one abstract method. |
29.
Which of the following class types cannot be marked final or abstract ?
- A.Static nested class
- B.Local inner class
- C.Anonymous inner class
- D.Member inner class
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
While an anonymous inner class can extend another class or implement an interface, it cannot be declared final or abstract since it has no class definition. For this reason, Option C is correct. The other classes may be declared final or abstract since they have a class definition. |
30.
Which of the following is a valid lambda expression?
- A.r -> {return 1==2}
- B.(q) -> true
- C.(x,y) -> {int test; return test>0;}
- D.a,b -> true
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Option A is incorrect because the lambda expression is missing a semicolon ( ; ) at the end of the return statement. Option C is incorrect because the local variable test is used without being initialized. Option D is also incorrect. The parentheses are required on the left-hand side of the lambda expression when there is more than one value or a data type is specified. Option B is the correct answer and the only valid lambda expression. |