1Z0-804 | Improve 1Z0-804 Exam Study Guides With New Update Exam Questions


Q61. Given the interface: 

Public interface Idgenerator { 

int getNextId(); 

Which class implements IdGenerator in a thread-safe manner, so that no threads can get a duplicate id valuecurrent access? 

A. Public class generator Implements IdGenerator { 

Private AtomicInteger id = new AtomicInteger (0); 

return id.incrementAndget(); 

}} 

B. Public class Generator Implements idGenerator { 

private int id = 0; 

return ++id; }} 

C. Public class Generator Implements IdGenerator { 

private volatile int Id = 0; 

return ++Id; 

D. Public class Generator Implements IdGenerator { 

private int id = 0; 

public int getNextId() { 

synchronized (new Generator()) { 

return ++id; 

}}} 

E. Public class Generator Implements IdGenerator { 

private int id = 0; 

public int getnextId() { 

synchronized (id) { 

return ++id; 

}}} 

Answer:

Explanation: 

Code that is safe to call by multiple threads simultaneously is called thread safe. If a piece of code is threadsafe, then it contains no race conditions. Race condition only occur when multiple threads update sharedresources. Therefore it is important to know what resources Java threads share when executing. 

In Java you can mark a method or a block of code as synchronized. Synchronized blocks can be used to avoidrace conditions. 

A, B, C : false: wrong Implementation ( missing int getNextId(); ) 

E: false: synchronized (mutex Object! not Simple Data Type) 

Q62. Given: What is the result? 

A. p001 Widget p002 X-Large Widget 

B. p002 Large Widget p001 Widget 

C. p002 X-large Widget p001 Widget 

D. p001 Widget p002 Large Widget 

E. compilation fails 

Answer:

Explanation: Compiles fine. Output is: P001 Widget P002 X-Large Widget Line: partList.put("P002", "X-Large Widget"); >> overwrites >> line:partList.put("P002", "Large Widget"); put V put(K key, V value) Associates the specified value with the specified key in this map (optional operation). If the map previouslycontained a mapping for the key, the old value is replaced by the specified value. (Amap m is said to contain amapping for a key k if and only if m.containsKey(k) would return true.) 

Parameters: key - key with which the specified value is to be associated value - value to be associated with the specified key Returnsthe previous value associated with key, or null if there was no mapping for key. (A null return can alsoindicate that the map previously associated null with key, if the implementation supports null values.) 

Q63. Given the code fragment: 

Why is there no output when otherMethod is called? 

A. An exception other than IOException is thrown. 

B. Standard error is not mapped to the console. 

C. There is a compilation error. 

D. The exception is suppressed. 

Answer:

Explanation: 

C: wenn printStackTrace() ohne Referenz auf das Exception object aufgerufen 

A : java.io.FileNotFoundException: wenn e.printStackTrace(); 

The code compiles fine 

The line 

FileInputStream fis = new FileInputStream(file)) 

will fail at runtime since file is an empty string. 

Note: 

public void printStackTrace() 

Prints this throwable and its backtrace to the standard error stream. 

Q64. Give: Which is correct? 

A. Employee takes advantage of composition. 

B. Employee "has-an" Email. 

C. Employee "is-a" LetterPrinter. 

D. Employee has low cohesion. 

Answer:

Explanation: 

The relationship between Employee and e-mail is poorly implemented here. 

There is low cohesion. 

Note: 

Low cohesion is associated with undesirable traits such as being difficult to maintain, 

difficult to test, difficult toreuse, and even difficult to understand. 

Cohesion is decreased if: 

The functionalities embedded in a class, accessed through its methods, have little in 

common. Methods carryout many varied activities, often using coarsely-grained or 

unrelated sets of data. Disadvantages of lowcohesion (or"weak cohesion") are: 

Increased difficulty in understanding modules. 

Increased difficulty in maintaining a system, because logical changes in the domain affect 

multiple modules,and because changes in one module require changes in related modules. 

Increased difficulty in reusing amodule because most applications won't need the random 

set of operations provided by a module.Reference:Cohesion (computer science) 

Q65. Given: 

What is the result of invoking Car's scop method? 

A. Both vehicles and Motorized's stop methods are invoked. 

B. Vehicles stop method is invoked. 

C. Motorized's stop method is invoked-

D. The implementation of the Car's stop determines the behavior. 

E. Compilation fails. 

Answer:

Explanation: 

The Car class is implementing the methods. Methods are not implemented in interfaces. 

Q66. Given: 

What is the most likely result? 

A. Main One Two 

B. Main Two One 

C. One Two Main 

D. One Main Two 

E. Two Main One 

Answer:

Q67. Given: 

What is the result? 

A. Event Quiz 

B. Event Event 

C. Quiz Quiz 

D. Quiz Event 

E. Compilation fails 

Answer:

Q68. Which two are true about Singletons? 

A. A Singleton must implement serializable. 

B. A Singleton has only the default constructor. 

C. A Singleton implements a factory method. 

D. A Singleton improves a class's cohesion. 

E. Singletons can be designed to be thread-safe. 

Answer: C,E 

Q69. Given these facts about Java classes in an application: 

-

Class X is-a Class SuperX. 

-

Class SuperX has-a public reference to a Class Z. 

-

Class Y invokes public methods in Class Util. 

-

Class X uses public variables in Class Util. 

Which three statements are true? 

A. Class X has-a Class Z. 

B. Class Util has weak encapsulation. 

C. Class Y demonstrates high cohesion. 

D. Class X is loosely coupled to Class Util. 

E. Class SuperX's level of cohesion CANNOT be determined 

Answer: B,D,E 

Explanation: 

B: Has class Util has both public methods and variables, it is an example of weak 

encapsulation. 

Note:Inheritance is also sometimes said to provide "weak encapsulation," because if you 

have code thatdirectly uses a subclass, such as Apple, that code can be broken by 

changes to a superclass, such as Fruit. 

One of the ways to look at inheritance is that it allows subclass code to reuse superclass 

code. For example, if 

Apple doesn't override a method defined in its superclass 

Fruit, Apple is in a sense reusing Fruit's implementation of the method. But Apple only 

"weakly encapsulates"the Fruit code it is reusing, because changes to Fruit's interface can 

break code that directly uses Apple. 

D: 

Note:Tight coupling is when a group of classes are highly dependent on one another. 

This scenario arises when a class assumes too many responsibilities, or when one concern 

is spread overmany classes rather than having its own class. 

Loose coupling is achieved by means of a design that promotes single-responsibility and 

separation ofconcerns. 

A loosely-coupled class can be consumed and tested independently of other (concrete) 

classes. 

Interfaces are a powerful tool to use for decoupling. Classes can communicate through 

interfaces rather thanother concrete classes, and any class can be on the other end of that 

communication simply by implementingthe interface. 

E: Not enough information regarding SuperX' to determine the level of cohesion. 

Q70. Given the integer implements comparable: 

What is the result? 

A. 4 1 

B. 1 2 

C. 32 

D. 21 

E. 2 3 

Answer:

Explanation: 

binarySearch 

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 

Searches the specified list for the specified object using the binary search algorithm. 

The list must be sorted into ascending order according to the natural ordering of its 

elements (as by the sort(List) method) prior to making this call. If it is not sorted, the results 

are undefined. 

Parameters: 

list - the list to be searched. 

key - the key to be searched for. 

Returns: 

the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1).