Java 8+ Features Practice Exam Quiz

Get solved practice exam answers for your midterm and final examinations

Java 8+ Features Practice Exam Quiz

 

Which of the following features was introduced in Java 8?

A) Lambda expressions
B) Modules
C) Records
D) Var handles

Answer: A) Lambda expressions

Which functional interface is used for lambda expressions that take no arguments and return a result?

A) Runnable
B) Supplier
C) Consumer
D) Function

Answer: B) Supplier

In Java 8, which method of the Stream interface is used to get the first element of the stream?

A) find()
B) findFirst()
C) findAny()
D) first()

Answer: B) findFirst()

What does the Optional class represent in Java 8?

A) A value that may or may not be present
B) A container for non-nullable values
C) A stream of elements
D) A custom exception type

Answer: A) A value that may or may not be present

Which of the following methods can be used to create a stream from a collection in Java 8?

A) stream()
B) toStream()
C) collect()
D) getStream()

Answer: A) stream()

In Java 8, which method is used to iterate over the elements of a map in the forEach loop?

A) forEach()
B) iterate()
C) process()
D) loop()

Answer: A) forEach()

What is the default behavior of lambda expressions in terms of access to variables?

A) They can only access final or effectively final local variables
B) They can access any variable from the enclosing scope
C) They can access variables of any class in the program
D) They can access only instance variables of the class

Answer: A) They can only access final or effectively final local variables

What type of method reference is used to refer to a static method in Java 8?

A) Class::method
B) instance::method
C) Class::new
D) Class::methodReference

Answer: A) Class::method

Which of the following is a new feature introduced in Java 8 for concurrent programming?

A) ReentrantLock
B) CompletableFuture
C) ExecutorService
D) ThreadPoolExecutor

Answer: B) CompletableFuture

Which interface is used in Java 8 to support functional programming?

A) Runnable
B) Comparator
C) FunctionalInterface
D) Serializable

Answer: C) FunctionalInterface

Which method is used to combine two streams into one stream in Java 8?

A) combine()
B) flatMap()
C) merge()
D) collect()

Answer: B) flatMap()

In Java 8, which of the following is a valid way to filter a stream?

A) stream.filter()
B) stream.select()
C) stream.ignore()
D) stream.remove()

Answer: A) stream.filter()

Which of the following methods is used to compare two strings lexicographically in Java 8?

A) compare()
B) compareTo()
C) equals()
D) lexicographicalCompare()

Answer: B) compareTo()

What type of stream is created by the Stream.of() method in Java 8?

A) Infinite stream
B) Parallel stream
C) Sequential stream
D) Range stream

Answer: C) Sequential stream

Which Java 8 feature allows us to execute code after a certain time has passed?

A) ScheduledExecutorService
B) TimerTask
C) CompletableFuture
D) Duration

Answer: A) ScheduledExecutorService

What is the main benefit of using default methods in Java 8 interfaces?

A) They allow methods to be overridden
B) They can have code in the interface
C) They allow multiple inheritance
D) They enable interface instantiation

Answer: B) They can have code in the interface

Which of the following functional interfaces represents an operation on a single operand that produces a result of the same type?

A) Consumer
B) Function
C) Predicate
D) Supplier

Answer: B) Function

How can you create a stream from an array in Java 8?

A) Stream.array()
B) Arrays.stream()
C) Stream.of()
D) Arrays.asList()

Answer: B) Arrays.stream()

Which of the following is true about the Stream API in Java 8?

A) Streams can be reused multiple times
B) Streams are data structures
C) Streams are read-only
D) Streams can be either sequential or parallel

Answer: D) Streams can be either sequential or parallel

What is the new method introduced in Map interface in Java 8?

A) putIfAbsent()
B) removeIf()
C) add()
D) merge()

Answer: A) putIfAbsent()

Which of the following methods is used to sort a stream in Java 8?

A) sort()
B) order()
C) filter()
D) distinct()

Answer: A) sort()

