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 ?

package com.hadoopexam;
class Welcome {
public static void main(String[] args) {
"HadoopExam.com".chars().distinct().peek(ch -> System.out.printf("%c ", ch)).sorted();
}
}

 : You have been given below code. What is the expected behavior ?
1. Code will through a compile time error.

2. Code will run perfectly and print "H a d o p E x m . c"

3. Access Mostly Uused Products by 50000+ Subscribers

4. Code will run perfectly and print ". a c d m o p x E H "


Correct Answer : Get Lastest Questions and Answer :
Explanation: : As you know, stream pipeline is executed only when it has termination operation like count(), collect(), sum(), forEach or reduce() etc.
In the given code there is no termination method has been called. Hence, program will not print anything.




Question : You have been given below 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.nio.file.attribute.BasicFileAttributes;
import java.util.stream.Stream;


public class Welcome {

public static void main(String[] args) throws IOException {
Stream userHomes = Files.walk(Paths.get(System.getProperty("user.home")));
userHomes.forEach (fName ->
{//line n1
try
{
Path aPath = fName.toAbsolutePath();//line n2
System.out.println(fName + ":" + Files.readAttributes(aPath, BasicFileAttributes.class).creationTime());
} catch (IOException ex)
{
ex.printStackTrace();
}
});
}

}

 : You have been given below code, what is the expected behavior?
1. All files and directories under thehomedirectory are listed along with their attributes.

2. A compilation error occurs atline n1.

3. Access Mostly Uused Products by 50000+ Subscribers

4. A compilation error occurs atline n2.


Correct Answer : Get Lastest Questions and Answer :
Explanation: BasicFileAttributes java.nio.file.Files.readAttributes(Path path, Class type, LinkOption... options) throws IOException

Reads a file's attributes as a bulk operation.
The type parameter is the type of the attributes required and this method returns an instance of that type if supported. All implementations support a basic set of file attributes
and so invoking this method with a type parameter of BasicFileAttributes.class will not throw UnsupportedOperationException.
The options array may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed and the file attribute
of the final target of the link is read. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.
It is implementation specific if all file attributes are read as an atomic operation with respect to other file system operations.
Usage Example: Suppose we want to read a file's attributes in bulk:
Path path = ...
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);

Alternatively, suppose we want to read file's POSIX attributes without following symbolic links:
PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);

Parameters:
The BasicFileAttributes type
path the path to the file
type the Class of the file attributes required to read
options options indicating how symbolic links are handled
Returns:
the file attributes


BasicFileAttributes java.nio.file.Files.readAttributes(Path path, Class type, LinkOption... options) throws IOException

Reads a file's attributes as a bulk operation.
The type parameter is the type of the attributes required and this method returns an instance of that type if supported. All implementations support a basic set of file attributes
and so invoking this method with a type parameter of BasicFileAttributes.class will not throw UnsupportedOperationException.
The options array may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed and the file attribute
of the final target of the link is read. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.
It is implementation specific if all file attributes are read as an atomic operation with respect to other file system operations.
Usage Example: Suppose we want to read a file's attributes in bulk:
Path path = ...
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);

Alternatively, suppose we want to read file's POSIX attributes without following symbolic links:
PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);

Parameters:
The BasicFileAttributes type
path the path to the file
type the Class of the file attributes required to read
options options indicating how symbolic links are handled
Returns:
the file attributes





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

package com.hadoopexam;

import java.util.Set;
import java.util.TreeSet;

public class Welcome {

int userId;
String userName;

public Welcome(int userId, String userName) {
this.userId = userId;
this.userName = userName;
}

public String toString() {
return userId + ":" + userName;
}

public static void main(String[] args) {
Set users = new TreeSet <> ();
users.add(new Welcome (101, "Amit"));
users.add(new Welcome (102, "Anurag"));
System.out.println(users);
}
}

 : You have been given below code, what is the behavior expected?
1. 101 : Amit , 102:Anurag

2. 102 : Anurag , 102: Amit

3. Access Mostly Uused Products by 50000+ Subscribers

4. It will give compile time error.

5. It will give runtime error "java.lang.ClassCastException: com.hadoopexam.Welcome cannot be cast to java.lang.Comparable"


Correct Answer : Get Lastest Questions and Answer :
Explanation: As we know, all the objects stored in TreeSet must be comparable.

java.util.TreeSet.TreeSet()

Constructs a new, empty tree set, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface. Furthermore,
all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set. If the user attempts to add an element to
the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add call will throw a ClassCastException.



