0% found this document useful (0 votes)
17 views

Java8 MCQ

Uploaded by

gauri.rai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java8 MCQ

Uploaded by

gauri.rai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1. Which Java 8 feature allows us to filter a collection based on a predicate?

(a) Lambda Expression

(b) Default Method

(c) Optional class

(d) Stream API


Q2. Which Java 8 feature allows us to pass behavior as a parameter to a method?

(a) Lambda expressions

(b) Optional class

(c) Stream API

(d) Functional Interface


Q3. What is the output of the following program?

List<String> names = Arrays.asList("ABC", "CAB", "BCA");


String result = names.stream().reduce("", (a, b) -> a + b.charAt(1));
System.out.println(result);
(a) "ABC"

(b) "ACB"

(c) "BAC"

(d) "CBA"
Q4. Which is the new method introduced in the String class in Java 8?

(a) offsetByCodePoints()

(b) matches()

(c) intern()

(d) join()
Q5. What will be the output of the following code snippet?

Optional num= Stream.of(9, 5, 8, 7, 4, 9, 2, 11, 10, 3)


.filter(n -> n > 10)
.filter(n -> n % 5 == 0)
.findFirst();
System.out.println(num);
(a) Optional[8]

(b) Optional.empty

(c) Optional[11]

(d) 11
Q6. Which one is the correct syntax for declaring a lambda expression in Java 8?

(a) (param1, param2) => expression

(b) (param1, param2) :: { expression }


(c) (param1, param2) -> expression

(d) (param1, param2) : expression


Q7. Which of the following interface is not a functional interface?

(a) An interface with a single abstract method

(b) An interface with an abstract method and a default method

(c) An interface with an abstract method and an equals() method

(d) An interface with an abstract method and a run() method


Q8. What is the new Date and Time API in Java 8?

(a) A replacement for the java.sql.Date and java.sql.Calendar classes

(b) A new API for working with Optional

(c) A replacement for the java.util.Date and java.util.Calendar classes

(d) A new API for working with Java Stream API


Q9. Which one among the following is the main feature introduced in Java 8?

(a) Annotations

(b) Generics

(c) Lambda expressions

(d) Enumerations
Q10. Which feature out of following is introduced in Java 8?

(a) Strings in switch statement

(b) Improved type inference: the diamond operator <>

(c) StringJoiner Class

(d) Private methods in Interfaces


Q11. What is the output of the following program?

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


int result= numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println(result);
(a) 10

(b) 9

(c) 6

(d) 15
Q12. Mary is a Java developer. She is working in an assignment. She wants to use a
predefined functional interface introduced in Java 8 that doesn’t take any input
and it always returns some object. Which one among the following option should she
use to complete her assignment?
(a) Consumer

(b) Pedicate

(c) Supplier

(d) BiConsumer
Q13. What is the purpose of default method in Java 8?

(a) A method that is automatically called when an object is created

(b) A method that is automatically called when an object is destroyed

(c) A method that is defined in an interface with a default implementation

(d) A method that is called by the garbage collector to free up memory


Q14. What is the method reference feature in Java 8?

(a) A way to create a new object using a constructor reference

(b) A way to call a static method using a method reference

(c) A way to call an instance method using a method reference

(d) All of the above


Q15. What is the Optional class in Java 8?

(a) A class that represents a value which can either be present or absent

(b) A class that represents an immutable collection

(c) A class that represents optional values to stream of elements

(d) A class as an alternative to functional interface


Q16. What is the new feature introduced in Java 8 in the context of concurrent
programming?

(a) ExecutorService

(b) CompletableFuture

(c) ThreadLocal

(d) ReentrantLock
Q17. What is the purpose of the forEach() method in Java 8 Streams API?

(a) To apply an operation to each element of a stream

(b) To filter the elements of a stream

(c) To print the elements of a stream

(d) To map the elements of a stream to a different type


Q18. Robert is working in a Java project. In one of his assignment, he is using
Stream API of Java 8 where he wants to transform each element to multiple values
and also compress the resulting stream. Which of the following method of Stream API
will be the best option for him?

(a) map()
(b) filter()

(c) flatMap()

(d) reduce()
Q19. What is the Stream API in Java 8?

(a) A new file I/O stream library