What is the purpose of the @FunctionalInterface annotation in Java 8?

A) To enforce that the interface has only one abstract method
B) To indicate that the interface is designed to be used by lambda expressions
C) To define an interface that can be used for functional programming
D) All of the above

Answer: D) All of the above

In Java 8, which feature allows processing elements in parallel without writing any additional code?

A) Fork/Join framework
B) Parallel streams
C) ExecutorService
D) Asynchronous tasks

Answer: B) Parallel streams

Which of the following methods in Optional class returns a value if present, otherwise throws an exception?

A) orElseThrow()
B) get()
C) orElse()
D) orElseEmpty()

Answer: A) orElseThrow()

What is the purpose of the forEach() method in Java 8?

A) To iterate over a stream
B) To filter elements from a stream
C) To sort elements in a stream
D) To collect elements from a stream

Answer: A) To iterate over a stream

What is the use of the Collectors.toList() method in Java 8?

A) To collect elements into a list
B) To filter elements in a stream
C) To sum the elements of a stream
D) To remove duplicates from a stream

Answer: A) To collect elements into a list

Which of the following is true about the Stream API’s intermediate operations in Java 8?

A) They modify the original data source
B) They are lazy
C) They are always executed eagerly
D) They must be terminal operations

Answer: B) They are lazy

What is the default behavior of a method reference in Java 8?

A) It is equivalent to writing the lambda expression
B) It is used for method overriding
C) It is used to refer to the super method
D) It requires parameters to be passed explicitly

Answer: A) It is equivalent to writing the lambda expression

Which of the following is the method of the Stream class used for finding the maximum value based on a comparator in Java 8?

A) max()
B) findMax()
C) reduce()
D) collect()

Answer: A) max()

How do you convert a Stream back to a collection in Java 8?

A) to()
B) collect()
C) convert()
D) collectTo()

Answer: B) collect()

 

Which of the following is a new type of method introduced in Java 8 interfaces?

A) Static methods
B) Private methods
C) Abstract methods
D) Synchronized methods

Answer: A) Static methods

Which of the following is NOT a valid way to create a stream in Java 8?

A) Stream.of()
B) Arrays.stream()
C) Collection.stream()
D) stream()

Answer: D) stream()

What does the Optional class in Java 8 help to prevent?

A) NullPointerException
B) ArrayIndexOutOfBoundsException
C) ClassCastException
D) ConcurrentModificationException

Answer: A) NullPointerException

What does the filter() method of the Stream interface do in Java 8?

A) Removes duplicate elements
B) Sorts the elements of the stream
C) Filters elements based on a condition
D) Converts all elements to uppercase

Answer: C) Filters elements based on a condition

Which of the following classes provides default methods in Java 8?

A) String
B) ArrayList
C) Collections
D) Collection

Answer: D) Collection

In Java 8, which method of the Stream interface is used to map each element to another form?

A) map()
B) transform()
C) convert()
D) reduce()

Answer: A) map()

Which of the following features does the CompletableFuture class support in Java 8?

A) Synchronous execution only
B) Asynchronous computation with a callback
C) Parallel computation only
D) Blocking computation

Answer: B) Asynchronous computation with a callback

What is the purpose of the forEach() method in a Stream?

A) To execute a given action for each element in the stream
B) To filter the stream based on a condition
C) To map each element to a different value
D) To collect elements into a collection

Answer: A) To execute a given action for each element in the stream

Which of the following is the correct syntax for creating a Stream from a list in Java 8?

A) list.stream()
B) Stream.create(list)
C) Stream.of(list)
D) list.toStream()

Answer: A) list.stream()

Which of the following methods does not belong to the Stream interface?

A) distinct()
B) sorted()
C) reduce()
D) add()

Answer: D) add()

What type of method is used for default implementation in an interface in Java 8?

A) static methods
B) abstract methods
C) default methods
D) final methods

Answer: C) default methods

Which of the following methods is used to return the first element of a stream in Java 8?