Related Questions


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

package com.hadoopexam;

import java.util.concurrent.atomic.AtomicInteger;

public class Welcome {
static AtomicInteger atomicInt = new AtomicInteger(100);

public static void plustOne() {
atomicInt.incrementAndGet();
}

public static void minusOne() {
atomicInt.getAndDecrement();
}

public static void setValue() {
atomicInt.compareAndSet(100, 99);
}

public static void main(String[] args) {
plustOne();
minusOne();
setValue();
System.out.println(atomicInt);
}
}

 : You have been given below code, what is the expected behavior?
1. It will give compile time error.

2. It will print 100

3. Access Mostly Uused Products by 50000+ Subscribers

4. It will print 99

5. It will print "Null"



Question : You have been given below code, what is the output, when executed using command "java -ea RateOfInterest"
package com.hadoopexam;
public class Welcome {

public static void main(String[] args) {
int courseFee = 0;
String courseName = "Web Service";
switch (courseName) {
case "Hadoop":
courseFee = 79;
break;
case "Spark":
courseFee = 89;
break;
default:
assert false : "Given course is in Progress and currently not available"; // line n1
}
System.out.println("Fees for the course is :" + courseFee);
}
}
 : You have been given below code, what is the output, when executed using command
1. AssertionError: Given course is in Progress and currently not available

2. It will print Given course is in Progress and currently not available.

3. Access Mostly Uused Products by 50000+ Subscribers

4. A compilation error occurs at line n1.



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

package com.hadoopexam;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Welcome implements Callable {
String message;

public Welcome(String s) {
this.message = s;
}

public String call() throws Exception {
return message.concat("Welcome to HadoopExam Learning Resources");
}

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(4); // line n1
Future futureResult = executorService.submit(new Welcome("Thank you ... "));
String resultValue = futureResult.get().toString();
System.out.println(resultValue);
}
}

 :
1. The program prints Thank you ... Welcome to HadoopExam Learning Resources and terminates.

2. The program prints Thank you ... Welcome to HadoopExam Learning Resources and does not terminate.

3. Access Mostly Uused Products by 50000+ Subscribers

4. An ExecutionExceptionis thrown at run time.



Question : You have been given below code, what is 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.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

public class Welcome implements Runnable {
String fileName;

public Welcome(String fileName) {
this.fileName = fileName;
}

public void run() {
System.out.println(fileName);
}

public static void main(String[] args) throws IOException, InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
Stream allFiles = Files.walk(Paths.get(System.getProperty("user.dir")));
allFiles.forEach(file -> {
executorService.execute(new Welcome(file.getFileName().toString())); //n1
});
executorService.shutdown();
executorService.awaitTermination(5, TimeUnit.DAYS);// n2
}
}

 : You have been given below code, what is expected behavior?
1. The program throws a runtime exception at line n2.

2. The program prints files names concurrently.

3. Access Mostly Uused Products by 50000+ Subscribers

4. A compilation error occurs at line n1.



Question :
You have been given below code

package com.hadoopexam;


public class Welcome {
public static int checkValue(String s1, String s2) {
return s1.length() - s2.length();
}

public static void main(String[] args) {
String[] siteArray = new String[] { "Hadoop" , "Quick" , "Training" };
// line n1
for (String siteName : siteArray) {
System.out.print(siteName + " ");
}
}
}

Which code segment from below, will help us to print as "Quick Hadoop Training"

 :
1. Arrays.sort(siteArray, Welcome:: checkValue);

2. Arrays.sort(siteArray, (Welcome :: new) :: checkValue);

3. Access Mostly Uused Products by 50000+ Subscribers

4. Arrays.sort(siteArray, Welcome :: new :: checkValue);



Question : You have been given below code, which of the following statement, replace at n can produce "Hadoop Data Science Spark"

package com.hadoopexam;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;public class Welcome {
String courseName;

Welcome(String course) {
this.courseName = course;
}

public static void main(String[] args) {
List courseName = Arrays.asList(new Welcome(" Hadoop"),
new Welcome(" Data Science"), new Welcome(" Spark"));
Stream stream = courseName.stream();
// line n1


}
}
 : You have been given below code, which of the following statement, replace at n can produce
1. stream.forEach(System.out::print);

2. stream.map(a-> a.courseName).forEach(System.out::print);

3. Access Mostly Uused Products by 50000+ Subscribers

4. stream.forEachOrdered(System.out::print);