class Welcome { public static void main(String[] args) { OutputStream os = new FileOutputStream("HadoopExam.txt"); System.setErr(new PrintStream(os)); System.err.println("There is an Error while reading file"); } } 1. It will not compile.
2. It will compile and run perfectly and print the message in "HadoopExam.txt"
4. It will run and print message "New file created as it was not exist" on console
Correct Answer : Get Lastest Questions and Answer : Explanation: : It is very simple, we know that this file API throws 'FileNotFoundException' . Hence, we should catch it or throws it back. Which we are not doing in this code. Hence, there will be a Compile time error. However, if you have throws exception as below.
public static void main(String[] args) throws FileNotFoundException { OutputStream os = new FileOutputStream("HadoopExam.txt"); System.setErr(new PrintStream(os)); System.err.println("There is an Error while reading file"); }
Then you can redirect the System.err programmatically using the setErr() method. System.err is of type PrintStream, and the System.setErr() method takes a PrintStream as an argument. Once the error stream is set, all writes to System.err will be redirected to it. Hence, this program will create HadoopExam.txt with the text "Error" in it.
Question : You have passed following string in Console
"I am learning Advanced Java from HadoopExam.com".
Which of the following Code snippet will help above string from Console? 1. BufferedReader br = new BufferedReader(System.in); String str = br.readLine();
Correct Answer : Get Lastest Questions and Answer : Explanation: This is the right way to read a line of a string from the console where you pass a System.in reference to InputStreamReader and pass the returning reference to BufferedReader. From the BufferedReader reference, you can call the readLine() method to read the string from the console
Question : You have been given below code, what is the expected behavior (Assuming you are not executing from eclipse)
class Welcome { public static void main(String[] args) throws FileNotFoundException { Console console = System.console(); console.printf("%d %1$x %1$o", 16); } } 1. T his program crashes after throwing an IllegalFormatException
2. T his program crashes after throwing ImproperFormatStringException
Correct Answer : Get Lastest Questions and Answer : Explanation: : In the format specifier, "1$" refers to first argument, which is 16 in this printf statement. Hence "%1$x" prints the hexadecimal value of 16, which is 10. Further, "%1$o" prints the octal value of 16, which is 20. Hence the output "16 10 20" from this program.