A) first()
B) findFirst()
C) top()
D) head()

Answer: B) findFirst()

Which of the following is true about the Stream API in Java 8?

A) Stream operations are always executed eagerly
B) Stream operations are always executed lazily
C) Stream operations are executed only once
D) Stream operations are executed in parallel by default

Answer: B) Stream operations are always executed lazily

What is the default value returned by Optional.orElse() if the value is not present?

A) null
B) false
C) 0
D) A default value specified by the user

Answer: D) A default value specified by the user

Which of the following is used to iterate over a Map in Java 8?

A) forEach()
B) iterate()
C) map()
D) each()

Answer: A) forEach()

Which of the following classes can be used to perform parallel processing in Java 8?

A) ParallelStream
B) ForkJoinPool
C) ParallelExecutor
D) ParallelTask

Answer: B) ForkJoinPool

Which of the following methods is used to combine two streams into one in Java 8?

A) concat()
B) merge()
C) flatten()
D) combine()

Answer: A) concat()

In Java 8, which method is used to generate a stream of values in a range?

A) Stream.range()
B) Stream.generate()
C) IntStream.range()
D) Stream.ofRange()

Answer: C) IntStream.range()

Which of the following is true about the Predicate interface in Java 8?

A) It represents a function that takes two arguments
B) It represents a function that returns a boolean value
C) It represents a function that takes no arguments
D) It represents a function that produces a result of the same type

Answer: B) It represents a function that returns a boolean value

Which Java 8 feature allows operations on data without mutating the original data?

A) Streams
B) Optional
C) Immutable lists
D) Lambda expressions

Answer: A) Streams

What does the mapToInt() method in Java 8 return?

A) A stream of integers
B) An integer result after applying a function
C) A stream of elements converted to integers
D) An array of integers

Answer: C) A stream of elements converted to integers

In Java 8, which of the following methods is used to collect stream results into a List?

A) collect()
B) toList()
C) collectList()
D) list()

Answer: A) collect()

Which of the following method signatures is valid for a lambda expression in Java 8?

A) (int x, int y) -> { return x + y; }
B) int x, int y -> { return x + y; }
C) int x -> { return x * x; }
D) int -> x * x

Answer: A) (int x, int y) -> { return x + y; }

Which of the following is the correct way to use Optional in Java 8?

A) Optional<String> value = new Optional<>(“Hello”);
B) Optional<String> value = Optional.ofNullable(“Hello”);
C) Optional<String> value = Optional.empty(“Hello”);
D) Optional<String> value = Optional.of(“Hello”);

Answer: B) Optional<String> value = Optional.ofNullable(“Hello”);

What is the purpose of the Collectors.joining() method in Java 8?

A) To join elements of a stream into a single string
B) To create a string that represents the stream
C) To combine two collections into one
D) To add an element to the end of the stream

Answer: A) To join elements of a stream into a single string

Which of the following methods is used to apply a function to each element in a stream in Java 8?

A) apply()
B) transform()
C) map()
D) modify()

Answer: C) map()

 

Which method of the Stream class in Java 8 allows you to remove duplicate elements from a stream?

A) distinct()
B) unique()
C) removeDuplicates()
D) filter()

Answer: A) distinct()

What does the flatMap() method in Java 8 do?

A) It maps elements to a new type
B) It flattens a nested stream of elements into a single stream
C) It sorts elements in the stream
D) It applies a function to each element and returns a stream

Answer: B) It flattens a nested stream of elements into a single stream

Which of the following is true about method references in Java 8?

A) They refer to instance methods only
B) They can refer to static methods, instance methods, and constructors
C) They can refer only to private methods
D) They are the same as lambda expressions in functionality but more concise

Answer: B) They can refer to static methods, instance methods, and constructors

 

61. Which of the following is a valid way to define a lambda expression in Java 8?

A) () -> { return “Hello”; }
B) () => { “Hello” }
C) () -> “Hello”;
D) function() -> “Hello”;

