[Go to site: main page, start]

0% found this document useful (0 votes)
22 views28 pages

Java 8 Coding Interview Questions

The document provides a collection of Java 8 interview coding questions along with sample answers to assist candidates in their preparation. It covers various topics such as separating odd and even numbers, removing duplicates, finding character frequency, and merging arrays. Each question includes code snippets and expected outputs to illustrate the solutions effectively.

Uploaded by

pjiyykkuoyghj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views28 pages

Java 8 Coding Interview Questions

The document provides a collection of Java 8 interview coding questions along with sample answers to assist candidates in their preparation. It covers various topics such as separating odd and even numbers, removing duplicates, finding character frequency, and merging arrays. Each question includes code snippets and expected outputs to illustrate the solutions effectively.

Uploaded by

pjiyykkuoyghj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

6/27/23, 11:34 Java 8 Interview Sample Coding

PM Questions

Java Concept Of The Day


([Link]

Java 8 Interview Sample


Coding Questions
(HTTPS://[Link]/AUTHOR/ PRAMODBABLAD
PRAMODBABLA
(HTTPS://[Link]/AUTHOR/PRAMODBABLAD
/) /
JUNE 26, 2023 /
JAVA 8 (HTTPS://[Link]/CATEGORY/JAVA-8/), JAVA INTERVIEW
QUESTIONS (HTTPS://[Link]/CATEGORY/JAVA-INTERVIEW-
QUESTIONS/)

Here are the some Java 8 interview sample coding questions with answers. I
hope it will be helpful for you guys while preparing for an interview.

Works Where You Write

[Link] 1/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

([Link]
content/uploads/2023/06/Java_8_Interview_Sample_Coding_Questions.pn
g?ssl=1)

Java 8 Interview Coding Questions And Answers :


1) Given a list of integers, separate odd and even numbers?

[Link] 2/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 import
[Link]; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<Integer> listOfIntegers = [Link](71, 18, 42, 21,
6
13
14 Map<Boolean, List<Integer>> oddEvenNumbersMap =
15 [Link]().collect([Link]
ionin 16
17 Set<Entry<Boolean, List<Integer>>> entrySet =
oddEvenNumbersMa
18
19 for (Entry<Boolean, List<Integer>> entry :
entrySet) 20 {
21 [Link]("-------------------");
22
23 if ([Link]())
24 {
25 [Link]("Even
Numbers"); 26 }
27 else
28 {
29 [Link]("Odd
Numbers"); 30 }
31
32 [Link]("-------------------");
33
34 List<Integer> list =
[Link](); 35
36 for (int i : list)
37 {
38
[Link](i);
39 }
40 }
41 }
42 }

Output :

————–
Odd Numbers
————–
71
21
67
95
87
————–
Even Numbers
————–
18

[Link] 3/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 42 Questions

32

[Link] 4/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
14
56
2) How do you remove duplicate elements from a list using Java 8 streams?

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Java",
"Python", " 10
11 List<String> uniqueStrngs =
[Link]().distinct(). 12
13
[Link](uniqueStrngs);
14 }
15 }

Output :

Watch bananas turn into chips

[Java, Python, C#, Kotlin]

3) How do you find frequency of each character in a string using Java 8 streams?

[Link] 5/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 String inputString = "Java Concept Of The
Day"; 10
11 Map<Character, Long> charCountMap =
12 [Link]()
13 .mapToObj(c -> (char) c)
14 .collect([Link](Func
tio 15
16
[Link](charCountMap);
17 }
18 }

Output :

{ =4, a=3, c=1, C=1, D=1, e=2, f=1, h=1, J=1, n=1, O=1, o=1, p=1, T=1, t=1, v=1,
y=1}

4) How do you find frequency of each element in an array or a list?

1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4 impor [Link];
t
5 impor [Link];
t
6
7 publi class Java8Code
c
8 {
9 public static void main(String[] args)
10 {
11 List<String> stationeryList = [Link]("Pen", "
"Eraser",
12
13 Map<String, Long> stationeryCountMap =
14 [Link]().collect([Link]
ngBy(
15
16 [Link](stationeryCountMap);
17 }
18 }

Output :

{Pen=2, Stapler=1, Pencil=2, Note Book=2, Eraser=1}

5) How do you sort the given list of decimals in reverse order?

[Link] 6/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Double> decimalList = [Link](12.45, 23.58,
17.13, 10
11

[Link]().sorted([Link]()).forEach 12
}
13 }

Output :

71.85
56.98
42.89
33.78
23.58
21.12
17.13
12.45

6) Given a list of strings, join the strings with ‘[‘ as prefix, ‘]’ as suffix and ‘,’ as delimiter?

[Link] 7/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Facebook",
"Twitte 10
11 String joinedString =
[Link]().collect(Collector 12
13
[Link](joinedString);
14 }
15 }

Output :

[Facebook, Twitter, YouTube, WhatsApp, LinkedIn]

7) From the given list of integers, print the numbers which are multiples of 5?

