1Z0-804 | A Review Of Validated 1Z0-804 practice test


Q11. An application is waiting for notification of changes to a tmp directory using the following code statements: 

Path dir = Paths.get("tmp") 

WatchKey key = dir.register (watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) ; 

In the tmp directory, the user renames the file testA to testB, 

Which statement is true? 

A. The events received and the order of events are consistent across all platforms. 

B. The events received and the order of events are consistent across all Microsoft Windows versions. 

C. The events received and the order of events are consistent across all UNIX platforms. 

D. The events received and the order of events are platform dependent. 

Answer:

Explanation: 

Most file system implementations have native support for file change notification. The 

WatchService API takesadvantage of this support where available. 

However, when a file system does not support this mechanism, the WatchService will poll 

the file system,waiting for events. 

Note: 

WatchKey : When a Watchable entity is registered with a WatchService a key which is a 

WatchKey isgenerated. Initially the key is in ready state waiting to be notified of any events 

on the Watchable entity. Oncean event occurs the key goes into signaled state and allows 

to access the events using its pollEvents method. 

After processing the poll events the key has to be reset by invoking its reset method. 

Reference: The Java Tutorials,Watching a Directory for Changes 

Q12. Given the code fragment: 

And a DOS-based file system: 

Which option, containing statement(s), inserted at line 3, creates the file and sets its attributes to hidden andread-only? 

A. DOSFileAttributes attrs = Files.setAttribute(file,"dos:hidden","dos: readonly") Files.createFile(file, attrs) 

B. Files.craeteFile(file); Files.setAttribute(file,"dos:hidden","dos:readonly"); 

C. Files.createFile(file,"dos:hidden","dos:readonly"); 

D. Files.createFile(file); Files.setAttribute(file,"dos:hidden", true); Files.setAttribute(file,"dos:readonly", true); 

Answer:

Explanation: 

You can set a DOS attribute using the setAttribute(Path, String, Object, LinkOption...) 

method, as 

follows: 

Path file = ...; 

Files.setAttribute(file, "dos:hidden", true); 

Note: 

setAttribute 

public static Path setAttribute(Path path, 

String attribute, 

Object value, 

LinkOption... options) 

throws IOException 

Sets the value of a file attribute. 

Reference:Interface DosFileAttribute 

Q13. Which four are true about enums? 

A. An enum is typesafe. 

B. An enum cannot have public methods or fields. 

C. An enum can declare a private constructor. 

D. All enums implicitly implement Comparable. 

E. An enum can subclass another enum. 

F. An enum can implement an interface. 

Answer: A,C,D,F Explanation: 

C: The constructor for an enum type must be package-private or private access. Reference: Java Tutorials,Enum Types 

Q14. Given: 

What is the result? 

A. An exception is thrown at runtime on line 9. 

B. An exception is thrown at runtime on line 12 

C. onetwonull 

D. onetwothree 

E. twoonenull 

F. threetwoone 

Answer:

Explanation: 

addFirst void addFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted deque, it is generally preferable to 

use method offerFirst 

(E). 

pollLast 

E pollLast() 

Retrieves and removes the last element of this deque, or returns null if this deque is empty. 

Returns: 

the tail of this deque, or null if this deque is empty 

Q15. Given the code fragment: 

What is the result? 

A. getName (0): C: 

subpath (0, 2): C:educationreport.txt 

B. getName(0): C: 

subpth(0, 2): C:education 

C. getName(0): education 

subpath (0, 2): educationinstitute 

D. getName(0): education 

subpath(0, 2): educationinstitutestudent 

E. getName(0): report.txt 

subpath(0, 2): insritutestudent 

Answer:

Explanation: 

Example: 

Path path = Paths.get("C:\home\joe\foo"); 

getName(0) 

-> home 

subpath(0,2) 

Reference: The Java Tutorial, Path Operations 

Q16. Which class(es) safely protects the doIt () method from concurrent thread access? A. Option A 

B. Option B 

C. Option C 

D. Option D 

Answer: A,D 

Explanation: 

only A und D possible 

It should be pointed out that: 

public void blah() { 

synchronized (this) { 

// do stuff 

}} 

is semantically equivalent to: 