Answer: C) () -> “Hello”;

 

Which of the following is true about Java 8 streams?

A) Streams are mutable
B) Streams can be reused once created
C) Streams can only be used once
D) Streams can store state

Answer: C) Streams can only be used once

Which class is used to handle asynchronous programming in Java 8?

A) Future
B) CompletableFuture
C) Promise
D) Task

Answer: B) CompletableFuture

What is the default return type of the Stream.reduce() method in Java 8?

A) Optional
B) Stream
C) List
D) Integer

Answer: A) Optional

Which of the following is an example of a functional interface in Java 8?

A) util.Comparator
B) java.lang.Runnable
C) java.util.List
D) java.util.Map

Answer: B) java.lang.Runnable

Which of the following statements is true regarding the Optional class in Java 8?

A) Optional is a wrapper class for any type
B) Optional helps handle null values gracefully
C) Optional is a collection class
D) Optional does not provide methods for null handling

Answer: B) Optional helps handle null values gracefully

How do you create a stream from an array in Java 8?

A) fromArray()
B) Arrays.stream()
C) Stream.ofArray()
D) Stream.create()

Answer: B) Arrays.stream()

In Java 8, which of the following methods is used to create a stream from a collection?

A) stream()
B) createStream()
C) toStream()
D) fromCollection()

Answer: A) stream()

Which of the following is an example of a default method in an interface in Java 8?

A) public void myMethod();
B) default void myDefaultMethod() {}
C) static void myMethod() {}
D) protected void myDefaultMethod() {}

Answer: B) default void myDefaultMethod() {}

Which of the following stream operations is an intermediate operation?

A) collect()
B) forEach()
C) map()
D) reduce()

Answer: C) map()

In Java 8, how do you define a method reference to a static method?

A) ClassName::methodName
B) methodName::ClassName
C) methodReference::methodName
D) ::methodName

Answer: A) ClassName::methodName

Which of the following is an advantage of using streams in Java 8?

A) Streams provide parallel processing capabilities
B) Streams are always faster than loops
C) Streams support mutation of elements
D) Streams require manual memory management

Answer: A) Streams provide parallel processing capabilities

Which of the following is used to apply an action to each element of a stream in Java 8?

A) reduce()
B) collect()
C) forEach()
D) map()

Answer: C) forEach()

In Java 8, what does the Collectors.toList() method do?

A) Converts a list to a stream
B) Collects the elements of the stream into a List
C) Converts a List to a Set
D) Collects the elements into a Map

Answer: B) Collects the elements of the stream into a List

Which of the following is true about the Stream class in Java 8?

A) Stream supports only sequential processing
B) Stream supports parallel processing
C) Stream is not part of the util package
D) Stream is a subclass of Collection

Answer: B) Stream supports parallel processing

In Java 8, which method of the Optional class returns the value if present, or the provided default value if absent?

A) get()
B) orElse()
C) ifPresent()
D) map()

Answer: B) orElse()

How can you generate an infinite stream in Java 8?

A) generate()
B) Stream.infinite()
C) Stream.iterate()
D) Stream.of()

Answer: A) Stream.generate()

Which of the following is a correct example of using flatMap() in Java 8?

A) flatMap(i -> Arrays.asList(i, i * 2));
B) stream.flatMap(i -> { return i; });
C) stream.flatMap(i -> i * 2);
D) stream.flatMap(i -> new List<>(i));

Answer: A) stream.flatMap(i -> Arrays.asList(i, i * 2));

Which of the following methods in Stream is used to convert elements into another form (such as collecting into a Map)?

A) collect()
B) map()
C) reduce()
D) flatMap()

Answer: A) collect()

Which of the following is used to perform a side-effect operation for each element of the stream in Java 8?

A) map()
B) forEach()
C) reduce()
D) collect()

Answer: B) forEach()

Which of the following methods is used to find the maximum value in a stream of integers in Java 8?