1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 9
10 [Link]().filter(i -> i % 5 ==
0).forEach(System
11 }
12 }

Output :

45
15
75

8) Given a list of integers, find maximum and minimum of those numbers?

[Link] 8/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 10
11 int max =
[Link]().max([Link]( 12
13 [Link]("Maximum Element :
"+max); 14
15 int min =
[Link]().min([Link](
16
17 [Link]("Minimum Element :
"+min); 18 }
19 }

Output :

Maximum Element :
89 Minimum
Element : 12

9) How do you merge two unsorted arrays into single sorted array using Java 8
streams?

1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 int[] a = new int[] {4, 2, 7, 1};
9
10 int[] b = new int[] {8, 3, 9, 5};
11
12 int[] c = [Link]([Link](a),
[Link](b)) 13
14
[Link]([Link](c));
15 }
16 }

Output :

[1, 2, 3, 4, 5, 7, 8, 9]

10) How do you merge two unsorted arrays into single sorted array without duplicates?

[Link] 9/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 int[] a = new int[] {4, 2, 5, 1};
9
10 int[] b = new int[] {8, 1, 9, 5};
11
12 int[] c = [Link]([Link](a),
[Link](b)) 13
14
[Link]([Link](c));
15 }
16 }

Output :

[1, 2, 4, 5, 8, 9]

11) How do you get three maximum numbers and three minimum numbers from the
given list of integers?

1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4
5 publi class Java8Code
c
6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 2
56, 15,
10
11 //3 minimum Numbers
12
13 [Link]("------------------------");
14
15 [Link]("Minimum 3
Numbers"); 16
17 [Link]("------------------------");
18
19

[Link]().sorted().limit(3).forEach([Link]:: 20
21 //3 Maximum
Numbers 22
23 [Link]("------------------------");
24
25 [Link]("Maximum 3
Numbers"); 26
27 [Link]("------------------------");
28
29

[Link]().sorted([Link]()).limit(3).fo

[Link] 10
6/27/23, 11:34 Java 8 Interview Sample Coding
PM r 30 } Questions
31 }

Output :

[Link] 11
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
—————–
Minimum 3 Numbers
—————–
12
15
24
—————–
Maximum 3 Numbers
—————–
89
75
56
12) Java 8 program to check if two strings are anagrams or not?

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 String s1 = "RaceCar";
9 String s2 =
"CarRace"; 10
11 s1 =
[Link]([Link]("")).map(String::toUpperCase).sorted() 12
13 s2 =
[Link]([Link]("")).map(String::toUpperCase).sorted() 14
15 if ([Link](s2))
16 {
17 [Link]("Two strings are
anagrams"); 18 }
19 else
20 {
21 [Link]("Two strings are not
anagrams"); 22 }
23 }
24 }

Output :

Two strings are anagrams

13) Find sum of all digits of a number in Java 8?

[Link] 12
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 int i = 15623;
9
10 Integer sumOfDigits =
[Link]([Link](i).split("")).c 11
12 [Link](sumOfDigits);
13 }
14 }

Output :

17

14) Find second largest number in an integer array?

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[]
args) 8 {
9 List<Integer> listOfIntegers = [Link](45, 12, 56,
15, 2 10
11 Integer secondLargestNumber =
[Link]().sorted(C
12
13
[Link](secondLargestNumber);
14 }
15 }

Output :

75

15) Given a list of strings, sort them according to increasing order of their length?

[Link] 13
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import
[Link]; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = [Link]("Java",
"Python", " 10
11

[Link]().sorted([Link](String::len 12
}
13 }

Output :

C
C#
C+
+
Java
HTML
COBOL
Pytho
n
Kotlin

16) Given an integer array, find sum and average of all elements?

1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 int[] a = new int[] {45, 12, 56, 15, 24, 75, 31, 89};
8
9 int sum =
[Link](a).sum(); 10
11 [Link]("Sum =
"+sum); 12
13 double average =
[Link](a).average().getAsDouble(); 14
15 [Link]("Average =
"+average); 16 }
17 }

Output :

Sum = 347
Average = 43.375

17) How do you find common elements between two arrays?

[Link] 14
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> list1 = [Link](71, 21, 34, 89,
56, 28); 9
10 List<Integer> list2 = [Link](12, 56, 17, 21,
94, 34); 11
12

[Link]().filter(list2::contains).forEach([Link]::pri
13 }
14 }

Output :

21
34
56

18) Reverse each word of a string using Java 8 streams?

1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 String str = "Java Concept Of The
Day"; 9
10 String reversedStr = [Link]([Link](" "))
11 .map(word -> new StringBuffer(word
12 .collect([Link](" "));
13
14
[Link](reversedStr);
15 }
16 }

