75+ Java 8 CCEE MCQ
75+ Java 8 CCEE MCQ
Sr.No
Project Name YouTube Link
11 Tour and Travel System Project version 3.0 https://youtu.be/Dm7nOdpasWg?si=P_Lh2gcOFhlyudug
12 Gym Management system Project https://youtu.be/J8_7Zrkg7ag?si=LcxV51ynfUB7OptX
13 Online Driving License system Project https://youtu.be/3yRzsMs8TLE?si=JRI_z4FDx4Gmt7fn
14 Online Flight Booking system Project https://youtu.be/m755rOwdk8U?si=HURvAY2VnizIyJlh
15 Employee management system project https://youtu.be/lD1iE3W_GRw?si=Y_jv1xV_BljhrD0H
16 Online student school or college portal https://youtu.be/4A25aEKfei0?si=RoVgZtxMk9TPdQvD
17 Online movie booking system project https://youtu.be/Lfjv_U74SC4?si=fiDvrhhrjb4KSlSm
18 Online Pizza Delivery system project https://youtu.be/Tp3izreZ458?si=8eWAOzA8SVdNwlyM
19 Online Crime Reporting system Project https://youtu.be/0UlzReSk9tQ?si=6vN0e70TVY1GOwPO
20 Online Children Adoption Project https://youtu.be/3T5HC2HKyT4?si=bntP78niYH802I7N
pg. 4 contact us on 8007592194 / 9284926333 www.codewitharrays.in
MCQ on Java 8 Features
Q1. Which Java 8 feature allows us to filter a collection based on a predicate?
(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()
(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?
Q9. Which one among the following is the main feature introduced in Java 8?
(a) Annotations
(b) Generics
(d) Enumerations
(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
(a) A class that represents a value which can either be present or absent
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?
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()
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()
(c) HELLOWORLD
(c) When we wish to produce one single result from a sequence of elements
(b) 10
(c) 6
(d) 5
(a) 18
(b) 45
(c) 30
(d) 56
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
Q32. Which Java 8 feature allows us to iterate through a collection using lambda expressions?
(c) Optional
(d) Annotation Processing
Q33. Which of the following is not a valid target type for a lambda expression in Java 8?
(b) To represent a method that takes one argument and returns a value
(d) To represent a method that takes one argument and returns no value
(a) none
(b) Washington
—-
Explanation: Stream API is a new feature introduced in Java 8 that allows us to filter a collection
based on a predicate.
Explanation: Lambda expressions allow us to pass behavior as a parameter to a method, which can
be used to implement functional interfaces.
A3: c) “BAC”
Explanation: The code reduces the list of names to a single string by concatenating the second
character of each name. The resulting string is “BAC”.
A4: d) join()
Explanation: The new join() method in the String class in Java 8 allows you to join a collection of
strings into a single string with a specified delimiter. For examples of join() method, kindly
visit Examples of join() method.
A5: b) Optional.empty
Explanation: Since 11 is not divisible by 5, the findFirst() will return an empty stream. When the
stream is empty, the return value is an empty Optional which is represented by ‘Optional.empty’.
Explanation: In Java 8, a lambda expression is declared using the syntax (param1, param2) ->
expression, where param1 and param2 are the parameters of the lambda expression, and expression
is the body of the lambda expression.
Explanation: In addition to single abstract method, we can also have any number of default & static
methods inside a Functional Interface. Moreover, we don’t have any restrictions on including ‘Object’
class’ public methods inside Functional interfaces. Needless to say, they are equals(), hashCode(),
notify(), notifyAll(), toString(), wait(), getClass().
Explanation: The new Date and Time API in Java 8 provides a more comprehensive and flexible API
for working with dates and times, with classes such as LocalDate, LocalTime, and ZonedDateTime.
Explanation: Java 8 introduced lambda expressions, which allows you to write more concise and
readable code by providing a way to represent anonymous functions.
Explanation: StringJoiner is a new class added in Java 8 under java.util package. In fact, we can use it
for joining Strings by making use of a delimiter, prefix, and suffix. For example on StringJoiner class,
kindly visit a separate article on StringJoiner.
A11: c) 6
Explanation: The code filters the even numbers in the list, maps them to integers, and then sums
them up using the sum() method. The even numbers in the list are 2 and 4, and their sum is 6.
A12: c) Supplier
Explanation: The Supplier functional interface in Java 8 represents a function that takes no
arguments and returns an object.
Explanation: Default methods in Java 8 allow you to add new methods to interfaces without breaking
backward compatibility, by providing a default implementation that can be overridden by
implementing classes.
A14: d) All of the above
Explanation: Method reference in Java 8 allows you to reference a method or constructor by its
name, without actually calling it, with various types such as constructor references, static method
references, and instance method references.
Explanation: The Optional class in Java 8 is used to represent an optional value, which can either be
present or absent, to avoid NullPointerException and make code more readable.
A16: b) CompletableFuture
Explanation: The forEach() method in Java 8 streams is used to apply a specified operation to each
element of a stream.
A18: c) flatMap()
Explanation: If you want to transform the elements of a stream in some way. Use map() if you want
to transform each element into a single value. Use flatMap() if you want to transform each element
to multiple values and also compress/flatten the resulting stream.
Explanation: The Stream API in Java 8 provides a new way to work with collections in a functional
style, with operations such as filter, map, and reduce.
A20: d) collect()
Explanation: The collect() method of the ‘Stream’ interface is used to convert a stream into an array,
a collection, or any other data structure. The Collectors class provides various methods like
toCollection(), toList(), toSet(), toMap(), and toConcurrentMap() to collect the result of Stream into
List, Set, Map, and ConcurrentMap respectively. Please note that the collect() method doesn’t belong
to the Collectors class. It is defined in Stream interface and that’s the reason you can call it on Stream
after doing any filtering or mapping operations. However, it accepts a Collector to accumulate
elements of Stream into a specified Collection.
A21: c) HELLOWORLD
Explanation: The code filters the elements in the stream that contain the letter ‘o’, converts them to
uppercase strings, and prints them out using the forEach() method. The elements in the stream that
contain the letter ‘o’ are “hello” and “world”. Since ‘print’ (not ‘println’) method is used, it will print
the two strings without any space or line break in between.
A22: c) When we wish to produce one single result from a sequence of elements
Explanation: The flatMap() method in Java 8 streams is used to convert a nested stream into a single
stream. It flattens the nested stream by merging its elements into a single stream.
A23: a) 15
Explanation: The code reduces the stream to a single value by adding all its elements, and prints out
the result using the ifPresent() method. The sum of the elements in the stream is 1 + 2 + 3 + 4 + 5 =
15.
Explanation: A functional interface is an interface that has only one abstract method. We apply
@FunctionalInterface annotation in order to tell compiler that it is a functional interface. When we
try to add any additional abstract method in it, compiler will display a compilation error. On the
other hand, in the absence of @FunctionalInterface annotation, compiler will consider it as a normal
interface and doesn’t display any compilation error.
Explanation: The parallelStream() method in Java 8 is used to create a parallel stream from a
sequential stream. It allows for the processing of elements in the stream to be done in parallel.
A26: b) 45
Explanation: The code filters the numbers which are divisible by 3 in the list, maps them to integers,
and calculates the sum using the sum() method. The elements in the list which are divisible by 3 are
15, 3 and 27, and their sum is 45.
Explanation: The Optional class in Java 8 is used to store a reference to a value that may or may not
be null. It is used to avoid null pointer exceptions and to make the code more robust.
Explanation: The code sorts the elements in the list in natural order using the sorted() method and
prints them out using the forEach() method.
Explanation: The peek() method in Java 8 streams is used to apply an operation to each element of a
stream. It does not modify the stream, but allows for side effects such as logging or debugging.
A30: c) 1 2 3 4 5
Explanation: The code generates a stream of integers from 1 to 5 using the range() method, maps each
integer to a string with a trailing space using the mapToObj() method, and prints out the strings using
the forEach() method.
A31: a) () -> { System.out.println(“Hello”); }
Explanation: This lambda expression takes no arguments and has a single statement that prints
“Hello” to the console. The other options are invalid because they either have an incorrect number of
arguments or an incorrect syntax.
Explanation: Stream API is a new feature introduced in Java 8 that provides a functional way to
iterate, filter, and transform collections using lambda expressions.
Explanation: A lambda expression can only be used with a target type that is a functional interface or
an interface with a single abstract method. A class is not a valid target type for a lambda expression.
Explanation: The Supplier interface in Java 8 is a functional interface that represents a method that
doesn’t take any argument and returns a value. We use it when we need to get some value based on
some operation like supply Random numbers, supply Random OTPs, supply Random Passwords etc.
A35: a) none
Explanation: The code creates a list of strings, and then creates a stream from the list. The stream is
filtered to only include strings with a length greater than 10, and the first element of the resulting
stream is retrieved using the findFirst() method. Since no element is found which is greater than 10
in length, the orElse() method returns the string “none”.
1. What is a lambda expression in Java?
Answer:
Explanation:
Lambda expressions in Java are a way to define anonymous methods (functions) that can be passed as
arguments, stored in variables, or returned as values.
a) Java 6
b) Java 7
c) Java 8
d) Java 9
Answer:
c) Java 8
Explanation:
Lambda expressions were introduced in Java 8, bringing functional programming features to the language.
Answer:
Explanation:
The Stream API in Java allows processing sequences of elements in a functional style, enabling operations like
filtering, mapping, and reducing.
a) stream()
b) createStream()
c) asStream()
d) toStream()
Answer:
a) stream()
Explanation:
The stream() method is used to create a stream from a collection, such as a List or Set.
c) A collection of elements
Answer:
Explanation:
The Optional class in Java is a container object which may or may not contain a non-null value. It helps in
avoiding null pointer exceptions.
a) Runnable
b) Callable
c) Function
d) Serializable
Answer:
c) Function
Explanation:
The Function interface is a common functional interface used with lambda expressions in Java. It represents a
function that takes one argument and returns a result.
Answer:
Explanation:
The basic syntax for a lambda expression in Java is (parameters) -> expression. It can also have a block of
code if needed.
Answer:
Explanation:
Lambda expressions in Java can have multiple parameters, enclosed in parentheses and separated by commas.
Answer:
Explanation:
The filter() method in the Stream API is used to filter elements of a stream based on a given predicate.
10. What is the purpose of the forEach() method in the Stream API?
Answer:
Explanation:
The forEach() method is a terminal operation that iterates over each element in the stream and performs the
specified action.
Explanation:
A default method in an interface is a method with a default implementation. It allows interfaces to evolve
without breaking existing implementations.
a) isEmpty()
b) noneMatch()
c) count()
d) allMatch()
Answer:
b) noneMatch()
Explanation:
The noneMatch() method checks if no elements in the stream match a given predicate, which can be used to
check if the stream is empty.
a) To return an object
d) To create an object
Answer:
c) To test a condition on an object
Explanation:
The Predicate functional interface in Java is used to test a condition on an object and returns a boolean value.
Answer:
Explanation:
Lambda expressions can be used with the Comparator interface to simplify comparison logic.
c) To throw exceptions
Answer:
Explanation:
The Optional class in Java is used to represent the presence or absence of a value, helping to avoid null
pointer exceptions.
a) getValue()
b) retrieve()
c) get()
d) fetch()
Answer:
c) get()
Explanation:
Answer:
Explanation:
The map() method transforms elements in a stream, while flatMap() flattens nested structures such as streams
of streams into a single stream.
Answer:
Explanation:
Streams can be parallelized in Java using the parallelStream() method, which processes elements concurrently
on multiple cores.
19. What is the Collectors class in the Stream API used for?
Answer:
Explanation:
The Collectors class in the Stream API is used to collect elements from a stream into a collection, such as a List
or Set.
20. Which method in the Optional class is used to return a default
value if the value is absent?
a) orElse()
b) ifPresent()
c) getOrDefault()
d) fetch()
Answer:
a) orElse()
Explanation:
The orElse() method in the Optional class returns a default value if the value is absent.
21. Which new date and time API was introduced in Java 8?
a) java.util.Date
b) java.sql.Date
c) java.time
d) java.date
Answer:
c) java.time
Explanation:
The java.time package introduced in Java 8 provides a new date and time API that is more comprehensive and
user-friendly compared to the older java.util.Date and java.sql.Date classes.
Answer:
Explanation:
The map() method in the Stream API transforms each element in the stream using a given function, producing
a new stream of transformed elements.
a) allMatch()
b) anyMatch()
c) noneMatch()
d) matchesAll()
Answer:
a) allMatch()
Explanation:
The allMatch() method is used to check if all elements in a stream match the given predicate.
Answer:
Explanation:
Streams in Java cannot be reused once they are consumed. After a terminal operation is performed, the
stream is considered closed and cannot be used again.
a) filter()
b) distinct()
c) unique()
d) removeDuplicates()
Answer:
b) distinct()
Explanation:
The distinct() method in the Stream API is used to remove duplicate elements from a stream, ensuring that
each element appears only once.
a) sort()
b) order()
c) sorted()
d) arrange()
Answer:
c) sorted()
Explanation:
The sorted() method in the Stream API is used to sort the elements in a stream either in natural order or using
a custom comparator.
a) map()
b) reduce()
c) combine()
d) collect()
Answer:
b) reduce()
Explanation:
The reduce() method in the Stream API is used to combine elements of a stream into a single result using an
accumulator function.
Answer:
Explanation:
The findFirst() method in the Stream API finds the first element in a stream and returns it wrapped in an
Optional if it exists.
a) size()
b) count()
c) getCount()
d) length()
Answer:
b) count()
Explanation:
The count() method in the Stream API returns the number of elements in the stream.
30. Can you perform parallel processing with the Stream API in Java?
Explanation:
You can perform parallel processing with the Stream API in Java using the parallelStream() method, which
processes elements concurrently on multiple cores.
d) There is no difference
Answer:
Explanation:
findFirst() returns the first element in a stream, while findAny() returns any element in the stream, potentially
more efficient in parallel streams.
a) drop()
b) skip()
c) omit()
d) jump()
b) skip()
Explanation:
The skip() method in the Stream API skips the first n elements and returns a stream containing the remaining
elements.
a) Function
b) Supplier
c) Consumer
d) Predicate
Answer:
b) Supplier
Explanation:
The Supplier interface was introduced in Java 8 to represent a function that takes no arguments and returns a
result.
Answer:
a) Yes, lambda expressions can be used with Runnable
Explanation:
Lambda expressions can be used with the Runnable interface to simplify the syntax of creating threads or
tasks in Java.
b) To handle exceptions
Answer:
Explanation:
The Stream API in Java is designed to process collections of data in a functional style, enabling operations like
filtering, mapping, and reducing.
36. What is the purpose of the peek() method in the Stream API?
Answer:
The peek() method is used to view elements of a stream without modifying them. It is often used for
debugging purposes.
a) limit()
b) reduce()
c) cap()
d) filter()
Answer:
a) limit()
Explanation:
The limit() method is used to limit the number of elements in a stream to the specified size.
Answer:
Explanation:
The parallelStream() method processes elements in parallel, potentially improving performance on multi-core
processors.
a) findFirst()
b) firstElement()
c) getFirst()
d) find()
Answer:
a) findFirst()
Explanation:
The findFirst() method is used to find the first element in a stream and returns an Optional containing the
element if it exists.
Answer:
Explanation:
Lambda expressions can be used to create threads in Java by passing them to the Runnable interface, which is
then passed to a Thread.
https://www.youtube.com/@codewitharrays
https://www.instagram.com/codewitharrays/
https://codewitharrays.in/project