1Z0-804 | 10 Tips For Avant-garde 1Z0-804 practice test


Q51. What are two differences between Callable and Runnable? 

A. A Callable can return a value when executing, but a Runnable cannot. 

B. A Callable can be executed by a ExecutorService, but a Runnable cannot. 

C. A Callable can be passed to a Thread, but a Runnable cannot. 

D. A Callable can throw an Exception when executing, but a Runnable cannot. 

Answer: A,D 

Explanation: 

The Callable interface is similar to Runnable, in that both are designed for classes whose instances arepotentially executed by another thread. A Runnable, however, does not return a result and cannot throw achecked exception. 

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

Q53. Which statement creates a low overhead, low-contention random number generator that is isolated to thread togenerate a random number between 1 and 100? 

A. int i = ThreadLocalRandom.current().nextInt(1, 101); 

B. int i = ThreadSafeRandom.current().nextInt(1, 101); 

C. int i = (int) Math.random()*100+1; 

D. int i = (int) Math.random(1, 101); 

E. int i = new random().nextInt(100)+1; 

Answer:

Explanation: 

public class ThreadLocalRandom extends Random A random number generator isolated to the current thread. Like the global Random generator used by the Mathclass, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise bemodified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrentprograms will typically encounter much less overhead and contention. Use of ThreadLocalRandom isparticularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers inparallel in thread pools. Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads. 

This class also provides additional commonly used bounded random generation methods. Reference:Class ThreadLocalRandom 

Q54. Given: 

What is the result? 

A. Pastel Enamel Fresco Gouache B. Pastel *Enamel Fresco *Gouache 

C. Pastel Enamel Fresco Gouache 

D. Pastel Enamel, Fresco Gouache 

Answer:

Explanation: 

regex explanation: 

, = , 

= masks the following 

s = A whitespace character: [ t n x0B f r ] 

* = Greedy Quantifier: zero or more times Delimiter: comma + zero or more whitespace characters 

Q55. Given a language code of fr and a country code of FR, which file name represents a resource bundle file namethat is not the default? 

A. MessageBundle_fr_FR.properties 

B. MessageBundle_fr_FR.profile 

C. MessageBundle_fr_FR.xinl 

D. MessageBundle__fr__FR.Java 

E. MessageBundle__fr__FR.Locale 

Answer:

Explanation: 

The default file is MessageBundle.properties. The non-default file name is 

MessageBundle_fr_FR.properties 

Note 0:.properties is a file extension for files mainly used in Java related technologies to 

store the configurableparameters of an application. They can also be used for storing 

strings for Internationalization and localization;these are known as Property Resource 

Bundles. Each parameter is stored as a pair of strings, one storing thename of the 

parameter (called the key), and the other storing the value.Note 1:You can obtain an 

instance of ResourceBundle by calling its static getBundle method.public static 

ResourceBundle getBundle(java.lang.String baseName) public static ResourceBundle 

getBundle(java.lang.String baseName, Locale locale) For example: 

ResourceBundle rb = ResourceBundle.getBundle("MyResources", Locale.US); This will 

load theResourceBundle object with the values in the corresponding properties file.1.If a 

suitable properties file is not found, the ResourceBundle object will use the default 

properties file, whichwill be the one whose name equals the base name and has the 

properties extension. In this case, the defaultfile would be MyResources.properties. 2.If this 

file is not found, a java.util.MissingResourceException will bethrown. 

Note2:java.util.ResourceBundle class enables you to choose and read the properties file 

specific to the user'slocale and look up the values. 

A ResourceBundle object has a base name. In order for a ResourceBundle object to pick 

up a properties file,the filename must be composed of the ResourceBundle base name, 

followed by an underscore, followed bythe language code, and optionally followed by 

another underscore and the country code. 

The format for the properties file name is as follows: 

basename_languageCode_countryCode 

For example, suppose the base name is MyResources and you define the following three 

locales: 

US-en DE-de CN-zh Then you would have these three properties files: MyResources_en_US.properties MyResources_de_DE.properties MyResources_zh_CN.properties 

Reference:Reading Properties Files using ResourceBundle 

Q56. Given: And the commands: 

javac Test.java 

java ea Test 

What is the result? 

A. Compilation fails 

B. Standard Edition Enterprise Edition Micro Edition 

C. Standard Edition class java.lang.AssertionError Micro Edition 

D. Standard Edition is printed and an Assertion Error is thrown 

Answer:

Explanation: 

javac Test.java 

will compile the program. 

As for command line: 

java ea Test 

