public class Welcome { public static void main(String[] args) throws IOException { Stream lines = Files.lines(Paths.get("C:\\Welcome.java")); //n1 } } Which of the following line will print first line from Welcome.java file 1. lines.limit(1).forEach(System.out::println);
Read all lines from a file as a Stream. Bytes from the file are decoded into characters using the UTF-8 charset. This method works as if invoking it were equivalent to evaluating the expression: Files.lines(path, StandardCharsets.UTF_8) The limit(1) method truncates the result to one line; and the forEach() method, when passed with the System.out::println method reference, prints that line to the console.
Question : You have been given below code, what is expected behavior?
4. It will run, and print one level file information.
Correct Answer : Get Lastest Questions and Answer : Explanation: : Stream java.nio.file.Files.find(Path start, int maxDepth, BiPredicate matcher, FileVisitOption... options) throws IOException
Return a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file. This method walks the file tree in exactly the manner specified by the walk method. For each file encountered, the given BiPredicate is invoked with its Path and BasicFileAttributes. The Path object is obtained as if by resolving the relative path against start and is only included in the returned Stream if the BiPredicate returns true. Compare to calling filter on the Stream returned by walk method, this method may be more efficient by avoiding redundant retrieval of the BasicFileAttributes. The returned stream encapsulates one or more DirectoryStreams. If timely disposal of file system resources is required, the try-with-resources construct should be used to ensure that the stream's close method is invoked after the stream operations are completed. Operating on a closed stream will result in an java.lang.IllegalStateException. If an IOException is thrown when accessing the directory after returned from this method, it is wrapped in an UncheckedIOException which will be thrown from the method that caused the access to take place. Parameters: start the starting file maxDepth the maximum number of directory levels to search matcher the function used to decide whether a file should be included in the returned stream options options to configure the traversal
Question : You have been given below code. What is the expected behavior ?
package com.hadoopexam;
import java.io.IOException;
public class Welcome extends Thread {
@Override public void run() { System.out.print("HadoopExam"); }
public static void main(String[] args) throws IOException { Welcome wel = new Welcome(); System.out.println(".com"); } } 1. It will give compile time error
Correct Answer : Get Lastest Questions and Answer : Explanation: As we can see we have created the object of Thread class named Welcome , which represent a Thread object. However, we have not called start() method on that Thread object. Hence, its run() method will not be executed. Only main() method Sysout will be printed.