Premium

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



Question : You have been given below code.
package com.hadoopexam;

import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

class Welcome {

String fName;
String lName;

public Welcome(String fn, String ln) {
fName = fn;
lName = ln;
}

public String getfName() {
return fName;
}

public String getlName() {
return lName;
}

public static void main(String[] args) throws IOException {
List allUser = Arrays.asList (
new Welcome ("Amit", "Kumar"),
new Welcome ("Amit", "Jain"),
new Welcome ("Vineet", "Sinha"));
List sortedUser = allUser.stream()
//n1

.collect(Collectors.toList());
sortedUser.stream().forEach((wel) -> System.out.println(wel.getfName() + " " + wel.getlName()));
}
}

Which of the below code will be replaced at n1, will print following result?

Vineet Sinha
Amit Jain
Amit Kumar


 : You have been given below code.
1. .sorted (Comparator.comparing(Welcome::getfName).reversed().thenComparing(Welcome::getlName))

2. .sorted (Comparator.comparing(Welcome::getfName).thenComparing(Welcome::getlName))

3. Access Mostly Uused Products by 50000+ Subscribers

4. map(Welcome::getfName).sorted(Comparator.reverseOrder().map(Welcome::getlName).reserved


Correct Answer : Get Lastest Questions and Answer :
Explanation: As you can see output of the code, it clearly says that output sorted in descending order by first name and ascending order by last name.

Comparator java.util.Comparator.thenComparing(Function keyExtractor)

Returns a lexicographic-order comparator with a function that extracts a Comparable sort key.

Stream java.util.stream.Stream.sorted(Comparator comparator)

Returns a stream consisting of the elements of this stream, sorted according to the provided Comparator.
For ordered streams, the sort is stable. For unordered streams, no stability guarantees are made.
This is a stateful intermediate operation.
Parameters:
comparator a non-interfering, stateless Comparator to be used to compare stream elements





Question : You have been given below code

package com.hadoopexam;

public enum INR {
RS1(100), QUARTERRS(25);
private int value;

public INR(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}


package com.hadoopexam;

public class Coin {

public static void main(String[] args) {
INR inr = new INR.QUARTERRS;
System.out.println(inr.getValue());
}
}

There seems to be a compile time error. How, will fix it?

A. Remove new keyword from this line INR inr = new INR.QUARTERRS;
B. Correct statement as INR inr = new INR.QUARTERRS();
C. Keep INR class as subclass of Coin class.
D. Change the access modifier public to private for INR(int value)
 : You have been given below code
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D
5. B,D

Correct Answer : Get Lastest Questions and Answer :
Explanation: A constructor in an enum class can only be specified as private.

Enums are implicitly declared public, static, and final, which means you cannot extend them.

When you define an enumeration, it implicitly inherits from java.lang.Enum.
Internally, enumerations are converted to classes. Further, enumeration constants
are instances of the enumeration class for which the constant is declared as a
member.

You can apply the valueOf() and name() methods to the enum element to return the
name of the enum element.

If you declare an enum within a class, then it is by default static.

You cannot use the new operator on enum data types, even inside the enum class

You can compare two enumerations for equality using == operator.

If enumeration constants are from two different enumerations, the equals() method
does not return true.

When an enumeration constant's toString() method is invoked, it prints the name
of the enumeration constant.

The static values() method in the Enum class returns an array of the enumeration
constants when called on an enumeration type.

Enumeration constants cannot be cloned. An attempt to do so will result in a
CloneNotSupportedException.





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

package com.hadoopexam;

class PDFScanner implements AutoCloseable {
public void close() throws Exception {
System.out.print("3 Scanner closed, ");
}

public void scanPDF() throws Exception {
System.out.print("1 PDF Scanner , ");
throw new Exception("2 Unable to scan, ");
}
}

package com.hadoopexam;

class PDFPrinter implements AutoCloseable {
public void close() throws Exception {
System.out.print("5 Printer closed, ");
}

public void printCoursePDF() {
System.out.print("4 Print pdf, ");
}
}


package com.hadoopexam;

import java.io.IOException;

class Welcome {

public static void main(String[] args) throws IOException {
try (PDFScanner courseScanner = new PDFScanner(); PDFPrinter taskPrinter = new PDFPrinter()) {
courseScanner.scanPDF();
taskPrinter.printCoursePDF();
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
}

 : You have been given below code, what is expected behavior?
1. 1 PDF Scanner , 5 Printer closed, 3 Scanner closed, 2 Unable to scan,

2. 5 Printer closed, 3 Scanner closed,

3. Access Mostly Uused Products by 50000+ Subscribers

4. 3 Scanner closed

Correct Answer : Get Lastest Questions and Answer :
Explanation: Both of the classes instances will be created in try block. try (PDFScanner courseScanner = new PDFScanner(); PDFPrinter taskPrinter = new PDFPrinter())

Next it will call courseScanner.scanPDF();

It prints : 1 PDF Scanner ,

Then it will throw throw new Exception("2 Unable to scan, ");

It will call the reverse order of close() method for each resources opened in try block. Hence, Printer will be closed first. "5 Printer closed" then Scanner will be closed "3
Scanner closed" and then main method exception block will be called.

You cannot assign to the resource variables declared in the try-with-resources within
the body of the try-with-resources statement. This is to make sure that the same
resources acquired in the try-with-resources header are released in the finally block.

It is a common mistake to close a resource explicitly inside the try-with-resources
statement. Remember that try-with-resources expands to calling the close()
method in the finally block, so the expanded code will have a double call to the
close() method if you explicitly provide a close() method.

The documentation of the close() method in the Scanner class says that if the
scanner object is already closed, then invoking the method again will have no effect.
So, you are safe in this case. However, in general, you cannot expect all the resources
to have implemented a close() method that is safe to call twice. So, it is a bad
practice to explicitly call the close() method inside a try-with-resource statement.


Related 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



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);



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



Question : You have been given below information

Character Stream : Derived using Reader(Reading from Character file) and Writer (Writing character to files) interface
Byte Stream : Derived using InputStream(Reading from file) and OutputStream(for writing to file)

Which of the following statement is correct?
 : You have been given below information
1. In character streams (e.g. HadoopExam.com), data is handled in terms of bytes (e.g. 0100001001), in byte streams, data is handled in terms of Unicode characters
e.g. UTF-8. UTF-16.

2. Character streams are suitable for reading or writing to files such as executable files, image files, and files in low-level file formats such as .zip, .class,
and .jpg.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Byte streams are meant for handling binary data (e.g. 1010101010); character streams are meant for human-readable characters.



Question : You have been given below code,

package com.hadoopexam;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Welcome implements Serializable {
int value;
Welcome(int val) {
value = val;
}

public static void main(String[] args) throws IOException {
Welcome welcome = new Welcome(100);
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("HadoopExam.data"))) {
oos.writeObject(welcome);
welcome.setValue(101);
oos.writeObject(welcome);
}
}

private void setValue(int i) {
value = i;
}
}

What would be printed when you desacralize the "HadoopExam.data" and extract value;

 : You have been given below code,
1. 101

2. 100

3. Access Mostly Uused Products by 50000+ Subscribers

4. Null

5. Cannot be predicted



Question : Which of the following is a correct way to create Local instance?

A. Locale loc1 = "CA";
B. Locale loc2 = Locale.getInstance("en");
C. Locale loc3 = Locale.getLocaleFactory("CA");
D. Locale loc4 = Locale.GERMANY;
E. Locale loc5 = new Locale("en", "CA");

 : Which of the following is a correct way to create Local instance?
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. D,E
5. A,E