aiotestking uk

1Z0-804 Exam Questions - Online Test


1Z0-804 Premium VCE File

Learn More 100% Pass Guarantee - Dumps Verified - Instant Download
150 Lectures, 20 Hours

Q1. Given the fragment: 

If thread a and thread b are running, but not completing, which two could be occurring? 

A. livelock 

B. deadlock 

C. starvation 

D. loose coupling 

E. cohesion 

Answer: A,B 

Explanation: 

A: A thread often acts in response to the action of another thread. If the other thread's action is also a responseto the action of another thread, then livelock may result. A thread often acts in response to the action ofanother thread. If the other thread's action is also a response to the action of another thread, then livelock mayresult. 

B: Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. 

Q2. 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; 

Q3. Which concept allows generic collections to interoperate with java code that defines collections that use rawtypes? 

A. bytecode manipulation 

B. casting 

C. autoboxing 

D. auto-unboxing 

E. type erasure 

Answer:

Explanation: 

The type erasure of its leftmost bound, or type Object if no bound was specified. Examples: type parameters type erasure List<String> List Map.Entry<String,Long> Map.Entry <T extends Cloneable & Comparable<T>> Cloneable <T extends Object & Comparable<T>> Object 

<T> T[] toArray(T[] a) Object[] toArray(Object[] a) 

The type erasure process can be imagined as a translation from generic Java source code 

back into regularJava code. In reality the compiler is more efficient and translates directly to 

Java byte code. But the byte codecreated is equivalent to the non-generic Java code. 

Q4. Given: What is the result? 

A. Both const and inner will be in the output. 

B. Only const will be in the output. 

C. Compilation fails due to an error on line A. 

D. Compilation fails due to an error on line B. 

E. An Exception is thrown at runtime. 

Answer:

Explanation: 

The code compiles fine. Note:The Runnable interface should be implemented by any class whose instances are intended to beexecuted by a thread. The class must define a method of no arguments called run. This interface is designed to provide a common protocol for objects that wish to execute code while they areactive. For example, Runnable is implemented by class Thread. Being active simply means that a thread hasbeen started and has not yet been stopped. 

In addition, Runnable provides the means for a class to be active while not subclassing Thread. Aclass that implements Runnable can run without subclassing Thread by instantiating a Thread instance andpassing itself in as the target. In most cases, the 

Runnable interface should be used if you are only planning tooverride the run() method and 

no other Thread methods. This is important because classes should not besubclassed 

unless the programmer intends on modifying or enhancing the fundamental behavior of the 

class. 

Note 2:start() 

Causes this thread to begin execution; the Java Virtual Machine calls the run method of 

this thread. 

Reference:java.lang 

Interface Runnable 

Q5. Select four examples that initialize a NumberFormat reference using a factory. 

A. NumberFormat nf1 = new DecimalFormat(); 

B. NumberFormat nf2 = new DecimalFormat("0.00") ; C. NumberFormat nf3 = NumberFormat.getInstance(); 

D. NumberFormat nf4 = NumberFormat.getIntegerInstance(); 

E. NumberFormat nf5 = DecimalFormat.getNumberInstance (); 

F. NumberFormat nf6 = NumberFormat.getCurrencyInstance () ; 

Answer: C,D,E,F 

Explanation: 

getInstance 

public static finalNumberFormatgetInstance() 

Returns the default number format for the current default locale. The default format is one 

of the styles 

provided by the other factory methods: getNumberInstance(E), getIntegerInstance(D), 

getCurrencyInstance(F) 

or getPercentInstance. Exactly which one is locale dependant. 

C: To obtain a NumberFormat for a specific locale, including the default locale, call one of NumberFormat'sfactory methods, such as getInstance(). E:To obtain standard formats for a given locale, use the factory methods on NumberFormat such asgetNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a givenlocale. F:To obtain standard formats for a given locale, use the factory methods on NumberFormat such asgetInstance or getCurrencyInstance. 

Reference:java.textClass NumberFormat 

Q6. Assuming the port statements are correct, which two (three?) code fragments create a one-byte file? 

A. OutputStream fos = new FileOutputStream(new File("/tmp/data.bin")); OutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); dos.writeByte(0); dos.close(); 

B. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); 

DataOutputStream dos = new DataOutputStream(fos); 

dos.writeByte(0); 

dos.close(); 

C. OutputStream fos = new FileOutputStream (new File ("/tmp/data.bin")); 

DataOutputStream dos = new DataOutputStream(fos); 

dos.writeByte(0); 

dos.close(); 

D. OutputStream fos = new FileOutputStream ("/tmp/data.bin"); fos.writeByte(0); 

fos.close(); 

Answer: A,B,C 

Explanation: 

B:Create DataOutputStream from FileOutputStream public static void main(String[] args) 

throws 

Exception { FileOutputStream fos = new FileOutputS tream("C:/demo.txt"); 

DataOutputStream dos = new 

DataOutputStream(fos); 

Note: 

The FileOutputStream class is a subclass of OutputStream. You can construct a 

FileOutputStream object by 

passing a string containing a path name or a File object. 

You can also specify whether you want to append the output to an existing file. 

public FileOutputStream (String path) 

public FileOutputStream (String path, boolean append) 

public FileOutputStream (File file) 

public FileOutputStream (File file, boolean append) 

With the first and third constructors, if a file by the specified name already exists, the file 

will be overwritten. Toappend to an existing file, pass true to the second or fourth 

constructor. 

Note 2:public class DataOutputStreamextends FilterOutputStreamimplements DataOutput 

A data output stream lets an application write primitive Java data types to an output stream 

in a portable way. 

An application can then use a data input stream to read the data back in. 

Reference:java.io Class DataOutputStream 

Q7. 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.) 

Q8. ITEM Table 

ID, INTEGER: PK 

DESCRIP, VARCHAR(100) 

PRICE, REAL 

QUALITY, INTEGER 

And given the code fragment (assuming that the SQL query is valid): 

What is the result of compiling and executing this code? 

A. An exception is thrown at runtime 

B. Compile fails 

C. The code prints Error 

D. The code prints information about Item 110 

Answer:

Explanation: 

Tricky: 

Compiles successfully ! Not B ! 

D is correct, if Column Quantity instead of Quality 

Table Item Column Quality --- System.out.println("Quantity: " + rs.getInt("Quantity")); 

wenn jedoch so gewollt: die Zeile gibt Error aus (die anderen funktionieren) !!! 

The connection conn is not defined. The code will not compile. 

Q9. Given the code fragment: 

What change should you make to apply good coding practices to this fragment? 

A. Add nested try-with-resources statements for the statement and ResultSet declarations. 

B. Add the statement and ResultSet declarations to the try-with-resources statement. 

C. Add a finally clause after the catch clause. 

D. Rethrow SQLException. 

Answer:

Explanation: 

The finally block always executes when the try block exits. This ensures that the finally block is executed evenif an unexpected exception occurs. But finally is useful for more than just exception handling -- it allows theprogrammer to avoid having cleanup code accidentally bypassed by a return, continue, or break.Putting cleanup code in a finally block is always a good practice, even when no exceptions areanticipated. 

Q10. 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