Ensurepass

QUESTION 11

Given the code fragment:

 

clip_image002

 

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.

 

Correct Answer: C

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.

 

 

QUESTION 12

Given the code fragment:

 

clip_image004

 

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.

 

Correct Answer: C

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.

 

 

QUESTION 13

How many Threads are created when passing task to an Executor instance?

 

A.

A new Thread is used for each task.

B.

A number of Threads equal to the number of CPUs Is used to execute tasks.

C.

A single Thread Is used to execute all tasks.

D.

A developer-defined number of Threads is used to execute tasks.

E.

A number of Threads determined by system load is used to execute tasks.

F.

The method used to obtain the Executor determines how many Threads are used to execute tasks.

 

Correct Answer: F

Explanation:

The Executor interface provides a single method, execute, designed to be a drop-in replacementfor a common thread-creation idiom. If r is a Runnable object, and e is an Executor object you can replace(new Thread(r)).start();

with

e.execute(r);

However, the definition of execute is less specific. The low-level idiom creates a new thread and launches it immediately. Depending on the Executor implementation, execute may do the same thing, but is more likely to use an existing worker thread to run r, or to place r in a queue to wait for a worker thread to become available.

Reference: The Java Tutorial,The Executor Interface

 

 

QUESTION 14

Given the code fragment:

 

SimpleDataFormat sdf;

 

Which code fragment displays the three-character month abbreviation?

 

A.

SimpleDateFormat sdf = new SimpleDateFormat (“mm”, Locale.UK); System.out.println (“Result:” + sdf.format(new Date()));

B.

SimpleDateFormat sdf = new SimpleDateFormat (“MM”, Locale.UK); System.out.println (“Result:” + sdf.format(new Date()));

C.

SimpleDateFormat sdf = new SimpleDateFormat (“MMM”, Locale.UK); System.out.println (“Result:” + sdf.format(new Date()));

D.

SimpleDateFormat sdf = new SimpleDateFormat (“MMMM”, Locale.UK); System.out.println (“Result:” + sdf.format(new Date()));

 

Correct Answer: C

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

QUESTION 15

Given this code fragment:

 

clip_image006

 

Assume that the SQL query returns records.

 

What is the result?

 

A.

Compilation fails due to error at line 17

B.

The program prints Error

C.

The program prints each record

D.

Compilation fails at line 14

 

Correct Answer: C

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

QUESTION 16

Given:

 

clip_image008

 

Which two are true about the lines labeled A through D?

 

A.

The code compiles and runs as is.

B.

If only line A is removed, the code will compile and run.

C.

If only line B is removed, the code will compile and run.

D.

If only line D is removed, the code will compile and run.

E.

Line C is optional to allow the code to compile and run.

F.

Line C is mandatory to allow the code to compile andrun.

 

Correct Answer: AE

Explanation:

A: The code will compile. The abstract method doDock() is implemented fine, and doFloat() isoverridden.

E: Line C overrides the implementation of doFloat(). This is optional.

 

 

QUESTION 17

Which three are true?

 

A.

A setAutoCommit (False) method invocation starts a transaction context.

B.

An instance of Savepoint represents a point in the current transaction context.

C.

A rollback () method invocation rolls a transaction back to the last savepoint.

D.

A rollback () method invocation releases any database locks currently held by this connection object.

E.

After calling rollback (mysavepoint), you must close the savepoint object by calling mySavepoint.close() .

 

Correct Answer: ABC

Explanation:

A:The way to allow two or more statements to be grouped into a transaction is to disable the auto-commitmode. After the auto-commit mode is disabled, no SQL statements are committed until you call the methodcommit explicitly. All statements executed after the previous call to the method commit are included in thecurrent transaction and committed together as a unit.

Note:When a connection is created, it is in auto-commit mode. This means that each individual SQL statementis treated as a transaction and is automatically committed right after it is executed. (To be more precise, thedefault is for a SQL statement to be committed when it is completed, not when it is executed. A statement iscompleted when all of its result sets and update counts have been retrieved. In almost all cases, however, astatement is completed, and therefore committed, right after it is executed.)

B:The method Connection.setSavepoint, sets a Savepoint object within the current transaction. The Connection.rollback method is overloaded to take a Savepoint argument. When a transaction is rolled back toa savepoint all changes made after that savepoint are undone.

C: calling the method rollback terminates a transaction and returns any values that were modified to theirprevious values. If you are trying to execute one or more statements in a transaction and get a SQLException, call the method rollback to end the transaction and start the transaction all over again.

 

 

QUESTION 18

Given:

 

clip_image010

 

What is the result?

 

A.

Nice to see you,be fine

B.

Nice,see you,be fine

C.

Nice,see you, to, be fine

D.

Nice, see you, be fine

E.

Nice to see y, u, be fine

 

Correct Answer: A

Explanation:

The text “,to,” is replaced by the “,”

 

 

 

 

 

 

 

 

 

QUESTION 19

Given that myfile.txt contains:

 

clip_image012

 

What is the result?

 

A.

new file.txt contains:

1: First

2: Second

3: Third

B.

new file.txt contains:

1: First 2: Second 3: Third

C.

newfile.txt is empty

D.

an exception is thrown at runtime

E.

compilation fails

 

Correct Answer: A

Explanation:

For each line in the file myfile.text the line number and the line is written into newfile.txt.

 

 

 

 

 

QUESTION 20

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;

 

Correct Answer: A

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

 

Free VCE & PDF File for Oracle 1Z0-804 Real Exam

Instant Access to Free VCE Files: CompTIA | VMware | SAP …
Instant Access to Free PDF Files: CompTIA | VMware | SAP …

Comments are closed.