Java 8 multiple choice questions (MCQs)
Here are some multiple-choice questions focusing on key features of Java 8:
1. Lambda expressions and functional interfaces
Q1: Which of the following is the correct syntax for a lambda expression in Java 8?
a)
b)
c)
d) All of the above
a)
(params) -> expression
b)
params -> { statements; }
c)
(type1 param1, type2 param2) -> expression
d) All of the above
- Correct Answer: d) All of the above
Q2: A functional interface in Java 8 must have:
a) At least one abstract method
b) Exactly one abstract method
c) No abstract methods
d) Any number of abstract methods
a) At least one abstract method
b) Exactly one abstract method
c) No abstract methods
d) Any number of abstract methods
- Correct Answer: b) Exactly one abstract method
Q3: Which of the following is not a standard functional interface introduced in Java 8?
a)
b)
c)
d)
a)
Predicate<T>
b)
Consumer<T>
c)
Supplier<T>
d)
Iterator<T>
- Correct Answer: d)
Iterator<T>
2. Stream API
Q4: Which method is used to filter elements of a stream based on a given condition (predicate)?
a)
b)
c)
d)
a)
map()
b)
filter()
c)
forEach()
d)
reduce()
- Correct Answer: b)
filter()
Q5: Which Stream API method is used to transform each element of a stream by applying a function to it?
a)
b)
c)
d)
a)
filter()
b)
sorted()
c)
map()
d)
distinct()
- Correct Answer: c)
map()
Q6: Which of the following is a terminal operation in the Stream API?
a)
b)
c)
d)
a)
filter()
b)
map()
c)
collect()
d)
peek()
- Correct Answer: c)
collect()
3. Optional class
Q7: What is the purpose of the
a) To make objects optional at runtime
b) To represent a value that may or may not be present (i.e., non-null)
c) To store a list of optional values
d) To define optional parameters for methods
Optional
class in Java 8?a) To make objects optional at runtime
b) To represent a value that may or may not be present (i.e., non-null)
c) To store a list of optional values
d) To define optional parameters for methods
- Correct Answer: b) To represent a value that may or may not be present (i.e., non-null)
Q8: Which method of the
a)
b)
c)
d)
Optional
class is used to provide an alternative value if the original value is not present?a)
get()
b)
isPresent()
c)
orElse()
d)
map()
- Correct Answer: c)
orElse()
4. Default methods
Q9: Why were default methods introduced in Java 8 interfaces?
a) To replace abstract methods
b) To enable interfaces to contain state
c) To add new methods to interfaces without breaking backward compatibility
d) To make interfaces behave like abstract classes
a) To replace abstract methods
b) To enable interfaces to contain state
c) To add new methods to interfaces without breaking backward compatibility
d) To make interfaces behave like abstract classes
- Correct Answer: c) To add new methods to interfaces without breaking backward compatibility
Q10: Which keyword is used to define a default method in an interface?
a)
b)
c)
d)
a)
abstract
b)
static
c)
default
d)
final
- Correct Answer: c)
default
5. Date and Time API
Q11: Which package contains the classes for the new Date and Time API in Java 8?
a)
b)
c)
d)
a)
java.util
b)
java.time
c)
java.sql
d)
java.text
- Correct Answer: b)
java.time
Q12: Which class represents a date without a time component in the new Date and Time API?
a)
b)
c)
d)
a)
LocalTime
b)
LocalDateTime
c)
LocalDate
d)
ZonedDateTime
- Correct Answer: c)
LocalDate