Question : You have been given below code, which of the given snippet can be replaced at line n. So that entire file content can be printed at console?
public static void main(String[] args) throws IOException {
Path file = Paths.get ("HadoopExam.txt"); //n1 } } 1. Stream fc = (Stream) Files.readAllLines(file); fc.forEach (s -> System.out.println(s));
2. Stream fc = Files.lines (file); fc.forEach (s -> System.out.println(s));
3. List fc = Files.readAllLines(file); fc.stream().forEach (s -> System.out.println(s));
Correct Answer : 3 Explanation: List java.nio.file.Files.readAllLines(Path path) throws IOException
Read all lines from a file. 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.readAllLines(path, StandardCharsets.UTF_8) Parameters: path the path to the file Returns: the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified Throws: IOException - if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the file.
Question : You have following declaration, please select the one which correctly applies
package com.hadoopexam;
public abstract final class Welcome { } 1. It will throw a compile time error, as you cannot have Class without any method.
2. It will not throw any compile time error.
3. It will throw compile time error because, you can not have a final abstract class.
4. You must have at least one abstract method.
Correct Answer : 3 Explanation: As you know, the purpose of abstract class is that, it should be extended by child classes. If you mark your abstract class as an final, then it cannot be extended.
Question : You have been given below code, what is the expected behavior ?
class Welcome { public void fileDelete(String dirName) throws IOException { File[] allFilesAndDir = new File(dirName).listFiles(); System.out.println(Arrays.toString(allFilesAndDir)); if (allFilesAndDir != null && allFilesAndDir.length > 0) { for (File file : allFilesAndDir) { if (file.isDirectory()) { fileDelete(file.getAbsolutePath()); } else { if (file.getName().endsWith(".class")) { System.out.println("Deleted "+file); file.delete(); } } } } }
public static void main(String[] args) throws IOException { Welcome wel = new Welcome(); System.out.println(System.getProperty("user.dir")); wel.fileDelete(System.getProperty("user.dir"));
} }
1. The method deletes all the.class files in the Projects directory and its subdirectories.
2. The method deletes the .class files of the Projects directory only.
3. The method executes and does not make any changes to the Projects directory.
4. The method throws an IOException.
Correct Answer : 1 Explanation: listFiles() : It will return all the paths under the Project, including file path.
As we know, here fileDelete, method is recursively called for each directory. Hence, all the .class files in directory and its subdirectory will be deleted.
File[] java.io.File.listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory. Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting pathname will be relative to the same directory. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.
boolean java.io.File.delete()
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the java.nio.file.Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted. Returns: true if and only if the file or directory is successfully deleted; false otherwise Throws: SecurityException - If a security manager exists and its java.lang.SecurityManager.checkDelete method denies delete access to the file