1Z0-805 | Down to date 1Z0-805 Exam Study Guides With New Update Exam Questions


Q41. Which statement creates a low-overhead, low contention random number generator that is isolated to a thread to generate 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()*.nextInt(1, 101); 

D. int i = (int) Match.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 Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use ofThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use 

random numbers in parallel 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. 

Q42. Given the code fragment: 

public class TestString { 

public static void main(String[] args) { 

String str=null; 

switch(str) { 

case "": 

System.out.println("blank"); break; 

case "null": 

System.out.println("NULL"); break; 

default: 

System.out.println("invalid"); break; 

What is the result? 

A. Compilation fails 

B. Blank 

C. NULL 

D. An exception is thrown at runtime 

E. Invalid 

Answer:

Explanation: A java.lang.NullPointerException will be thrown at runtime at line: switch(str) { 

Ensure that the expression in any switch statement is not null to prevent a NullPointerException from being thrown. 

Reference: The Java Tutorials, The switch Statement 

Q43. Which three objects must a vendor provide implementations in its JDBC driver? 

A. Time 

B. Date 

C. Statement 

D. RuleSet 

E. Connection 

F. SQLException 

G. DriverManager 

Answer: E,F,G 

Explanation: E: When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application. 

F: An SQLException is an exception that provides information on a database access error or other errors. 

G: DriverManager is the basic service for managing a set of JDBC drivers. 

Reference: The Java Tutorials, Class DriverManager 

Q44. Given the code fragment: 

Locale loc1 = Locale.getDefault (); 

ResourceBundle messages = ResourceBundle.getBundle("MessageBundle", loc1); 

Which two statements are a valid way to re-assign a resource bundle to a different Locale? 

A. loc1 = ResourceBundle.getBundle ("MessageBundle", Locale.CHINA); 

B. loc1 = ResourceBundle.getBundle ("MessageBundle", new Locale ("es", "ES")); 

C. messages = ResourceBundle.getBundle ("messageBundle", new Locale ("es", "ES")); 

D. messages = ResourceBundle.getBundle ("MessageBundle", Locale.CHINA); 

Answer: C,D 

Q45. Given: 

final int n; 

if (n <= 1) 

return n; 

Fibonacci f1 = new Fibonacci (n – 1); 

return f2.compute() + f1.join; 

Suppose that lines X and Y are transposed: 

Fibonacci f2 = new Fibonacci (n – 2); // Line Y 

f1.fork; // Line X 

What is the likely result? 

A. The program produces the correct result, with similar performance to the original 

B. The program produces the correct result, with performance degraded to the equivalent of being single-threaded. 

C. The program produces an incorrect result 

D. The program goes into an infinite loop 

E. An exception is thrown at runtime 

F. The program produces the correct result, the better performance than the original. 

Answer:

Explanation: The degree of parallelism is not changed. Functionality is the same. 

Q46. In the Java SE7 API, which method is most commonly used by factories to instantiate objects? 

A. new () 

B. make () 

C. create () 

D. getObject () 

E. getInstance () 

F. createObject () 

Answer:

Explanation: For example: KeyFactory.getInstance, ObjectFactory. getObjectInstance. 

Q47. Given: 

private static void copyContents() { 

try ( 

InputStream fis = new FileInputStream("report1.txt"); 

OutputStream fos = new FileOutputStream("consolidate.txt"); 

) { 

byte[] buf = new byte[8192]; 

int i; 

while ((i = fis.read(buf)) != -1) { 

fos.write(buf, 0, i); 

fis.close(); 

fis = new FileInputStream("report2.txt"); 

while ((i = fis.read(buf)) != -1) { 

fos.write(buf, 0, i); 

What is the result? 

A. Compilation fails due to an error at line 28 

B. Compilation fails due to error at line 15 and 16 

C. The contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are appended to consolidate.txt, after a new line 

D. The contents of report1.txt are copied to consolidate.txt. The contents of report2.txt are appended to consolidate.txt, without a break in the flow. 

Answer:

Explanation: The auto-closable resource fis may not be assigned. 

Note: The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. 

Reference: The Java Tutorials,The try-with-resources Statement 

Q48. Given this code fragment: 

try { 

String query = "SELECT * FROM Item"; 

Statement stmt = conn.createStatement(); 

ResultSet rs = stmt.executeQuery(query); 

ResultSetMetaData rsmd = rs.getMetaData(); 

int rowCount = rsmd.getRowCount(); 

System.out.println ("Processing: " + rowCount + " rows."); 

while (rs.next()) { 

// Process each row 

} catch (SQLException se) { 

System.out.println("Error"); 

Assume that the SQL query returns records. What is the result? 

A. Compilation fails. 

B. The program prints Error 

C. An exception is thrown at runtime 

D. The statement at line 16 execute 

Answer:

Explanation: There is no GetRowCount method in java.sql.ResultSetMetaData. 

The following line will not compile: 

int rowCount = rsmd.getRowCount(); 

Reference: java.sql.ResultSetMetaData 

Q49. Given the code fragment: String query = "SELECT ID FROM Employee"; \ Line 1 

try (Statement stmt = conn.CreateStatement()) { \ Line 2 

ResultSet rs = stmt.executeQuery(query); \ Line 3 

stmt.executeQuery ("SELECT ID FROM Customer"); \ Line 4 

while (rs.next()) { 

\process the results 

System.out.println ("Employee ID: " + rs.getInt("ID") ); 

} catch (Exception e) { 

system.out.println ("Error"); 

Assume that the SQL queries return records. What is the result of compiling and executing this code fragment? 

A. The program prints employees IDs. 

B. The program prints customer IDs. 

C. The program prints Error. 

D. Compilation fails on line 13. 

Answer:

Explanation: Line 3 sets the resultset rs. rs will contain IDs from the employee table. 

Line 4 does not affect the resultset rs. It just returns a resultset (which is not used). 

Note: 

A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database. 

You access the data in a ResultSet object through a cursor. Note that this cursor is not a database cursor. This cursor is a pointer that points to one row of data in the ResultSet. 

Initially, the cursor is positioned before the first row. The method ResultSet.next moves the cursor to the next row. This method returns false if the cursor is positioned after the last row. This method repeatedly calls the ResultSet.next method with a while loop to iterate through all the data in the ResultSet. 

Reference: The Java Tutorials,Retrieving and Modifying Values from Result Sets 

Q50. Given: 

import java.io.*; 

public class SampleClass { 

public static void main(String[] args) throws IOException { 

try { 

String dirName = args[0]; 

File dir = new File(dirName); 

File.createTempFile("temp", "log", dir); 

} catch (NullPointerException | IOException e) { 

e = new IOException("Error while creating temp file"); 

throw e; 

What is the result? 

A. Compilation fails. 

B. An IOException with the default message is thrown at runtime. 

C. An IOException with the message Error while creating temp file is thrown at runtime. 

D. A temporary file is created in the specified directory. 

Answer:

Explanation: The multi-catch parameter e may not be assigned. The compilation will fail at 

line: 

e = new IOException("Error while creating temp file");