A) max()
B) reduce()
C) sum()
D) aggregate()

Answer: A) max()

Which of the following methods does not belong to the Stream interface in Java 8?

A) findFirst()
B) peek()
C) reduce()
D) clear()

Answer: D) clear()

 

Which method in Stream is used to skip elements based on a condition in Java 8?

A) skip()
B) takeWhile()
C) filter()
D) limit()

Answer: A) skip()

Which of the following is true about Java 8 lambdas?

A) They can access local variables from the enclosing method only if they are final or effectively final
B) They can access local variables from the enclosing method without any restrictions
C) They cannot access instance variables
D) They are not able to throw exceptions

Answer: A) They can access local variables from the enclosing method only if they are final or effectively final

Which of the following is a valid way to define a method reference in Java 8?

A) ClassName::methodName
B) methodName
C) methodReference::methodName
D) methodName::Class

Answer: A) ClassName::methodName

 

Which of the following is an example of a terminal operation in a stream in Java 8?

A) map()
B) filter()
C) forEach()
D) peek()

Answer: C) forEach()

Which method is used to convert a stream of objects to a stream of their string representations in Java 8?

A) mapToString()
B) toString()
C) map()
D) flatMap()

Answer: C) map()

 

91. Which of the following is true about the Stream.distinct() method in Java 8?

A) It filters out all null values from the stream
B) It removes duplicate elements based on their hashCode()
C) It removes duplicate elements based on their content using the equals() method
D) It does not affect the stream elements

Answer: C) It removes duplicate elements based on their content using the equals() method

 

In Java 8, which method is used to convert a stream to a list?

A) toList()
B) Stream.collect()
C) Stream.toArray()
D) Stream.getList()

Answer: B) Stream.collect()

Which of the following methods is used to sort a stream in Java 8?

A) sort()
B) orderBy()
C) streamSort()
D) sorted()

Answer: D) sorted()

Which interface represents a function that takes one argument and returns a result in Java 8?

A) Function
B) Consumer
C) Predicate
D) Supplier

Answer: A) Function

In Java 8, which method is used to create a stream from a range of integers?

A) ofRange()
B) IntStream.range()
C) Stream.range()
D) IntStream.createRange()

Answer: B) IntStream.range()

Which method is used to get the first element from an Optional in Java 8?

A) get()
B) orElse()
C) ifPresent()
D) findFirst()

Answer: A) get()

Which of the following functional interfaces represents a predicate in Java 8?

A) Predicate<T>
B) Function<T, R>
C) Consumer<T>
D) Supplier<T>

Answer: A) Predicate<T>

 

Which of the following methods can be used to combine two streams in Java 8?

A) concat()
B) merge()
C) combine()
D) flatMap()

Answer: A) concat()

In Java 8, which interface represents a function that takes no arguments and returns a result?

A) Runnable
B) Supplier
C) Function
D) Consumer

Answer: B) Supplier

Which of the following statements about Optional in Java 8 is true?

A) Optional can be used to avoid null pointer exceptions
B) Optional is always empty when created
C) Optional is a wrapper class for collections
D) Optional can be created only with of()

Answer: A) Optional can be used to avoid null pointer exceptions

What is the purpose of the flatMap() method in Java 8 streams?

A) It flattens the structure of the stream
B) It maps the elements of the stream to a different type
C) It performs side-effects on the elements of the stream
D) It sorts the elements in the stream

Answer: A) It flattens the structure of the stream

Which of the following methods is used to handle multiple exceptions in CompletableFuture in Java 8?

A) exceptionally()
B) handle()
C) whenComplete()
D) catchException()

Answer: B) handle()

What is the purpose of the map() method in a stream?

A) To perform actions on each element and return a new stream
B) To merge the elements of the stream
C) To filter the elements of the stream
D) To collect elements into a collection

Answer: A) To perform actions on each element and return a new stream

Which of the following statements is true about Java 8’s default methods in interfaces?