public synchronized void blah() { 

// do stuff 

Incorrect answer: 

B: A constructor cannot be synchronized. () Object cannot be resolved to a type 

C: in static context (static void main !) no reference to this possible 

Q17. Given: Which of the four are valid modifications to synchronize access to the valid list between threads t1 and t2? 

A. Replace line 1 with: 

Synchronized (t2) (t1.start();) synchronized(t1) (t2.start(); ) 

korrekte Schreibweise: synchronized (t2) {t1.start();} synchronized(t1) { t2.start();} 

B. Replace Line 2 with: 

static CopyWriteArrayList<Integer> list = new CopyWriteArrayList<>(); 

korrekte Schreibweise: static CopyOnWriteArrayList<Integer> list = new 

CopyOnWriteArrayList<>(); 

C. Replace line 3 with: 

synchronized public static void addItem () { 

korrekte Schreibweise: synchronized public static void addItem () { 

D. Replace line 4 with: 

synchronized (list) (list.add(1);) 

korrekte Schreibweise: synchronized (list) { (list.add(1); } 

E. Replace line 5 with: 

Synchronized public void run () { 

korrekte Schreibweise: synchronized public void run () { 

F. replace line 6 with: 

Synchronized (this) {for (in i = 0, i<5000, i++) WorkPool.addItem(); } 

korrekte Schreibweise: synchronized (this) {for (int i = 0; i<500; i++) WorkPool.addItem(); } 

G. Replace line 6 with: 

synchronized (bar) {for (int i= 0; i<5000; i++) WorkPool.addItem(); } 

korrekte Schreibweise: synchronized (bar) {for (int i= 0; i<500; i++) WorkPool.addItem(); } 

Answer: B,C,D 

Explanation: 

Away to create synchronized code is with synchronized statements. 

Unlike synchronized methods, synchronized statements must specify the object that 

provides theintrinsic lock: 

For example: 

public void addName(String name) { 

synchronized(this) { 

lastName = name; 

nameCount++; 

nameList.add(name); 

In this example, the addName method needs to synchronize changes to lastName and 

nameCount, but alsoneeds to avoid synchronizing invocations of other objects' methods. 

Without synchronized statements, therewould have to be a separate, unsynchronized 

method for the sole purpose of invoking nameList.add. 

Reference: The Java Tutorial,Intrinsic Locks and Synchronization 

Q18. A valid reason to declare a class as abstract is to: 

A. define methods within a parent class, which may not be overridden in a child class 

B. define common method signatures in a class, while forcing child classes to contain unique methodimplementations 

C. prevent instance variables from being accessed 

D. prevent a class from being extended 

E. define a class that prevents variable state from being stored when object Instances are serialized 

F. define a class with methods that cannot be concurrently called by multiple threads 

Answer:

Explanation: 

Note:An abstract method in Java is something like a pure virtual function in C++ (i.e., a virtualfunction that is declared = 0). In C++, a class that contains a pure virtual function is called an abstract classand cannot be instantiated. The same is true of Java classes that contain abstract methods. Any class with an abstract method is automatically abstract itself and must be declared as such. An abstract class cannot be instantiated. A subclass of an abstract class can be instantiated only if it overrides each of the abstract methods of itssuperclass and provides an implementation (i.e., a method body) for all of them. Such a class is often called aconcrete subclass, to emphasize the fact that it is not abstract. If a subclass of an abstract class does not implement all the abstract methods it inherits, that subclass is itselfabstract.static, private, and final methods cannot be abstract, since these types of methods cannot be overridden by asubclass. Similarly, a final class cannot contain any abstract methods. A class can be declared abstract even if it does not actually have any abstract methods. Declaring such a classabstract indicates that the implementation is somehow incomplete and is meant to serve as a superclass forone or more subclasses that will complete the implementation. Such a class cannot be instantiated. 

Q19. Given: 

What two changes, made independently, will enable the code to compile? 

A. Change the signature of Account to: public class Account. 

B. Change the signature of CheckingAccount to: public abstract CheckingAccount 

C. Implement private methods for deposit and withdraw in CheckingAccount. 

D. Implement public methods for deposit and withdraw in CheckingAccount. 

E. Change Signature of checkingAccount to: CheckingAccount implements Account. 

F. Make Account an interface. 

Answer: B,D 

Explanation: 

Compiler say: 

-

Der Typ CheckingAccount muss die übernommene abstrakte Methode Account.deposit(double) implementieren 

-

Der Typ CheckingAccount muss die übernommene abstrakte Methode Account.withdraw(double) implementieren ODER Typ CheckingAccount als abstract definieren 

Q20. Given: 

What is the result? 

A. 5 

B. 6 

C. An exception is thrown at runtime 

D. Compilation fails due to an error on line 6 

E. Compilation fails due to an error on line 7 

Answer:

Explanation: 

The code compile fine but java.lang.NullPointerException is thrown at runtime. 

x has no value. The code would run if line 2 was changed to: 

Integer x = 3;