(b) A new concurrency library

(c) A new API for working with collections

(d) A new API for working with databases


Q20. In the context of Java 8 Stream API, assume that you have a requirement to
convert a stream into an array or a collection. Which of the following method of
the Stream interface will you apply on the stream to achieve that?

(a) toCollection()

(b) toArray()

(c) toList()

(d) collect()
Q21. What is the output of the following code?

Stream<String> stream = Stream.of("hello", "world", "java");


stream.filter(s -> s.contains("o"))
.map(String::toUpperCase)
.forEach(System.out::print);
(a) HELLO WORLD JAVA

(b) hello world java

(c) HELLOWORLD

(d) HELLO WORLD


Q22. When should we use the reduce() method in Java 8 streams?

(a) When we wish to apply a transformation to each element of a stream

(b) When we wish to convert a nested stream into a single stream

(c) When we wish to produce one single result from a sequence of elements

(d) When we wish to filter the elements of a stream


Q23. What is the output of the following code?

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);


stream.reduce((a, b) -> a + b)
.ifPresent(System.out::println);
(a) 15

(b) 10

(c) 6
(d) 5
Q24. Which is the mandatory condition to define a functional interface in Java 8?

(a) The interface should have @functionalInterface annotation on top of it

(b) The interface should have only one method

(c) The interface should have only one abstract method

(d) The interface should at least have one method


Q25. What is the purpose of the parallelStream() method in Java 8?

(a) To filter the elements of a stream

(b) To apply an operation to each element of a stream

(c) To create a parallel stream from a sequential stream

(d) To create a sequential stream from a parallel stream


Q26. What is the output of the following code?

List<Integer> list = Arrays.asList(1, 15, 3, 10, 27);


int sum = list.stream()
.filter(i -> i % 3 == 0)
.mapToInt(Integer::intValue)
.sum();
System.out.println(sum);
(a) 18

(b) 45

(c) 30

(d) 56
Q27. What is the purpose of the Optional class in Java 8?

(a) To provide optional values to a stream

(b) To store a reference to a value that may or may not be null

(c) To apply a transformation to each element of a stream

(d) To filter the elements of a stream


Q28. What is the output of the following code?

List<String> list = Arrays.asList("banana", "apple", "cherry");


list.stream()
.sorted((s1, s2) -> s1.compareTo(s2))
.forEach(System.out::println);
(a) apple, banana, cherry

(b) cherry, banana, apple

(c) banana, apple, cherry

(d) apple, cherry, banana


Q29. What is the purpose of the peek() method in Java 8 streams?

(a) To apply an operation to each element of a stream


(b) To filter the elements of a stream

(c) To convert a stream into an array or a collection

(d) To apply a transformation to each element of a stream


Q30. What is the output of the following code?

IntStream.range(1, 6)
.mapToObj(i -> i + " ")
.forEach(System.out::print);
(a) 1 2 3 4 5 6

(b) 2 3 4 5

(c) 1 2 3 4 5

(d) 1 2 3 4
Q31. Which of the following is a valid lambda expression in Java 8?

(a) () -> { System.out.println("Hello"); }

(b) (int x) -> { x++; }

(c) (String s, int x) -> { System.out.println(s + x); }

(d) All of the above


Q32. Which Java 8 feature allows us to iterate through a collection using lambda
expressions?

(a) Stream API

(b) Method Reference

(c) Optional

(d) Annotation Processing


Q33. Which of the following is not a valid target type for a lambda expression in
Java 8?

(a) An interface with a single abstract method

(b) A functional interface

(c) A class with a single abstract method

(d) All of the above are valid target types


Q34. What is the purpose of the Supplier interface in Java 8?

(a) To represent a method that takes no arguments and returns no value

(b) To represent a method that takes one argument and returns a value

(c) To represent a method that takes no arguments and returns a value

(d) To represent a method that takes one argument and returns no value
Q35. What is the output of the following code snippet?

List<String> words = Arrays.asList("Peris", "London", "New York", "Sydney",


"Washington");
String result = words.stream()
.filter(w -> w.length() > 10)
.findFirst()
.orElse("none");
System.out.println(result);
(a) none

(b) Washington

(c) New York

(d) An error is thrown

You might also like