A) They can have a body
B) They must be abstract
C) They cannot be overridden
D) They can only be used with abstract methods

Answer: A) They can have a body

Which method in the Stream class is used to find the first element in the stream that matches a condition in Java 8?

A) findFirst()
B) findMatch()
C) findAny()
D) filter()

Answer: A) findFirst()

What does the Collectors.groupingBy() method do in Java 8?

A) It groups elements by a given criterion
B) It filters elements based on a condition
C) It reduces the stream to a single element
D) It sorts elements based on a comparator

Answer: A) It groups elements by a given criterion

Which of the following is the correct syntax for using forEach() in a stream?

A) forEach(action)
B) stream.forEach(action, filter)
C) stream.forEach(action, condition)
D) stream.forEach() action

Answer: A) stream.forEach(action)

 

What is the purpose of the Predicate functional interface in Java 8?

A) To represent a function that takes one argument and produces a result
B) To represent a boolean-valued function of one argument
C) To represent an operation that takes two arguments and produces a result
D) To represent an action that consumes a value and returns nothing

Answer: B) To represent a boolean-valued function of one argument

In Java 8, which of the following is true about method references?

A) They cannot be used with lambda expressions
B) They are a shorthand for lambda expressions
C) They always require a return statement
D) They are used exclusively for instance methods

Answer: B) They are a shorthand for lambda expressions

Which of the following methods in Stream is used to limit the number of elements in the stream?

A) takeWhile()
B) limit()
C) skip()
D) restrict()

Answer: B) limit()

 

What is the return type of the Stream.collect() method?

A) List
B) Set
C) Map
D) Collector

Answer: D) Collector

Which of the following is the correct way to create an infinite stream of integers starting from 1 in Java 8?

A) generate(1)
B) IntStream.iterate(1, i -> i + 1)
C) Stream.of(1)
D) Stream.generate(() -> 1)

Answer: B) IntStream.iterate(1, i -> i + 1)

Which of the following methods returns a stream of the elements in an array in Java 8?

A) of()
B) Arrays.stream()
C) Stream.create()
D) Arrays.list()

Answer: B) Arrays.stream()

 

What does the Stream.peek() method do in Java 8?

A) It collects elements into a collection
B) It filters elements based on a condition
C) It allows you to perform a side-effect operation on the stream elements
D) It sorts the stream elements

Answer: C) It allows you to perform a side-effect operation on the stream elements

Which of the following statements is true about Collectors.joining() in Java 8?

A) It combines the stream elements into a single string
B) It filters elements based on a condition
C) It collects the stream elements into a list
D) It maps stream elements to a new form

Answer: A) It combines the stream elements into a single string

 

What is the purpose of the Optional.orElse() method in Java 8?

A) To return the value if present, otherwise it will throw an exception
B) To return the value if present, otherwise return a default value
C) To wrap the value in an Optional
D) To check if the value is present

Answer: B) To return the value if present, otherwise return a default value

Which method in the Stream class is used to concatenate multiple streams in Java 8?

A) concat()
B) merge()
C) combine()
D) flatMap()

Answer: A) concat()

Which of the following is true about the Collectors.toMap() method in Java 8?

A) It collects stream elements into a map using the provided key and value functions
B) It collects stream elements into a list
C) It collects stream elements into a set
D) It merges the elements based on a predicate

Answer: A) It collects stream elements into a map using the provided key and value functions

What does the Stream.flatMap() method do in Java 8?

A) It maps each element to a single value
B) It transforms each element into a stream of elements and flattens the result
C) It sorts the stream elements
D) It filters the stream based on a condition

Answer: B) It transforms each element into a stream of elements and flattens the result

 

Which method is used to create an empty Optional in Java 8?

A) empty()
B) Optional.of()
C) Optional.get()
D) Optional.none()

Answer: A) Optional.empty()

Which method of Stream class is used to skip the first n elements of a stream in Java 8?

A) skip()
B) dropWhile()
C) takeWhile()
D) discard()

