Premium

Oracle Advacned Java Advanced Certification Questions and Answers (Dumps and Practice Questions)



Question : You have been given below code, what is the expected behavior from it?

package com.hadoopexam;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

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");
}
}
 : You have been given below code, what is the expected behavior from it?
1. It will not compile.

2. It will compile and run perfectly and print the message in "HadoopExam.txt"

3. Access Mostly Uused Products by 50000+ Subscribers

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?
 : You have passed following string in Console
1. BufferedReader br = new BufferedReader(System.in);
String str = br.readLine();


2. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();


3. Access Mostly Uused Products by 50000+ Subscribers
String str = isr.readLine();


4. String str = System.in.readLine();
String str;
System.in.scanf(str);


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)

package com.hadoopexam;

import java.io.Console;
import java.io.FileNotFoundException;

class Welcome {
public static void main(String[] args) throws FileNotFoundException {
Console console = System.console();
console.printf("%d %1$x %1$o", 16);
}
}
 : You have been given below code, what is the expected behavior (Assuming you are not executing from eclipse)
1. T his program crashes after throwing an IllegalFormatException

2. T his program crashes after throwing ImproperFormatStringException

3. Access Mostly Uused Products by 50000+ Subscribers

4. T his program prints: 16 10 20


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.



Related Questions


Question : You have been given below code, what is the expected behavior?
package com.hadoopexam;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public class Welcome {
public static void main(String[] args) {
IntStream values = IntStream.of(-1, -2, -3, -4, 5, -6, -7);
IntPredicate positivePred = val -> val > 0;
if (values.anyMatch(positivePred)) {
int positiveVal = values.filter(positivePred).findAny().getAsInt(); //
System.out.println(positiveVal);
}
}
}
 : You have been given below code, what is the expected behavior?
1. Program will compile and run perfectly and print 5

2. Program will compile and run perfectly and print nothing

3. Access Mostly Uused Products by 50000+ Subscribers

4. Program will not compile



Question : You have been given below code, what is the behavior expected?
package com.hadoopexam;

import java.util.stream.Stream;

public class Welcome {
public static void main(String[] args) {
boolean value = Stream.of("Ha", "do", "op", "Ex", "am", ".c", "om")
.filter(str -> str.length() > 3) //n1
.peek(System.out::println)
.allMatch(str -> str.length() > 5);
System.out.println(value);
}
}
 : You have been given below code, what is the behavior expected?
1. Code will give compile time error.

2. Code will throw IllegalStateException.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print true

5. Code will run perfectly and print true



Question : You have been given following code, what is the behavior you are expecting?
package com.hadoopexam;

import java.util.*;

class Welcome {
public static void main(String[] args) {
List list = Arrays.asList("Hadoop ", "Exam ", "Quick ", "Techie ");
Collections.sort(list, (str1, str2) -> str2.compareTo(str1));
list.forEach(word -> System.out.print(word));
}
}
 : You have been given following code, what is the behavior you are expecting?
1. Code will give compile time error.

2. Code will give runtime error

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print "Exam Hadoop Quick Techie"



Question : You have been given below code, what is the expected behavior

package com.hadoopexam;

import java.util.regex.Pattern;
import java.util.stream.Stream;

public class Welcome {
public static void main(String[] args) {
Stream words = Pattern.compile(" ").splitAsStream("Hadoop Exam .com Quick Techie .com");
System.out.println(words.map(word -> word.length()).sum());
}
}
 : You have been given below code, what is the expected behavior
1. Code will give compile time error.

2. Code will give run time error.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run and compile perfectly and print 29




Question : You have been given below code, select the correct behavior
package com.hadoopexam;

import java.util.OptionalInt;
import java.util.stream.IntStream;

public class Welcome {
public static void main(String args[]) {
maxValue(IntStream.of(1,2,3,4,5)); // //n1
}

public static void maxValue(IntStream values) {
OptionalInt max = values.max(); //n2
if (max.ifPresent()) { //n3
System.out.print(max.getAsInt());
}
}
}
 : You have been given below code, select the correct behavior
1. Code will give compile time error.

2. Code will give run time error

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print nothing



Question : You have been given below code, what is the expected behavior?

package com.hadoopexam;

import java.util.Optional;
import java.util.stream.Stream;

public class Welcome {
public static void main(String args[]) {
Stream.of("Hadoop ", "Exam ", ".com ", null).forEach(Welcome::changeCase);
}

private static void changeCase(String word) {
Optional str = Optional.ofNullable(word);
System.out.print(str.map(String::toUpperCase).orElse("NULL"));
}
}
 : You have been given below code, what is the expected behavior?
1. Code will give compile time error.

2. Code will give run time error.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print "Hadoop Exam .com NULL"