Question : You have been given below code. What would be the behavior.
package com.hadoopexam;
import java.util.Arrays;
class Welcome { public static void main(String[] args) { "HadoopExam.com".chars().distinct() .peek(ch -> System.out.printf("%c ", ch)).sorted(); } } 1. It will print "H a d o p E x m . c"
2. It will print "H a d o o p E x a m . c o m"
3. Code will not compile.
4. Code will not print any output.
Correct Answer : 4 Explanation: As you know, stream is lazily evaluated and to call the intermediate functions of the stream it requires that you call termiate function like count(). If you change the statement like "HadoopExam.com".chars().distinct() .peek(ch -> System.out.printf("%c ", ch)).sorted().count();
Output will be "H a d o p E x m . c"
Question : You have been given following code, what is the behavior you expect.
package com.hadoopexam;
import java.util.stream.IntStream;
class Welcome { public static void main(String[] args) { IntStream.rangeClosed(5, 5).forEach(System.out::println); } }
1. It will compile and run perfectly and print 5,6
2. It will compile and run perfectly and print 5,5
3. It will compile and run perfectly and print 5
4. It will compile and run perfectly but not print anything
5. It will compile and run perfectly but throw exception at runtime. NoSufficientArgumentsProvided.
Correct Answer : 3 Explanation: When you call a menthod rangeClosed() on intStream, it will consider 2nd argument as inclusive. Hence, 5 will be included. So it will print 5 as an output. Also forEach is a stream termination function. Hence, it will be called.
Question : You have been given below source code and Message.properties file
public static void main(String[] args) throws IOException { Properties prop = new Properties (); FileInputStream fis = new FileInputStream("resources//Message.properties"); prop.load(fis); System.out.println(prop.getProperty("hadoopexam")); System.out.println(prop.getProperty("training", "Welcome to Training4Exam.com")); System.out.println(prop.getProperty("quickTechie")); System.out.println(prop.getProperty("quicktechie")); }
}
- resources\Message.properties hadoopexam=Welcome to HadoopExam Learning Resources quickTechie=Welcome to QuickTechie Professional Network
What is the behavior of the code? 1. It will give compile time error.
2. It will give "NullPointerException" when executed.
3. It will run perfectly and print as below. Welcome to HadoopExam Learning Resources Welcome to Training4Exam.com Welcome to QuickTechie Professional Network null
4. It will run perfectly and print as below. Welcome to HadoopExam Learning Resources Welcome to Training4Exam.com Welcome to QuickTechie Professional Network
Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns the default value argument if the property is not found. Parameters: key the hashtable key. defaultValue a default value. Returns: the value in this property list with the specified key value.
String java.util.Properties.getProperty(String key) Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found. Parameters: key the property key. Returns: the value in this property list with the specified key value.