Answer: A) skip()

 

129. Which of the following methods is used to create an unmodifiable collection in Java 8?

A) unmodifiableList()
B) Stream.unmodifiable()
C) List.of()
D) Set.of()

Answer: A) Collections.unmodifiableList()

 

Which of the following is true about the Stream.forEach() method in Java 8?

A) It modifies the elements of the stream
B) It performs an action on each element of the stream
C) It filters the stream elements
D) It returns a new stream

Answer: B) It performs an action on each element of the stream

Which method is used to combine two or more functions in Java 8?

A) andThen()
B) combine()
C) compose()
D) merge()

Answer: A) andThen()

Which method is used to iterate over a collection in Java 8?

A) forEach()
B) each()
C) loop()
D) iterate()

Answer: A) forEach()

Which of the following can be used to sort a stream of strings in Java 8 in reverse order?

A) sort(Comparator.reverseOrder())
B) Stream.sorted(Comparator.reverseOrder())
C) Stream.reverseSort()
D) Stream.reverse()

Answer: B) Stream.sorted(Comparator.reverseOrder())

Which of the following is the correct way to find the maximum value in a stream of integers using Java 8?

A) max()
B) Stream.getMax()
C) Stream.highest()
D) Stream.findMax()

Answer: A) Stream.max()

In Java 8, what type of variable can be used in a lambda expression?

A) Instance variables
B) Local variables
C) Static variables
D) All of the above

Answer: B) Local variables

Which functional interface does Stream.filter() use in Java 8?

A) Predicate<T>
B) Function<T, R>
C) Consumer<T>
D) Supplier<T>

Answer: A) Predicate<T>

What is the default value of an Optional object in Java 8?

A) null
B) empty()
C) Optional.of()
D) Optional.get()

Answer: B) Optional.empty()

 

Which method is used to get the minimum value from a stream in Java 8?

A) min()
B) Stream.lowest()
C) Stream.getMin()
D) Stream.findMin()

Answer: A) Stream.min()

In Java 8, which of the following is an example of a terminal operation?

A) map()
B) filter()
C) reduce()
D) peek()

Answer: C) reduce()

Which of the following methods returns a new stream that skips the first n elements in Java 8?

A) takeWhile()
B) skip()
C) limit()
D) forEach()

Answer: B) skip()

 

146. In Java 8, what is the return type of the Stream.collect() method when used with Collectors.toList()?

A) List
B) Set
C) Map
D) Collector

Answer: A) List

 

Which of the following is true about the CompletableFuture class in Java 8?

A) It represents an asynchronous computation
B) It is a blocking class
C) It is a stream-based class
D) It is a subclass of Future

Answer: A) It represents an asynchronous computation

What is the purpose of the Optional.ifPresent() method in Java 8?

A) To check if the value is null
B) To perform an action if the value is present
C) To retrieve the value
D) To return a default value

Answer: B) To perform an action if the value is present

What is the purpose of Stream.anyMatch() in Java 8?

A) To check if at least one element matches a condition
B) To filter elements based on a condition
C) To transform elements of a stream
D) To get the first element that matches a condition

Answer: A) To check if at least one element matches a condition

 

Which of the following is NOT a functional interface in Java 8?

A) Runnable
B) Callable
C) Function
D) Consumer

Answer: B) Callable

Which method is used to retrieve the value of an Optional object in Java 8, if it is present?

A) get()
B) retrieve()
C) extract()
D) orElse()

Answer: A) get()

 

Which of the following methods is used to sort a stream in Java 8?

A) sorted()
B) sort()
C) orderBy()
D) arrange()

Answer: A) sorted()

What does the Stream.reduce() method do in Java 8?

A) It collects the stream elements into a list
B) It transforms the stream elements into a single value
C) It filters the stream elements based on a condition
D) It performs an action on each element

Answer: B) It transforms the stream elements into a single value

Which of the following is true about the Predicate interface in Java 8?

