Premium

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



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

package com.hadoopexam;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream; public final class Welcome {
public static void main(String[] args) {
Stream paths = Stream.of(Paths.get("resources\\Message.properties"),
Paths.get("resources\\Message.txt"), Paths.get("resources\\Message.xml"));
paths.filter(s -> s.toString().endsWith("txt")).forEach(s -> {
try {
Files.readAllLines(s).stream().forEach(System.out::println); // line n1
} catch (IOException e) {
System.out.println("Exception");
} });
}
}
 : You have been given following code, what is the expected behavior?
1. The program prints the content of resources\\Message.txt file.

2. The program prints:
Exception
<>
Exception

3. Access Mostly Uused Products by 50000+ Subscribers

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 {

 : You have been given below code, which of the following changes will help to compile the 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: : java.lang.AutoCloseable

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.

void com.hadoopexam.Peer.close() throws IOException

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.

 : Which of the following are true for creating Singleton object?
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. D,E
5. A,E

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.



Related Questions


Question : You have been given below code, what is the behavior you can expect by looking at the code?

public class Welcome {
public Welcome() {
System.out.println("In Welcome constructor ");
}

public void hello() {
enum Company { HADOOPEXAM, QUICKTECHIE, TRAINING4EXAM }
}
}


 : You have been given below code, what is the behavior you can expect by looking at the code?
1. This code will compile perfectly.

2. This code will compile cleanly without any compiler warnings or errors, and when used, will generate a runtime exception

3. This code will compile cleanly without any compiler warnings or errors, and when used, will run without any problems

4. This code will generate compile time error.



Question : You have been given following Parent child Interface and classes.

package com.hadoopexam;

interface Parent {
default void call() {
System.out.println("Parent...");
}
}

package com.hadoopexam;

interface Child1 extends Parent {
default void call() {
System.out.println("Child1...");
}
}

package com.hadoopexam;

interface Child2 {
public static void call() {
System.out.println("Child 2..");
}
}

package com.hadoopexam;

public class GrandChild implements Child2, Child1 {
public static void main(String[] args) {
new GrandChild().call();
}
}

What happens, when you run above GrandChild application?


 : You have been given following Parent child Interface and classes.
1. There will be a compile time error. Because you try to implement multiple inheritance.

2. There will be a compile time error. Because there is a confusion which call() method should be called.

3. There will be a no compile time error, and it will print "Child1..." when executed.

4. There will be a no compile time error, and it will print "Child2..." when executed.




Question : You have been given following code, with the LambdaFunction.
package com.hadoopexam;

class Welcome {
@FunctionalInterface
interface HadoopFunction {
int call(int j);

boolean equals(java.lang.Object arg0);
}

public static void main(String[] args) {
HadoopFunction hadoopFunction = val -> val * new Integer(6); // n1
System.out.println(hadoopFunction.call(2));
}
}

 : You have been given following code, with the LambdaFunction.
1. There will be a compile time error. Because there is no lambda function defined which can take Integer as an argument.

2. There will be a compile time error. Because there is no lambda function defined which can take two Integer values as argument2.

3. There will be a compile time error. Because there is no lambda function defined for equals method.

4. The program will run and compile perfectly, because auto unboxing will be applied and it will print 12.



Question : You have been given below code, please select the correct option which applies to it.

package com.hadoopexam;

import java.util.*;

class Welcome {
public static void main(String []args) {
List hList = new ArrayList<>();
hList.add(1);
hList.add(2);
System.out.println("Contents in list are : " + hList);
}
}
 : You have been given below code, please select the correct option which applies to it.
1. Code will compile and run perfectly and print to "Contents in list are : [1,2]"

2. Code will compile and run perfectly and print to "Contents in list are : [2,1]"

3. Code will compile successfully but will produce run time error IllegalStateException

4. Code will not compile



Question : You have been given below code, what would be printed once you run the code.

package com.hadoopexam;

import java.util.*;

class Welcome {
public static void main(String[] args) {
List integerList = new LinkedList<>();
List doublelList = new LinkedList<>();
System.out.println("Type of integerList = " + integerList.getClass());
System.out.println("Type of doublelList = " + doublelList.getClass());
}
}
 : You have been given below code, what would be printed once you run the code.
1. It will print following statements
Type of integerList = class java.util.LinkedList
Type of doublelList = class java.util.LinkedList

2. It will print following statements
Type of integerList = class java.util.LinkedList<>
Type of doublelList = class java.util.LinkedList<>

3. It will print following statements
Type of integerList = class java.util.LinkedList
Type of doublelList = class java.util.LinkedList

4. It will print following statements
Type of integerList = class java.util.List
Type of doublelList = class java.util.List




Question : You have been given following code, please select the correct statement for it.

package com.hadoopexam;

import java.util.Arrays;

class Welcome {
public static void main(String[] args) {
String[] names = { "Bala", "Reena", "Iva", "Cate" };
Arrays.sort(names, null); //n1
for (String name : names) {
System.out.print(name + " ");
}
}
}

 : You have been given following code, please select the correct statement for it.
1. Code will not compile.

2. Code will compile but produce runtime error. As no InvalidComparatorException is defined.

3. There will be no compile time error. Code will be run successfully and print Bala Cate Iva Reena

4. There will be no compile time error. Code will be run successfully and print "Bala", "Reena", "Iva", "Cate"