Output :

avaJ tpecnoC fO ehT yaD

19) How do you find sum of first 10 natural numbers?

[Link] 15
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import
[Link];
2
3 public class Java8Code
4 {
5 public static void main(String[] args)
6 {
7 int sum = [Link](1, 11).sum()
;
8
9 [Link](sum);
10 }
11 }

Output :

55

20) Reverse an integer array

1 impor [Link];
t
2 impor [Link]
t m;
3
4 publi class Java8Code
c
5 {
6 public static void main(String[] args)
7 {
8 int[] array = new int[] {5, 1, 7, 3, 9, 6};
9
10 int[] reversedArray = [Link](1,
11
12 [Link]).m
13
14 [Link]([Link](reversedArray));
} }

Output :

[6, 9, 3, 7, 1, 5]

21) Print first 10 even numbers

1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[] args)
6 {
7 [Link](1, 10).map(i -> i *
2).forEach([Link] 8 }
9 }

Output :

[Link] 16
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

2
4
6

[Link] 17
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
8
10
12
14
16
18
20

22) How do you find the most repeated element in an array?

1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import [Link];
6 import
[Link]; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<String> listOfStrings = [Link]("Pen", "Eraser",
"N
13
14 Map<String, Long> elementCountMap = [Link]()
15 .collect(Coll
16
17 Entry<String, Long> mostFrequentElement =
[Link] 18
19 [Link]("Most Frequent Element :
"+mostFrequentElem 20
21 [Link]("Count :
"+[Link]()); 22 }
23 }

Output :

Most Frequent Element :


Pen Count : 3

23) Palindrome program using Java 8 streams

[Link] 18
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 String str = "ROTATOR";
8
9 boolean isItPalindrome = [Link](0, [Link]()/2).
10 noneMatch(i -> [Link](i) !=
[Link]([Link]( 11
12 if (isItPalindrome)
13 {
14 [Link](str+" is a
palindrome"); 15 }
16 else
17 {
18 [Link](str+" is not a
palindrome"); 19 }
20 }
21 }

Output :

ROTATOR is a palindrome

24) Given a list of strings, find out those strings which start with a number?

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = [Link]("One", "2wo",
"3hre 9
10 [Link]().filter(str ->
[Link]([Link] 11 }
12 }

Output :

2wo
3hre
e
5ive

25) How do you extract duplicate elements from an array?

[Link] 19
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import
[Link]; 6
7 public class Java8Code
8 {
9 public static void main(String[]
args) 10 {
11 List<Integer> listOfIntegers = [Link](111, 222,
333, 11 12
13 Set<Integer> uniqueElements = new
HashSet<>(); 14
15 Set<Integer> duplicateElements =
[Link]().filte
16
17
[Link](duplicateElements);
18 }
19 }

Output :

[333, 222, 111]

26) Print duplicate characters in a string?

1 import [Link];
2 import [Link];
3 import [Link];
4 import
[Link]; 5
6 public class Java8Code
7 {
8 public static void main(String[]
args) 9 {
10 String inputString = "Java Concept Of The
Day".replaceAll("\\s 11
12 Set<String> uniqueChars = new
HashSet<>(); 13
14 Set<String> duplicateChars =
15 [Link]([Link](""))
16 .filter(ch -> ! [Link](ch))
17 .collect([Link](
)); 18
19 [Link](duplicateChars);
20 }
21 }

Output :

[a, c, t, e, o]

27) Find first repeated character in a string?

[Link] 20
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 impor [Link];
t
2 impor [Link];
t
3 impor [Link];
t
4 impor [Link]
t on;
5 impor [Link]
t rs;
6
7 publi class Java8Code
c
8 {
9 public static void main(String[] args)
10 {
11 String inputString = "Java Concept Of The
12 Day".replaceAll("\\s
13
14 Map<String, Long> charCountMap =
15 [Link]([Link](""))
16 .collect([Link](Fun
17
18 String firstRepeatedChar = [Link]()
19 .stream()
20 .filter(entry -> entry
21 .map(entry -> [Link]
22 .findFirst()
23 .get();
24
25 [Link](firstRepeatedChar);
26 }
}

Output :

28) Find first non-repeated character in a string?

1 import [Link];
2 import [Link];
3 import [Link];
4 import [Link];
5 import
[Link]; 6
7 public class
Java8Code 8 {
9 public static void main(String[] args)
10 {
11 String inputString = "Java Concept Of The
Day".replaceAll("\\s 12
13 Map<String, Long> charCountMap =
14 [Link]([Link](""))
15 .collect([Link](Fun
16
17 String firstNonRepeatedChar = [Link]()
18 .stream()
19 .filter(entry -> entry
20 .map(entry -> [Link]
21 .findFirst()
22 .get();
23
[Link] 21
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 24 Questions
[Link](firstNonRepeatedChar);
25 }
26 }

[Link] 22
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Output :

29) Fibonacci series

1 import
[Link]; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 [Link](new int[] {0, 1}, f -> new int[] {f[1],
f[0]+f[
8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> [Link](i+"
")); 11 }
12 }

Output :

0 1 1 2 3 5 8 13 21 34

30) First 10 odd numbers

1 import
[Link]; 2
3 public class Java8Code
4 {
5 public static void main(String[]
args) 6 {
7 [Link](new int[] {1, 3}, f -> new int[] {f[1],
f[1]+2} 8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> [Link](i+"
")); 11 }
12 }

Output :

1 3 5 7 9 11 13 15 17 19

31) How do you get last element of an array?

[Link] 23
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

1 import [Link];
2 import
[Link]; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = [Link]("One", "Two",
"Thre 9
10 String lastElement =
[Link]().skip(listOfStrings 11
12 [Link](lastElement);
13 }
14 }

Output :

Six

32) Find the age of a person in years if the birthday has given?

1 import [Link];
2 import
[Link]; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 LocalDate birthDay = [Link](1985, 01, 23);
9 LocalDate today =
[Link](); 10
11 [Link]([Link](birthDay,
today));
12 }
13 }

Also Read :

Solving real time queries using Java 8 streams


([Link] real-time-queries-using-java-8-
features-employee-management-system/)
Java 8 theoretical interview questions ([Link]
8-interview- questions-and-answers/)
Java 8 Oracle Docs ([Link]

Watch Yo uTube
Get inspired by fun content and
Shorts
new creators,
songs, more
YouTube Shorts
[Link] 24
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
([Link]
PREVIOUS
strings-cheat-sheet/)

Related Posts

Java Array Interview Questions And Answers


([Link]
and-answers/)
April 23, 2023 /
11 Comments ([Link]
questions-and- answers/#comments)

30+ Java Exception Handling Interview Questions And Answers


([Link]
questions- and-answers/)
April 14, 2023 /
11 Comments ([Link]
questions-and- answers/#comments)

Top 70 Java Collections Interview Questions With Answers


([Link]
questions-with- answers/)
June 24, 2022

Leave a Reply
Name *

Email *

Website

[Link] 25
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Add Comment

Notify me of follow-up comments by email.

Notify me of new posts by email.

Post Comment

Tutorials :

Java 11 ([Link]
Java 10 ([Link]
Java 9 ([Link]
Java 8 ([Link]
Java Collections ([Link]
hierarchy/) Java Threads ([Link]
java-tutorial/)
Exception Handling ([Link]
handling/) Java Generics
([Link]
Java Strings
([Link]
Java Arrays
([Link]
JDBC ([Link]

Quiz :

Java Strings Quiz ([Link]


questions-and- answers/)
[Link] 26
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
++ And -- Quiz ([Link]
decrement- operators/)
Java Arrays Quiz ([Link]
sample-coding- questions-on-arrays/)
Java Enums Quiz ([Link]
questions-on- enum-types/)
Nested Classes Quiz ([Link]
questions- on-nested-classes/)
Java Modifiers Quiz ([Link]
questions-on- access-modifiers/)
Java Interfaces Quiz ([Link]
questions- on-interfaces/)
Java Abstract Classes Quiz ([Link]
practice-coding- questions-on-abstract-classes/)
Java Polymorphism Quiz ([Link]
questions-on- method-overloading-and-overriding/)
Java Inheritance Quiz ([Link]
inheritance-practice- coding-questions/)
Java Classes & Objects Quiz ([Link]
questions- on-classes-and-objects/)

Interview Q&A :

Java 8 Interview Questions ([Link]


interview-questions- and-answers/)
Java Threads Interview Questions
([Link] interview-questions-
and-answers/)
Exceptions Handling Interview Questions
([Link] exception-handling-interview-
questions-and-answers/)
Java Strings Interview Questions ([Link]
string-interview- questions-and-answers/)
Java Arrays Interview Questions ([Link]
array-interview- questions-and-answers/)
300+ Core Java Interview Questions
([Link] questions-and-answers/)

[Link] 27
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions

Copyright © 2023 [Link] | Privacy


Policy ([Link]
policy/) | About Us
([Link] | Contact
Us ([Link]

[Link] 28

Common questions

Powered by AI

In Java 8, streams can be used to partition a list of integers into odd and even numbers using the Collectors.partitioningBy method. By streaming the list and applying the partitioning logic with a modulus check inside a Collectors.partitioningBy, you can separate the numbers. For example, if you have a List<Integer> listOfIntegers, you can separate them by: Map<Boolean, List<Integer>> oddEvenMap = listOfIntegers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0)); This separates even numbers into one list and odd numbers into another .

You might also like