Java Interview Sample Coding Questions (1)
Java Interview Sample Coding Questions (1)
PM 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.
https://javaconceptoftheday.com/java-8-interview-sample- 1/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
(https://i0.wp.com/javaconceptoftheday.com/wp-
content/uploads/2023/06/Java_8_Interview_Sample_Coding_Questions.pn
g?ssl=1)
https://javaconceptoftheday.com/java-8-interview-sample- 2/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.List;
3 import java.util.Map;
4 import java.util.Map.Entry;
5 import java.util.Set;
6 import
java.util.stream.Collectors; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<Integer> listOfIntegers = Arrays.asList(71, 18, 42, 21,
6
13
14 Map<Boolean, List<Integer>> oddEvenNumbersMap =
15 listOfIntegers.stream().collect(Collectors.partit
ionin 16
17 Set<Entry<Boolean, List<Integer>>> entrySet =
oddEvenNumbersMa
18
19 for (Entry<Boolean, List<Integer>> entry :
entrySet) 20 {
21 System.out.println("-------------------");
22
23 if (entry.getKey())
24 {
25 System.out.println("Even
Numbers"); 26 }
27 else
28 {
29 System.out.println("Odd
Numbers"); 30 }
31
32 System.out.println("-------------------");
33
34 List<Integer> list =
entry.getValue(); 35
36 for (int i : list)
37 {
38
System.out.println(i);
39 }
40 }
41 }
42 }
Output :
————–
Odd Numbers
————–
71
21
67
95
87
————–
Even Numbers
————–
18
https://javaconceptoftheday.com/java-8-interview-sample- 3/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 42 Questions
32
https://javaconceptoftheday.com/java-8-interview-sample- 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 java.util.Arrays;
2 import java.util.List;
3 import
java.util.stream.Collectors; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = Arrays.asList("Java",
"Python", " 10
11 List<String> uniqueStrngs =
listOfStrings.stream().distinct(). 12
13
System.out.println(uniqueStrngs);
14 }
15 }
Output :
3) How do you find frequency of each character in a string using Java 8 streams?
https://javaconceptoftheday.com/java-8-interview-sample- 5/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Map;
2 import java.util.function.Function;
3 import
java.util.stream.Collectors; 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 inputString.chars()
13 .mapToObj(c -> (char) c)
14 .collect(Collectors.groupingBy(Func
tio 15
16
System.out.println(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}
1 impor java.util.Arrays;
t
2 impor java.util.List;
t
3 impor java.util.Map;
t
4 impor java.util.function.Function;
t
5 impor java.util.stream.Collectors;
t
6
7 publi class Java8Code
c
8 {
9 public static void main(String[] args)
10 {
11 List<String> stationeryList = Arrays.asList("Pen", "
"Eraser",
12
13 Map<String, Long> stationeryCountMap =
14 stationeryList.stream().collect(Collectors.groupi
ngBy(
15
16 System.out.println(stationeryCountMap);
17 }
18 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 6/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.Comparator;
3 import
java.util.List; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Double> decimalList = Arrays.asList(12.45, 23.58,
17.13, 10
11
decimalList.stream().sorted(Comparator.reverseOrder()).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?
https://javaconceptoftheday.com/java-8-interview-sample- 7/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.List;
3 import
java.util.stream.Collectors; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = Arrays.asList("Facebook",
"Twitte 10
11 String joinedString =
listOfStrings.stream().collect(Collector 12
13
System.out.println(joinedString);
14 }
15 }
Output :
7) From the given list of integers, print the numbers which are multiples of 5?
1 import java.util.Arrays;
2 import
java.util.List; 3
4 public class
Java8Code 5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> listOfIntegers = Arrays.asList(45, 12, 56,
15, 2 9
10 listOfIntegers.stream().filter(i -> i % 5 ==
0).forEach(System
11 }
12 }
Output :
45
15
75
https://javaconceptoftheday.com/java-8-interview-sample- 8/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.Comparator;
3 import
java.util.List; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = Arrays.asList(45, 12, 56,
15, 2 10
11 int max =
listOfIntegers.stream().max(Comparator.naturalOrder( 12
13 System.out.println("Maximum Element :
"+max); 14
15 int min =
listOfIntegers.stream().min(Comparator.naturalOrder(
16
17 System.out.println("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 java.util.Arrays;
2 import
java.util.stream.IntStream; 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 = IntStream.concat(Arrays.stream(a),
Arrays.stream(b)) 13
14
System.out.println(Arrays.toString(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?
https://javaconceptoftheday.com/java-8-interview-sample- 9/2
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import
java.util.stream.IntStream; 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 = IntStream.concat(Arrays.stream(a),
Arrays.stream(b)) 13
14
System.out.println(Arrays.toString(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 java.util.Arrays;
t
2 impor java.util.Comparator;
t
3 impor java.util.List;
t
4
5 publi class Java8Code
c
6 {
7 public static void main(String[] args)
8 {
9 List<Integer> listOfIntegers = Arrays.asList(45, 12, 2
56, 15,
10
11 //3 minimum Numbers
12
13 System.out.println("------------------------");
14
15 System.out.println("Minimum 3
Numbers"); 16
17 System.out.println("------------------------");
18
19
listOfIntegers.stream().sorted().limit(3).forEach(System.out:: 20
21 //3 Maximum
Numbers 22
23 System.out.println("------------------------");
24
25 System.out.println("Maximum 3
Numbers"); 26
27 System.out.println("------------------------");
28
29
listOfIntegers.stream().sorted(Comparator.reverseOrder()).limit(3).fo
https://javaconceptoftheday.com/java-8-interview-sample- 10
6/27/23, 11:34 Java 8 Interview Sample Coding
PM r 30 } Questions
31 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 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 java.util.stream.Collectors;
2 import
java.util.stream.Stream; 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 =
Stream.of(s1.split("")).map(String::toUpperCase).sorted() 12
13 s2 =
Stream.of(s2.split("")).map(String::toUpperCase).sorted() 14
15 if (s1.equals(s2))
16 {
17 System.out.println("Two strings are
anagrams"); 18 }
19 else
20 {
21 System.out.println("Two strings are not
anagrams"); 22 }
23 }
24 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 12
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.stream.Collectors;
2 import
java.util.stream.Stream; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 int i = 15623;
9
10 Integer sumOfDigits =
Stream.of(String.valueOf(i).split("")).c 11
12 System.out.println(sumOfDigits);
13 }
14 }
Output :
17
1 import java.util.Arrays;
2 import java.util.Comparator;
3 import
java.util.List; 4
5 public class
Java8Code 6 {
7 public static void main(String[]
args) 8 {
9 List<Integer> listOfIntegers = Arrays.asList(45, 12, 56,
15, 2 10
11 Integer secondLargestNumber =
listOfIntegers.stream().sorted(C
12
13
System.out.println(secondLargestNumber);
14 }
15 }
Output :
75
15) Given a list of strings, sort them according to increasing order of their length?
https://javaconceptoftheday.com/java-8-interview-sample- 13
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.Comparator;
3 import
java.util.List; 4
5 public class
Java8Code 6 {
7 public static void main(String[] args)
8 {
9 List<String> listOfStrings = Arrays.asList("Java",
"Python", " 10
11
listOfStrings.stream().sorted(Comparator.comparing(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
java.util.Arrays; 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 =
Arrays.stream(a).sum(); 10
11 System.out.println("Sum =
"+sum); 12
13 double average =
Arrays.stream(a).average().getAsDouble(); 14
15 System.out.println("Average =
"+average); 16 }
17 }
Output :
Sum = 347
Average = 43.375
https://javaconceptoftheday.com/java-8-interview-sample- 14
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import
java.util.List; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<Integer> list1 = Arrays.asList(71, 21, 34, 89,
56, 28); 9
10 List<Integer> list2 = Arrays.asList(12, 56, 17, 21,
94, 34); 11
12
list1.stream().filter(list2::contains).forEach(System.out::pri
13 }
14 }
Output :
21
34
56
1 import java.util.Arrays;
2 import
java.util.stream.Collectors; 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 = Arrays.stream(str.split(" "))
11 .map(word -> new StringBuffer(word
12 .collect(Collectors.joining(" "));
13
14
System.out.println(reversedStr);
15 }
16 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 15
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import
java.util.stream.IntStream;
2
3 public class Java8Code
4 {
5 public static void main(String[] args)
6 {
7 int sum = IntStream.range(1, 11).sum()
;
8
9 System.out.println(sum);
10 }
11 }
Output :
55
1 impor java.util.Arrays;
t
2 impor java.util.stream.IntStrea
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 = IntStream.rangeClosed(1,
11
12 array.length).m
13
14 System.out.println(Arrays.toString(reversedArray));
} }
Output :
[6, 9, 3, 7, 1, 5]
1 import
java.util.stream.IntStream; 2
3 public class
Java8Code 4 {
5 public static void main(String[] args)
6 {
7 IntStream.rangeClosed(1, 10).map(i -> i *
2).forEach(System.out 8 }
9 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 16
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
2
4
6
https://javaconceptoftheday.com/java-8-interview-sample- 17
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
8
10
12
14
16
18
20
1 import java.util.Arrays;
2 import java.util.List;
3 import java.util.Map;
4 import java.util.Map.Entry;
5 import java.util.function.Function;
6 import
java.util.stream.Collectors; 7
8 public class
Java8Code 9 {
10 public static void main(String[]
args) 11 {
12 List<String> listOfStrings = Arrays.asList("Pen", "Eraser",
"N
13
14 Map<String, Long> elementCountMap = listOfStrings.stream()
15 .collect(Coll
16
17 Entry<String, Long> mostFrequentElement =
elementCountMap.entr 18
19 System.out.println("Most Frequent Element :
"+mostFrequentElem 20
21 System.out.println("Count :
"+mostFrequentElement.getValue()); 22 }
23 }
Output :
https://javaconceptoftheday.com/java-8-interview-sample- 18
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import
java.util.stream.IntStream; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 String str = "ROTATOR";
8
9 boolean isItPalindrome = IntStream.range(0, str.length()/2).
10 noneMatch(i -> str.charAt(i) !=
str.charAt(str.length( 11
12 if (isItPalindrome)
13 {
14 System.out.println(str+" is a
palindrome"); 15 }
16 else
17 {
18 System.out.println(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 java.util.Arrays;
2 import
java.util.List; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = Arrays.asList("One", "2wo",
"3hre 9
10 listOfStrings.stream().filter(str ->
Character.isDigit(str.cha 11 }
12 }
Output :
2wo
3hre
e
5ive
https://javaconceptoftheday.com/java-8-interview-sample- 19
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import java.util.HashSet;
3 import java.util.List;
4 import java.util.Set;
5 import
java.util.stream.Collectors; 6
7 public class Java8Code
8 {
9 public static void main(String[]
args) 10 {
11 List<Integer> listOfIntegers = Arrays.asList(111, 222,
333, 11 12
13 Set<Integer> uniqueElements = new
HashSet<>(); 14
15 Set<Integer> duplicateElements =
listOfIntegers.stream().filte
16
17
System.out.println(duplicateElements);
18 }
19 }
Output :
1 import java.util.Arrays;
2 import java.util.HashSet;
3 import java.util.Set;
4 import
java.util.stream.Collectors; 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 Arrays.stream(inputString.split(""))
16 .filter(ch -> ! uniqueChars.add(ch))
17 .collect(Collectors.toSet(
)); 18
19 System.out.println(duplicateChars);
20 }
21 }
Output :
[a, c, t, e, o]
https://javaconceptoftheday.com/java-8-interview-sample- 20
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 impor java.util.Arrays;
t
2 impor java.util.LinkedHashMap;
t
3 impor java.util.Map;
t
4 impor java.util.function.Functi
t on;
5 impor java.util.stream.Collecto
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 Arrays.stream(inputString.split(""))
16 .collect(Collectors.groupingBy(Fun
17
18 String firstRepeatedChar = charCountMap.entrySet()
19 .stream()
20 .filter(entry -> entry
21 .map(entry -> entry.ge
22 .findFirst()
23 .get();
24
25 System.out.println(firstRepeatedChar);
26 }
}
Output :
1 import java.util.Arrays;
2 import java.util.LinkedHashMap;
3 import java.util.Map;
4 import java.util.function.Function;
5 import
java.util.stream.Collectors; 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 Arrays.stream(inputString.split(""))
15 .collect(Collectors.groupingBy(Fun
16
17 String firstNonRepeatedChar = charCountMap.entrySet()
18 .stream()
19 .filter(entry -> entry
20 .map(entry -> entry.ge
21 .findFirst()
22 .get();
23
https://javaconceptoftheday.com/java-8-interview-sample- 21
6/27/23, 11:34 Java 8 Interview Sample Coding
PM 24 Questions
System.out.println(firstNonRepeatedChar);
25 }
26 }
https://javaconceptoftheday.com/java-8-interview-sample- 22
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Output :
1 import
java.util.stream.Stream; 2
3 public class
Java8Code 4 {
5 public static void main(String[]
args) 6 {
7 Stream.iterate(new int[] {0, 1}, f -> new int[] {f[1],
f[0]+f[
8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> System.out.print(i+"
")); 11 }
12 }
Output :
0 1 1 2 3 5 8 13 21 34
1 import
java.util.stream.Stream; 2
3 public class Java8Code
4 {
5 public static void main(String[]
args) 6 {
7 Stream.iterate(new int[] {1, 3}, f -> new int[] {f[1],
f[1]+2} 8 .limit(10)
9 .map(f -> f[0])
10 .forEach(i -> System.out.print(i+"
")); 11 }
12 }
Output :
1 3 5 7 9 11 13 15 17 19
https://javaconceptoftheday.com/java-8-interview-sample- 23
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
1 import java.util.Arrays;
2 import
java.util.List; 3
4 public class Java8Code
5 {
6 public static void main(String[]
args) 7 {
8 List<String> listOfStrings = Arrays.asList("One", "Two",
"Thre 9
10 String lastElement =
listOfStrings.stream().skip(listOfStrings 11
12 System.out.println(lastElement);
13 }
14 }
Output :
Six
32) Find the age of a person in years if the birthday has given?
1 import java.time.LocalDate;
2 import
java.time.temporal.ChronoUnit; 3
4 public class
Java8Code 5 {
6 public static void main(String[] args)
7 {
8 LocalDate birthDay = LocalDate.of(1985, 01, 23);
9 LocalDate today =
LocalDate.now(); 10
11 System.out.println(ChronoUnit.YEARS.between(birthDay,
today));
12 }
13 }
Also Read :
Watch Yo uTube
Get inspired by fun content and
Shorts
new creators,
songs, more
YouTube Shorts
https://javaconceptoftheday.com/java-8-interview-sample- 24
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
(https://javaconceptoftheday.com/java-
PREVIOUS
strings-cheat-sheet/)
Related Posts
Leave a Reply
Name *
Email *
Website
https://javaconceptoftheday.com/java-8-interview-sample- 25
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
Add Comment
Post Comment
Tutorials :
Java 11 (https://javaconceptoftheday.com/category/java-11/)
Java 10 (https://javaconceptoftheday.com/category/java-10/)
Java 9 (https://javaconceptoftheday.com/category/java-9/)
Java 8 (https://javaconceptoftheday.com/category/java-8/)
Java Collections (https://javaconceptoftheday.com/collection-framework-class-
hierarchy/) Java Threads (https://javaconceptoftheday.com/category/threads-
java-tutorial/)
Exception Handling (https://javaconceptoftheday.com/category/exception-
handling/) Java Generics
(https://javaconceptoftheday.com/category/generics/)
Java Strings
(https://javaconceptoftheday.com/category/strings/)
Java Arrays
(https://javaconceptoftheday.com/category/arrays/)
JDBC (https://javaconceptoftheday.com/category/jdbc/)
Quiz :
Interview Q&A :
https://javaconceptoftheday.com/java-8-interview-sample- 27
6/27/23, 11:34 Java 8 Interview Sample Coding
PM Questions
https://javaconceptoftheday.com/java-8-interview-sample- 28