4. The program prints the content of the three files.
Correct Answer : Get Lastest Questions and Answer : Explanation: Here we are filtering the path, only considering the path which has txt in its name.
Question : You have been given below code, which of the following changes will help to compile the code? package com.hadoopexam;
import java.io.IOException;
final class Peer {// line n1 // line n2 public void message() { System.out.print("Welcome to HadoopExam"); }
}
public class Peer2 { public static void main(String[] args) throws Exception { try (Peer f = new Peer()) { f.message(); } } } A. Replace n1 with implements AutoCloseable { B. Replace n2 with public void close() throws IOException { System.out.print("Close"); } C. Replaceline n1with: class Folder extends Closeable {
D. Replaceline n1with: class Folder extends Exception {
An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur.
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement. While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail. Cases where the close operation may fail require careful attention by implementers. It is strongly advised to relinquish the underlying resources and to internally mark the resource as closed, prior to throwing the exception. The close method is unlikely to be invoked more than once and so this ensures that the resources are released in a timely manner. Furthermore it reduces problems that could arise when the resource wraps, or is wrapped, by another resource.
Question : Which of the following are true for creating Singleton object? A. Make the classstatic. B. Make the constructorprivate. C. Overrideequals() andhashCode() methods of the java.lang.Object class. D. Use astaticreference to point to the single instance. E. Implement theSerializableinterface.
Correct Answer : Get Lastest Questions and Answer : Explanation: The singleton class offers two things: one and only one instance of the class, and a global single point of access to that object.
A singleton class ensures that only one instance of that class is created. To ensure point of access, the class controls instantiation of its object. Singleton classes are found in many places in Java Development Kit (JDK), such as java.lang.Runtime.
The constructor of the class is declared as private, so you cannot simply create a new instance of the class using the new operator.
The only way to get an instance of this class is to call the static member method of the class via the getInstance() method. This method checks whether a object already exists or not. If not, it creates a instance and assigns it to the static member variable. In this way, whenever you call the getInstance() method, it will always return the same object of the class.