First the code will produce the output: 

Standard Edition 

See Note below. 

The ea option will enable assertions. This will make the following line in the switch 

statement to be run: 

default: assert false; 

This will throw an assertion error. This error will be caught. An the class of the assertion 

error (classjava.lang.AssertionError) will be printed by the following line: 

System.out.println(e.getClass()); 

Note:The java tool launches a Java application. It does this by starting a Java runtime 

environment, loading aspecified class, and invoking that class's main method. The method 

declaration must look like the following: 

public static void main(String args[]) 

Paramater ea: 

-enableassertions[:<package name>"..." | :<class name> ] -ea[:<package name>"..." | 

:<class name> ] 

Enable assertions. Assertions are disabled by default. With no arguments, 

enableassertions or -ea enablesassertions. 

Note 2: 

An assertion is a statement in the JavaTM programming language that enables you to test 

your assumptionsabout your program. 

Each assertion contains a boolean expression that you believe will be true when the 

assertion executes. If it isnot true, the system will throw an error. 

public class AssertionError extends Error 

Thrown to indicate that an assertion has failed. 

Note 3: 

The javac command compiles Java source code into Java bytecodes. You then use the 

Java interpreter - the 

java command - to interprete the Java bytecodes. 

Reference:java - the Java application launcher 

Reference:java.langClass AssertionError 

Q57. Given the code fragment: 

Which two try statements, when inserted at line ***, enable the code to successfully move the file info.txt to thedestination directory, even if a file by the same name already exists in the destination directory? 

A. try (FileChannel in = new FileInputStream (source). getChannel(); FileChannel out = 

new FileOutputStream 

(dest).getChannel()) { in.transferTo(0, in.size(), out); 

B. try ( Files.copy(Paths.get(source),Paths.get(dest)); 

Files.delete (Paths.get(source)); 

C. try ( Files.copy(Paths.get(source), 

Paths.get(dest),StandardCopyOption.REPLACE_EXISTING); Files.delete 

(Paths.get(source)); 

D. try (Files.move(Paths.get(source),Paths.get(dest)); 

E. try(BufferedReader br = Files.newBufferedReader(Paths.get(source), 

Charset.forName("UTF- 8")); 

BufferedWriter bw = Files.newBufferedWriter(Paths.get(dest), Charset.forName("UTF-8")); 

String record = 

""; 

while ((record = br.readLine()) ! = null) { 

bw.write(record); 

bw.newLine(); 

Files.delete(Paths.get(source)); 

Answer: C,E 

Explanation: 

A: copies only, don’t move operation 

B,C,D (no try-with-resource !) syntax change to: try { … 

B: throws FileAlreadyExistsException 

C: correct if syntax change to : StandardCopyOption.REPLACE_EXISTING (before 

REPLACE_Existing) 

D: throws FileAlreadyExistsException 

E: works properly if the sourcefile has the correct format, utf-8 here (else throws 

MalformedInputException) 

AND syntax is corrected to: 

try ( BufferedReader br = Files.newBufferedReader(Paths.get(source), 

Charset.forName(“UTF-8)); 

BufferedWriter bw = Files.newBufferedWriter(Paths.get(dest), Charset.forName(“UTF-8)); 

){ 

String record = “”; 

….. 

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

Q59. Given: What is the result? 

A. Three 

B. One 

C. Compilation fails. 

D. The program runs, but prints no output. 

Answer:

Explanation: 

push 

void push(E e) 

Pushes an element onto the stack represented by this deque (in other words, at the head 

of this deque) if it ispossible to do so immediately without violating capacity restrictions, 

returning true upon success and throwingan IllegalStateException if no space is currently 

available. 

This method is equivalent to addFirst(E). 

pop 

E pop() 

Pops an element from the stack represented by this deque. In other words, removes and 

returns the firstelement of this deque. 

This method is equivalent to removeFirst(). 

Returns: 

the element at the front of this deque (which is the top of the stack represented by this 

deque) 

Throws: 

NoSuchElementException - if this deque is empty 

Q60. Given two classes in separate files: 

Which two import statements can make the a.b.parent class compliable? 

A. import a.b.c.Parent; 

B. import a.b.c.Child; 

C. import a.b.c.*; 

D. import a.b.*; 

E. import a.*; 

Answer: B,C 

Explanation: 

To import a specific member into the current file, put an import statement at the beginning of thefile before any type definitions but after the package statement, if there is one.C:To import all the types contained in a particular package, use the import statement with the asterisk (*)wildcard character. 

Reference: The Java Tutorials,Using Package Members