A) It takes one argument and returns a boolean
B) It takes no arguments and returns a boolean
C) It takes one argument and returns a string
D) It takes one argument and returns an integer

Answer: A) It takes one argument and returns a boolean

 

Which of the following methods can be used to remove duplicates from a stream in Java 8?

A) distinct()
B) filter()
C) unique()
D) removeDuplicates()

Answer: A) distinct()

What does the Stream.limit() method do in Java 8?

A) It limits the stream to the first n elements
B) It filters the stream based on a condition
C) It transforms each element of the stream
D) It sorts the stream

Answer: A) It limits the stream to the first n elements

Which method of the Stream interface is used to convert the elements into a list in Java 8?

A) toList()
B) collect()
C) map()
D) toArray()

Answer: B) collect()

Which method in the Optional class returns the value if present or the given default value otherwise?

A) orElse()
B) get()
C) isPresent()
D) map()

Answer: A) orElse()

Which method in Java 8 is used to convert an array into a stream?

A) asStream()
B) Stream.of()
C) Stream.toArray()
D) Arrays.stream()

Answer: D) Arrays.stream()

Which of the following is used to create an immutable set in Java 8?

A) HashSet()
B) of()
C) Collections.unmodifiableSet()
D) TreeSet()

Answer: B) Set.of()

What is the purpose of the CompletableFuture.thenApply() method in Java 8?

A) To combine two futures
B) To handle exceptions asynchronously
C) To process the result of the computation
D) To block until the computation is completed

Answer: C) To process the result of the computation

 

Which of the following statements is true about the Lambda expressions in Java 8?

A) Lambda expressions can access only final local variables
B) Lambda expressions can access both final and non-final local variables
C) Lambda expressions can access only non-final local variables
D) Lambda expressions cannot access any local variables

Answer: A) Lambda expressions can access only final local variables

Which of the following is true about the Stream.peek() method in Java 8?

A) It is a terminal operation
B) It is used to examine the elements of the stream without consuming them
C) It is used to modify elements in the stream
D) It throws an exception if the stream is empty

Answer: B) It is used to examine the elements of the stream without consuming them

Which interface is used by the Stream.map() method in Java 8?

A) Function<T, R>
B) Consumer<T>
C) Predicate<T>
D) Supplier<T>

Answer: A) Function<T, R>

Which of the following is a valid method for iterating through a Map in Java 8?

A) forEach()
B) map.entries()
C) map.iterate()
D) map.entries().forEach()

Answer: A) map.forEach()

 

Which of the following methods is used to create an immutable list in Java 8?

A) of()
B) ArrayList()
C) Collections.unmodifiableList()
D) LinkedList()

Answer: A) List.of()

In Java 8, which method is used to check if any element of a stream matches a given predicate?

A) anyMatch()
B) noneMatch()
C) allMatch()
D) findFirst()

Answer: A) anyMatch()

Which of the following is a new feature of Java 8?

A) Default methods in interfaces
B) Multi-threading support
C) Lambda functions in classes
D) A new type of method for strings

Answer: A) Default methods in interfaces

 

What is the purpose of the Stream.forEachOrdered() method in Java 8?

A) To iterate over the stream while preserving the encounter order
B) To sort the stream
C) To iterate over the stream without preserving the order
D) To apply a terminal operation to the stream

Answer: A) To iterate over the stream while preserving the encounter order

Which of the following classes in Java 8 is a functional interface?

A) Thread
B) Runnable
C) Executor
D) String

Answer: B) Runnable

What is the purpose of the Optional.ifPresentOrElse() method in Java 8?

A) It returns a default value if the value is not present
B) It executes a specified action if the value is present
C) It executes an action if the value is present and another action if not
D) It throws an exception if the value is not present

Answer: C) It executes an action if the value is present and another action if not

 

180. Which of the following methods is used to transform each element of a stream into another type in Java 8?

A) map()
B) filter()
C) reduce()
D) collect()

Answer: A) map()