Java Functional Programming Practice Quiz
- What is the primary feature of functional programming in Java?
A) Objects and inheritance
B) First-class functions
C) Loops and iteration
D) Data encapsulation
Answer: B) First-class functions
- Which interface represents a function that takes a single argument and produces a result in Java?
A) Runnable
B) Callable
C) Function
D) Consumer
Answer: C) Function
- What is the purpose of the Stream class in Java?
A) To handle exception handling
B) To perform sequential and parallel aggregate operations
C) To manage file I/O
D) To create and manage threads
Answer: B) To perform sequential and parallel aggregate operations
- What does the map() function do in a Java Stream?
A) Filters the stream based on conditions
B) Transforms each element of the stream
C) Sorts the stream
D) Combines elements of the stream
Answer: B) Transforms each element of the stream
- Which of the following is used to represent a function that consumes a single argument and returns no result?
A) Function
B) Consumer
C) Supplier
D) Predicate
Answer: B) Consumer
- What is the return type of filter() in Java Streams?
A) boolean
B) int
C) Stream
D) void
Answer: C) Stream
- Which functional interface is used to generate a sequence of elements?
A) Predicate
B) Function
C) Supplier
D) Consumer
Answer: C) Supplier
- What does reduce() function do in Java Streams?
A) Combines all elements into one
B) Removes all elements from the stream
C) Filters elements based on conditions
D) Transforms elements
Answer: A) Combines all elements into one
- In Java, how can you convert a List into a stream?
A) list.stream()
B) Stream.of(list)
C) list.toStream()
D) Stream.list()
Answer: A) list.stream()
- What is the function of flatMap() in Java Streams?
A) Maps elements and returns another stream
B) Filters elements based on a predicate
C) Merges two streams into one
D) Returns a new stream with the same elements
Answer: A) Maps elements and returns another stream
- Which of the following operations is not a terminal operation in Java Streams?
A) collect()
B) reduce()
C) filter()
D) forEach()
Answer: C) filter()
- What is a Predicate in Java?
A) A function that accepts one argument and returns a boolean
B) A function that accepts one argument and returns a result
C) A function that takes no arguments
D) A function that supplies a value
Answer: A) A function that accepts one argument and returns a boolean
- How do you create an immutable collection in Java 8?
A) List.copyOf()
B) Collections.unmodifiableList()
C) List.unmodifiable()
D) List.createImmutable()
Answer: B) Collections.unmodifiableList()
- What is a primary advantage of using Java functional programming?
A) More mutable objects
B) Easier debugging and tracing
C) Cleaner and more readable code
D) Increased performance in all scenarios
Answer: C) Cleaner and more readable code
- What is the role of Optional in Java?
A) To handle null safely
B) To avoid loops
C) To represent an immutable object
D) To provide multi-threading capabilities
Answer: A) To handle null safely
- Which of these methods does not belong to the Stream interface?
A) peek()
B) limit()
C) forEach()
D) set()
Answer: D) set()
- What does the Supplier functional interface do in Java?
A) Returns a value
B) Consumes a value and produces a result
C) Returns a boolean result
D) Transforms a value
Answer: A) Returns a value
- Which method in Java Streams is used to create a new stream from an existing one, based on specific conditions?
A) map()
B) filter()
C) reduce()
D) forEach()
Answer: B) filter()
- In which package are the functional interfaces like Function, Consumer, and Supplier found in Java?
A) java.util.function
B) java.lang
C) java.io
D) java.util.stream
Answer: A) java.util.function
- What is the default behavior of Stream.forEach()?
A) Iterates over the elements sequentially
B) Iterates over the elements in parallel
C) Applies a filter to the stream
D) Reduces the stream to a single element
Answer: A) Iterates over the elements sequentially
- Which of these is true about Collectors in Java Streams?
A) Collectors are used for stream transformation
B) Collectors are used for reducing streams
C) Collectors are not available in functional programming
D) Collectors are used for grouping and accumulating data
Answer: D) Collectors are used for grouping and accumulating data
- Which method is used to convert a stream to a collection in Java?
A) toList()
B) collect()
C) streamToList()
D) convert()
Answer: B) collect()
- What is the main advantage of parallel streams in Java?
A) It always guarantees improved performance
B) It processes data in parallel, potentially improving performance for large data sets
C) It avoids any kind of iteration
D) It simplifies stream transformations
Answer: B) It processes data in parallel, potentially improving performance for large data sets
- What does the distinct() method in Java Streams do?
A) Removes duplicate elements from the stream
B) Filters out even numbers from the stream
C) Transforms each element into a distinct one
D) Sorts elements in ascending order
Answer: A) Removes duplicate elements from the stream
- Which of the following is not a terminal operation in the Java Streams API?
A) forEach()
B) collect()
C) reduce()
D) map()
Answer: D) map()
- Which functional interface is used to represent a function that takes two arguments and produces a result in Java?
A) Function
B) BiFunction
C) Predicate
D) Consumer
Answer: B) BiFunction
- Which method in Java allows you to iterate over a stream with side effects?
A) map()
B) filter()
C) forEach()
D) reduce()
Answer: C) forEach()
- Which of the following is the correct way to create a stream of integers from a range in Java?
A) Stream.range(1, 10)
B) Stream.of(1, 10)
C) Stream.generate(1, 10)
D) Stream.rangeClosed(1, 10)
Answer: A) Stream.range(1, 10)
- Which method in Java’s Stream class is used to skip the first n elements?
A) limit()
B) skip()
C) take()
D) remove()
Answer: B) skip()
- Which of the following is true about immutable objects in Java functional programming?
A) They can be modified after creation
B) They are inherently thread-safe
C) They are not compatible with Streams
D) They do not allow method chaining
Answer: B) They are inherently thread-safe
- Which of the following functional interfaces represents a function that takes two arguments and returns a boolean result?
A) Predicate
B) BiPredicate
C) BiFunction
D) Function
Answer: B) BiPredicate
- Which method in the Stream class returns the number of elements in the stream?
A) count()
B) size()
C) length()
D) getCount()
Answer: A) count()
- What does the sorted() method in Java Streams do?
A) Sorts the elements in reverse order
B) Sorts the elements in ascending order by default
C) Sorts the elements using a comparator in descending order
D) Sorts elements in a custom order
Answer: B) Sorts the elements in ascending order by default
- Which of the following is true about Java’s Stream.parallel() method?
A) It creates a new thread for each element in the stream
B) It splits the stream into multiple substreams and processes them concurrently
C) It guarantees faster processing for small datasets
D) It is only available for sorted streams
Answer: B) It splits the stream into multiple substreams and processes them concurrently
- Which of the following functional interfaces is used when you need to return a boolean result after processing a single argument?
A) Function
B) Predicate
C) Consumer
D) Supplier
Answer: B) Predicate
- Which method in Java Streams is used to combine elements into a single result?
A) collect()
B) reduce()
C) map()
D) flatMap()
Answer: B) reduce()
- Which method is used to convert a stream to a list in Java?
A) streamToList()
B) collect(Collectors.toList())
C) toList()
D) stream.collect()
Answer: B) collect(Collectors.toList())
- Which method in Java Streams is used to check if all elements satisfy a given condition?
A) anyMatch()
B) noneMatch()
C) allMatch()
D) filter()
Answer: C) allMatch()
- What is the result of applying findFirst() on a stream in Java?
A) It finds the first element in the stream
B) It returns an optional containing the first element of the stream, if present
C) It returns the last element of the stream
D) It throws an exception if the stream is empty
Answer: B) It returns an optional containing the first element of the stream, if present
- What is the default sorting order in the sorted() method of Java Streams?
A) Descending order
B) Random order
C) Ascending order
D) Custom comparator
Answer: C) Ascending order
- Which method allows you to apply a side-effecting operation to each element in a stream?
A) forEach()
B) map()
C) flatMap()
D) reduce()
Answer: A) forEach()
- Which of these is a method in Optional class that returns the value if present, or a default value if absent?
A) get()
B) ifPresent()
C) orElse()
D) orElseThrow()
Answer: C) orElse()
- What is the role of Collector in Java Streams?
A) Collects values from streams and applies reductions
B) Collects elements from a collection into a stream
C) Collects elements into a new collection, like List or Set
D) It’s used for filtering stream elements
Answer: C) Collects elements into a new collection, like List or Set
- Which functional interface represents a function that takes a single argument and returns a result in Java?
A) Consumer
B) Predicate
C) Function
D) Supplier
Answer: C) Function
- What is the key difference between map() and flatMap() in Java Streams?
A) map() flattens the elements, flatMap() applies a function
B) map() transforms each element, flatMap() allows for stream concatenation
C) map() collects data, flatMap() filters data
D) Both methods do the same thing
Answer: B) map() transforms each element, flatMap() allows for stream concatenation
- Which of these methods is used to limit the number of elements in a stream in Java?
A) skip()
B) take()
C) limit()
D) setSize()
Answer: C) limit()
- Which of the following is true about Java’s Stream API?
A) Stream operations can be stateful or stateless
B) All stream operations are stateful
C) Only terminal operations are allowed to modify a stream
D) Streams can be processed only once
Answer: A) Stream operations can be stateful or stateless
- Which of these is a terminal operation in Java Streams?
A) map()
B) filter()
C) collect()
D) flatMap()
Answer: C) collect()
- How do you create a stream from a collection in Java?
A) stream.from(collection)
B) Stream.of(collection)
C) collection.stream()
D) Stream.collect(collection)
Answer: C) collection.stream()
- Which method in Java Streams returns true if any elements match a given condition?
A) anyMatch()
B) allMatch()
C) noneMatch()
D) filter()
Answer: A) anyMatch()
- What is the purpose of the takeWhile() method in Java Streams?
A) Filters elements until a condition is false
B) Returns the first element of the stream
C) Skips elements until a condition is true
D) Limits the number of elements in a stream
Answer: A) Filters elements until a condition is false
- What happens when you invoke Stream.generate() in Java?
A) It generates a sequence of numbers
B) It generates a stream based on a supplier function
C) It generates random elements
D) It produces a finite stream
Answer: B) It generates a stream based on a supplier function
- Which method in the Stream class is used to join the elements of a stream into a single string?
A) toList()
B) join()
C) collect(Collectors.joining())
D) concatenate()
Answer: C) collect(Collectors.joining())
- What is the default behavior of Stream.forEachOrdered()?
A) It processes elements in parallel
B) It guarantees the order of processing elements in the stream
C) It skips null elements
D) It processes elements randomly
Answer: B) It guarantees the order of processing elements in the stream
- Which of the following statements is true about Java Streams?
A) A stream can be reused after a terminal operation
B) Streams can be processed multiple times without any problem
C) Streams are stateful by default
D) Streams are lazy and only compute values when necessary
Answer: D) Streams are lazy and only compute values when necessary
- What is the purpose of Stream.concat() method in Java?
A) Combines two streams into a single stream
B) Joins elements from different collections into one
C) Removes duplicate elements from two streams
D) Sorts two streams together
Answer: A) Combines two streams into a single stream
- Which of these is a method to collect elements from a stream into a set?
A) collect(Collectors.toSet())
B) collect(Collectors.toList())
C) collect(Collectors.toMap())
D) toSet()
Answer: A) collect(Collectors.toSet())
- What does the reduce() method do when applied to a stream of integers in Java?
A) It filters out elements based on conditions
B) It combines elements using an associative accumulation function
C) It sorts the elements in descending order
D) It performs a transformation on each element of the stream
Answer: B) It combines elements using an associative accumulation function
- How can you apply a transformation to a stream of strings in Java?
A) map(String::toUpperCase)
B) filter(String::toUpperCase)
C) forEach(String::toUpperCase)
D) collect(String::toUpperCase)
Answer: A) map(String::toUpperCase)
- What is a key advantage of using the Stream API in Java?
A) It always guarantees faster execution
B) It allows processing data in a declarative and functional style
C) It requires less memory for processing large data
D) It allows random access to stream elements
Answer: B) It allows processing data in a declarative and functional style
- Which of the following is a valid way to create a stream from an array in Java?
A) Arrays.stream(array)
B) Stream.of(array)
C) array.stream()
D) Both A and B
Answer: D) Both A and B
- Which of the following methods is used to return a stream consisting of the elements of the original stream, skipping the first N elements?
A) skip()
B) take()
C) limit()
D) skipWhile()
Answer: A) skip()
- Which method is used to apply a transformation to each element in a stream and flatten it into a single stream?
A) map()
B) flatMap()
C) filter()
D) reduce()
Answer: B) flatMap()
- Which of the following is a terminal operation in Java Streams?
A) map()
B) filter()
C) forEach()
D) flatMap()
Answer: C) forEach()
- Which of the following statements about Optional is correct?
A) An Optional can never be null
B) Optional can contain multiple values
C) Optional is used to represent a non-null value
D) Optional is a container object which may or may not contain a value
Answer: D) Optional is a container object which may or may not contain a value
- Which method would you use to perform an action for each element in a stream without modifying the elements themselves?
A) map()
B) forEach()
C) collect()
D) reduce()
Answer: B) forEach()
- Which of the following is NOT a valid functional interface in Java?
A) Function<T, R>
B) Predicate<T>
C) Consumer<T>
D) Task<T, R>
Answer: D) Task<T, R>
- What is the result of applying filter() to a stream in Java?
A) It transforms each element based on a condition
B) It applies a function to the elements and returns a new stream
C) It returns a stream that includes only elements that match a given condition
D) It aggregates elements in the stream into a single result
Answer: C) It returns a stream that includes only elements that match a given condition
- What is the purpose of the Collectors.groupingBy() method in Java?
A) Collects elements into a List
B) Groups elements of the stream by a classifier function
C) Sorts elements in a stream
D) Reduces the stream into a single result
Answer: B) Groups elements of the stream by a classifier function
- Which of the following operations is considered a “lazy” operation in Java Streams?
A) collect()
B) map()
C) forEach()
D) reduce()
Answer: B) map()
- Which of the following methods is used to check if at least one element in the stream matches a given condition?
A) anyMatch()
B) allMatch()
C) noneMatch()
D) map()
Answer: A) anyMatch()
- How can you ensure that elements in a stream are processed sequentially in Java?
A) Use Stream.sequential()
B) Use Stream.parallel()
C) Use Stream.forEachOrdered()
D) Use Stream.sorted()
Answer: A) Use Stream.sequential()
- Which method in the Optional class allows you to return a default value if the Optional is empty?
A) ifPresent()
B) get()
C) orElse()
D) orElseThrow()
Answer: C) orElse()
- What does the Stream.peek() method do?
A) Returns a new stream with modified elements
B) Allows inspecting the elements of the stream for debugging purposes
C) Reduces the stream to a single element
D) Collects the elements of the stream
Answer: B) Allows inspecting the elements of the stream for debugging purposes
- Which method in Java Streams can be used to skip elements until a condition becomes true?
A) skipWhile()
B) takeWhile()
C) skip()
D) filter()
Answer: A) skipWhile()
- What does the Stream.distinct() method do?
A) Returns a new stream with duplicate elements removed
B) Filters out elements that do not match a condition
C) Collects elements into a distinct collection
D) Sorts the elements of the stream in ascending order
Answer: A) Returns a new stream with duplicate elements removed
- Which method in Java Streams is used to reduce a stream to a single value by accumulating elements?
A) map()
B) reduce()
C) collect()
D) flatMap()
Answer: B) reduce()
- Which functional interface represents a function that takes no arguments and returns a result in Java?
A) Consumer
B) Supplier
C) Function
D) Predicate
Answer: B) Supplier
- What is the primary purpose of Stream.flatMap() in Java?
A) To apply a function to each element and flatten the result
B) To transform a stream of elements into a new stream
C) To filter out elements based on a condition
D) To reduce the stream to a single result
Answer: A) To apply a function to each element and flatten the result
- Which method is used to convert a stream of elements into a Map in Java?
A) collect(Collectors.toMap())
B) collect(Collectors.toList())
C) collect(Collectors.groupingBy())
D) collect(Collectors.toSet())
Answer: A) collect(Collectors.toMap())
- What is the correct way to create a parallel stream in Java?
A) stream.parallel()
B) Stream.parallel()
C) Stream.of(array).parallel()
D) stream.toParallel()
Answer: C) Stream.of(array).parallel()
- What does the Stream.limit() method do?
A) Limits the stream to a certain number of elements
B) Restricts the stream to a particular data type
C) Returns a stream that includes elements until a condition is met
D) Applies a condition to limit the elements of the stream
Answer: A) Limits the stream to a certain number of elements
- Which method in the Optional class throws an exception if no value is present?
A) ifPresent()
B) orElse()
C) orElseThrow()
D) get()
Answer: C) orElseThrow()
- Which of the following is the correct syntax to create a stream from a list in Java?
A) list.stream()
B) Stream.of(list)
C) Stream.create(list)
D) list.toStream()
Answer: A) list.stream()
- Which method is used to concatenate two streams into one in Java?
A) Stream.concat()
B) Stream.combine()
C) Stream.merge()
D) Stream.append()
Answer: A) Stream.concat()
- Which of these operations in Java Streams is intermediate and can be chained?
A) reduce()
B) collect()
C) map()
D) forEach()
Answer: C) map()
- Which method is used to check whether no elements of the stream match a given condition?
A) noneMatch()
B) anyMatch()
C) allMatch()
D) filter()
Answer: A) noneMatch()
- What is the purpose of the Stream.of() method in Java?
A) Creates a stream from an array
B) Creates a stream from a collection
C) Creates a stream from individual elements
D) Creates a stream from a file
Answer: C) Creates a stream from individual elements
- What is the default behavior of Stream when used with parallel()?
A) It processes elements sequentially
B) It processes elements concurrently across multiple threads
C) It sorts the stream automatically
D) It filters out elements
Answer: B) It processes elements concurrently across multiple threads
- Which method in Java Streams is used to combine the results of a mapping operation into a single stream?
A) map()
B) flatMap()
C) groupBy()
D) reduce()
Answer: B) flatMap()
- Which of the following is the correct way to create a stream from a Set in Java?
A) set.stream()
B) Stream.of(set)
C) Stream.create(set)
D) set.toStream()
Answer: A) set.stream()
- What does the Collectors.toList() collector do in Java?
A) Converts a stream into a list
B) Sorts the stream and collects into a list
C) Groups the stream elements into a list
D) Collects the elements of the stream into a Set
Answer: A) Converts a stream into a list
- Which of the following methods in Stream is used to return the first element of the stream, if present?
A) findFirst()
B) findAny()
C) limit(1)
D) get()
Answer: A) findFirst()
- How can you generate an infinite stream of even numbers starting from 0 in Java?
- A) iterate(0, n -> n + 2)
B) Stream.generate(n -> n + 2)
C) Stream.of(0, 2, 4, 6)
D) Stream.generate(0)
Answer: A) Stream.iterate(0, n -> n + 2)
- Which of the following operations is NOT supported by a stream in Java?
A) map()
B) reduce()
C) add()
D) collect()
Answer: C) add()
- What does the Stream.sorted() method do in Java?
A) Returns a sorted stream of elements
B) Returns a stream with the elements sorted in ascending order by default
C) Returns a stream with elements sorted in the order specified by a comparator
D) All of the above
Answer: D) All of the above
- Which method in Optional allows you to apply a function to the value if present, and return a new Optional?
A) map()
B) ifPresent()
C) orElse()
D) filter()
Answer: A) map()
- Which method in Stream can be used to combine the elements of the stream into a single result, such as summing all numbers?
A) reduce()
B) forEach()
C) collect()
D) toList()
Answer: A) reduce()
- Which of the following statements about Stream.forEach() is correct?
A) It is a terminal operation that performs an action for each element of the stream
B) It returns a new stream
C) It is a lazy operation
D) It filters elements of the stream based on a condition
Answer: A) It is a terminal operation that performs an action for each element of the stream
- What is the behavior of Stream.limit() in Java?
A) Limits the number of elements processed in the stream to the given count
B) Returns only distinct elements from the stream
C) Limits the stream to elements that match a condition
D) Returns the first N elements of the stream
Answer: A) Limits the number of elements processed in the stream to the given count
- Which of the following methods allows you to check if all elements in a stream satisfy a condition?
A) anyMatch()
B) noneMatch()
C) allMatch()
D) filter()
Answer: C) allMatch()
- What does the Collectors.joining() collector do in Java?
A) Combines the elements of the stream into a single string
B) Joins two separate streams into one
C) Collects elements into a List of Strings
D) Joins the elements into a Map
Answer: A) Combines the elements of the stream into a single string
- How do you create a stream that contains the elements of a list, excluding those that are null in Java?
- A) stream().filter(Objects::nonNull)
B) list.stream().map(Objects::nonNull)
C) Stream.of(list).filter(Objects::nonNull)
D) list.stream().removeNulls()
Answer: A) list.stream().filter(Objects::nonNull)
- What is the default type of a stream in Java?
A) Integer
B) String
C) Object
D) Void
Answer: C) Object
- Which of the following methods allows you to filter a stream based on a condition?
A) filter()
B) map()
C) reduce()
D) collect()
Answer: A) filter()
- Which method in Stream is used to collect the elements of the stream into a map?
A) collect() with Collectors.toMap()
B) collect() with Collectors.toList()
C) collect() with Collectors.groupingBy()
D) collect() with Collectors.joining()
Answer: A) collect() with Collectors.toMap()
- Which method would you use to return a stream containing the elements of the original stream that match a specific condition?
A) map()
B) filter()
C) reduce()
D) flatMap()
Answer: B) filter()
- Which of the following is true about the Stream.concat() method in Java?
A) It concatenates two streams into a single stream
B) It merges two streams based on a common key
C) It applies a function to combine two streams
D) It filters elements from both streams
Answer: A) It concatenates two streams into a single stream