public static void main(String[] args) throws IOException { List strs = Arrays.asList("Hadoop and Spark", "Hadoop and HBase", "Hadoop and AWS", "Hadoop "); Predicate predicate = s -> s.length() > 4; Predicate cf2 = new CourseFilter() { // line n1 public boolean test(String s) { return s.contains("Hadoop "); } };
long c = strs.stream().filter(predicate).filter(cf2)// line n2 .count(); System.out.println(c); } }
1. It will fail to compile
2. It will run and print 3
3. It will run and print 4
4. It will run and print 0
Correct Answer : Get Lastest Questions and Answer : Explanation: There is no issue with the given code. It will compile and run as expected. There are two predicates, which will be applied on each element of the list. First predicate will check whether each String in list has length greater than 4. At the same time second predicate will check whether string contains "Hadoop" keyword or not. If it contains Hadoop keyword then it will be considered for count. In given code all the 4 string contains "Hadoop" word so it will print 4 as an output.
Question : You have been given following code, what will be the behavior?
package com.hadoopexam;
import java.util.stream.DoubleStream;
public class Welcome { public static void main(String[] args) { DoubleStream nums = DoubleStream.of(1.0, 2.0, 3.0).map(i -> -i); // n1 System.out.printf("Total element count = %d, Sum of all elements = %f", nums.count(), nums.sum()); } } 1. Code will compile and run successful with output as Total element count = 3, Sum of all elements = 6
2. Code will compile and run successful with output as Total element count = 3, Sum of all elements =
3. Code will give compile time exception as you can not use same stream twice.
4. Code will compile, but will throw a RuntimeException "java.lang.IllegalStateException: stream has already been operated upon or closed"
Correct Answer : Get Lastest Questions and Answer : Explanation: Both count() and sum() are terminal operation. When you call termination operation on a stream. Stream will be consumed and you can not call next termination operation. It will throw a runtime exception.
Question : You have been given following code, what would be the behavior of it.
package com.hadoopexam;
class Welcome { private static boolean checkChars(int c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; } return false; }
public static void main(String[] args) { "hadoopexam.com".chars().filter(Welcome::checkChars) .forEach(ch -> System.out.printf("%c", ch)); } } 1. It will compile and run successfully and print nothing
2. It will compile and run successfully and print hdpxm.cm
3. It will compile and run successfully and print aooeao
4. It will give compile time error. Because, you don't have break and default statement in switch.
Correct Answer : Get Lastest Questions and Answer : Explanation: As you can see, we can have switch statement without break and default statement. So there will be no compile time exception. Because each case will be checked against the each character, if match true will be returned else it will return false. For each vowel it will return true. And in filter function we have used the method reference, where it will use each vowel it will get true hence it will be passed to forEach and printed. But this is not applied on non-vowel characters.