Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Streams & Lambda Practice

Lambda expressions and the Stream API (filter, map, reduce, collect).

1
Lambda - Square
Easy
Read an integer N. Use a lambda for a functional interface MathOp { int apply(int x); } that returns x*x, and print op.apply(N).
A single integer N.
N*N.
Sample Input 1
9
Sample Output 1
81
1
MathOp op = x -> x * x;
1
Filter Even Numbers
Medium
Read N and N integers into a List. Use stream().filter(x -> x % 2 == 0) and print the evens space-separated (or "None").
First line: N. Second: N ints.
Evens on one line, or "None".
Sample Input 1
6
1 2 3 4 5 6
Sample Output 1
2 4 6
1
Collect filtered results.
2
Map to Uppercase
Medium
Read N and N strings. Use stream().map(String::toUpperCase) and print each on its own line.
First line: N. Next N lines: strings.
N lines, uppercased.
Sample Input 1
2
hello
java
Sample Output 1
HELLO
JAVA
1
map with a method reference.
3
Sum with Streams
Medium
Read N and N integers. Use stream().mapToInt(Integer::intValue).sum() and print the total.
First line: N. Second: N ints.
The sum.
Sample Input 1
4
10 20 30 40
Sample Output 1
100
1
mapToInt then sum.
4
Sort Strings by Length
Medium
Read N and N strings. Sort them by length ascending with a lambda comparator, and print them one per line. If two lengths tie, keep original order.
First line: N. Next N lines: strings.
N lines sorted by length.
Sample Input 1
3
bb
a
ccc
Sample Output 1
a
bb
ccc
1
list.sort((a, b) -> a.length() - b.length()).
1
Collect to Uppercase Set
Hard
Read N and N words (may repeat). Use stream().map(toUpperCase).collect(toCollection(LinkedHashSet::new)) and print the unique uppercased words in first-seen order.
First line: N. Next N lines: words.
Unique uppercase words in order.
Sample Input 1
4
apple
Banana
APPLE
cherry
Sample Output 1
APPLE
BANANA
CHERRY
1
LinkedHashSet keeps insertion order.
2
Max with Streams
Hard
Read N and N integers. Use stream().max(Integer::compare) and print the maximum (or "Empty" if N == 0).
First line: N. Second: N ints.
The maximum, or "Empty".
Sample Input 1
5
3 7 2 9 4
Sample Output 1
9
Sample Input 2
0
Sample Output 2
Empty
0
max returns an Optional.
3
Average Using Streams
Hard
Read N and N integers. Use streams to print the integer average (floor).
First line: N. Second: N ints.
The floor average.
Sample Input 1
4
1 2 3 4
Sample Output 1
2
1
average() returns OptionalDouble.
Competitive MCQs — Java Streams & Lambda
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 33
Q1
What is a lambda expression?
Correct!
Wrong — correct answer is .
Lambdas implement functional interfaces concisely.
Q2
Which interface can a lambda implement?
Correct!
Wrong — correct answer is .
Lambdas target functional interfaces like Runnable or Comparator.
Q3
What is the output of this code?
java
1
2
3
List<Integer> n = Arrays.asList(1, 2, 3, 4);
long c = n.stream().filter(x -> x % 2 == 0).count();
System.out.println(c);
Correct!
Wrong — correct answer is .
Only 2 and 4 are even → count is 2.
Q4
What is the output of this code?
List n = Arrays.asList(1, 2, 3, 4);
long c = n.stream().filter(x -> x % 2 == 0).count();
System.out.println(c);
Correct!
Wrong — correct answer is .
Only 2 and 4 are even → count is 2.
Q5
Which stream operation transforms each element?
Correct!
Wrong — correct answer is .
map applies a function to each element, producing new values.
Q6
What is the output of this code?
java
1
2
int s = Arrays.asList(1, 2, 3, 4).stream().mapToInt(Integer::intValue).sum();
System.out.println(s);
Correct!
Wrong — correct answer is .
Sum of 1+2+3+4 = 10.
Q7
What is the output of this code?
int s = Arrays.asList(1, 2, 3, 4).stream().mapToInt(Integer::intValue).sum();
System.out.println(s);
Correct!
Wrong — correct answer is .
Sum of 1+2+3+4 = 10.
Q8
Which terminal operation is lazy?
Correct!
Wrong — correct answer is .
Intermediate ops are lazy; terminal ops (collect, count) run them.
Q9
What is the output of this code?
java
1
2
List<String> s = Arrays.asList("a", "bb", "ccc");
s.stream().map(String::length).forEach(System.out::print);
Correct!
Wrong — correct answer is .
map to length gives 1, 2, 3 → "123".
Q10
What is the output of this code?
List s = Arrays.asList("a", "bb", "ccc");
s.stream().map(String::length).forEach(System.out::print);
Correct!
Wrong — correct answer is .
map to length gives 1, 2, 3 → "123".
Q11
Which code sorts a list of strings by length with a lambda?
Correct!
Wrong — correct answer is .
The lambda provides the length-based comparator.
Q12
What is a method reference?
Correct!
Wrong — correct answer is .
String::length is shorthand for s -> s.length().
Q13
What is the output of this code?
java
1
2
boolean any = Arrays.asList(3, 5, 8).stream().anyMatch(x -> x > 6);
System.out.println(any);
Correct!
Wrong — correct answer is .
8 is greater than 6, so anyMatch returns true.
Q14
What is the output of this code?
boolean any = Arrays.asList(3, 5, 8).stream().anyMatch(x -> x > 6);
System.out.println(any);
Correct!
Wrong — correct answer is .
8 is greater than 6, so anyMatch returns true.
Q15
What is the output of this code?
long c = Arrays.asList("a", "bb", "c").stream().filter(s -> s.length() == 1).count();
System.out.println(c);
Correct!
Wrong — correct answer is .
Only "a" and "c" have length 1, so the count is 2.
Q16
Which stream method transforms each element?
Correct!
Wrong — correct answer is .
map applies a function and emits new values.
Q17
Which stream method keeps only matching elements?
Correct!
Wrong — correct answer is .
filter retains elements that satisfy the predicate.
Q18
What is the output of this code?
List n = Arrays.asList(1, 2, 3, 4);
int s = n.stream().mapToInt(Integer::intValue).sum();
System.out.println(s);
Correct!
Wrong — correct answer is .
Summing 1+2+3+4 gives 10.
Q19
Which operation is a terminal operation?
Correct!
Wrong — correct answer is .
Terminal operations like collect() trigger the pipeline.
Q20
What is the output of this code?
List s = Arrays.asList("x", "yy", "zzz");
s.stream().map(String::length).forEach(System.out::print);
Correct!
Wrong — correct answer is .
Lengths 1, 2, 3 print as "123".
Q21
Which code sorts strings by length?
Correct!
Wrong — correct answer is .
The comparator uses length differences.
Q22
What is the output of this code?
Optional o = Arrays.asList("a", "bb").stream().filter(s -> s.length() > 1).findFirst();
System.out.println(o.orElse("none"));
Correct!
Wrong — correct answer is .
The only string longer than 1 is "bb".
Q23
Which creates a stream from a list?
Correct!
Wrong — correct answer is .
Collection.stream() creates a sequential stream.
Q24
What is the output of this code?
List n = Arrays.asList(5, 1, 4);
List s = n.stream().sorted().collect(Collectors.toList());
System.out.println(s);
Correct!
Wrong — correct answer is .
sorted() orders the elements ascending.
Q25
What does distinct() do?
Correct!
Wrong — correct answer is .
distinct() keeps only unique values in the stream.
Q26
What is the output of this code?
long d = Arrays.asList(1, 2, 2, 3).stream().distinct().count();
System.out.println(d);
Correct!
Wrong — correct answer is .
Distinct values are 1, 2, 3 — three of them.
Q27
Which is a lazy intermediate operation?
Correct!
Wrong — correct answer is .
Intermediate operations are lazy until a terminal runs.
Q28
What is the output of this code?
List up = Arrays.asList("a", "b").stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(up);
Correct!
Wrong — correct answer is .
Each string is uppercased to A and B.
Q29
What does reduce() do?
Correct!
Wrong — correct answer is .
reduce() folds the stream using an accumulator.
Q30
What is the output of this code?
int r = Arrays.asList(1, 2, 3, 4).stream().reduce(0, (a, b) -> a + b);
System.out.println(r);
Correct!
Wrong — correct answer is .
The accumulator sums to 10.
Q31
Which lambda correctly doubles an int?
Correct!
Wrong — correct answer is .
The arrow body x * 2 returns the doubled value.
Q32
What is the output of this code?
boolean all = Arrays.asList(2, 4, 6).stream().allMatch(x -> x % 2 == 0);
System.out.println(all);
Correct!
Wrong — correct answer is .
Every element is even, so allMatch returns true.
Q33
Which statement about streams is TRUE?
Correct!
Wrong — correct answer is .
A stream is a pipeline view; the source is not stored or changed.