Java SE 7 Programmer II v8.0 (1z0-804)

Page:    1 / 10   
Total 150 questions

Given the following code fragment:
10. p1 = paths.get("report.txt");
11. p2 = paths.get("company");
12. / / insert code here
Which code fragment, when inserted independently at line 12, move the report.txt file to the company directory,at the same level, replacing the file if it already exists?

  • A. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
  • B. Files.move(p1, p2, StandardCopyOption.REPLACE_Existing, LinkOption.NOFOLLOW_LINKS);
  • C. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS);
  • D. Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.copy_ATTRIBUTES, StandrardCopyOp)
  • E. Files.move (p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.copy_ATTRIBUTES, LinkOption.NOF)


Answer : A,C

Explanation:
Moving a file is equally as straight forward
move(Path source, Path target, CopyOption... options);
The available StandardCopyOptions enums available are:
StandardCopyOption.REPLACE_EXISTING

StandardCopyOption.ATOMIC_MOVE -
If Files.move is called with StandardCopyOption.COPY_ATTRIBUTES an
UnsupportedOperationException isthrown.

Given the code fragment:
What is the result when the result.txt file already exists in c:\student?

  • A. The program replaces the file contents and the file's attributes and prints Equal.
  • B. The program replaces the file contents as well as the file attributes and prints Not equal.
  • C. An UnsupportedOperationException is thrown at runtime.
  • D. The program replaces only the file attributes and prints Not equal.


Answer : B

Explanation:
Assuming there is a file D:\\faculty\\report.txt then this file will be copied and will be replacing
C:\\student\\report.txt.

An application is waiting for notification of changes to a tmp directory using the following code statements:
Path dir = Paths.get("tmp")
WatchKey key = dir.register (watcher, ENTRY_CREATE, ENTRY_DELETE,
ENTRY_MODIFY) ;
In the tmp directory, the user renames the file testA to testB,
Which statement is true?

  • A. The events received and the order of events are consistent across all platforms.
  • B. The events received and the order of events are consistent across all Microsoft Windows versions.
  • C. The events received and the order of events are consistent across all UNIX platforms.
  • D. The events received and the order of events are platform dependent.


Answer : A

Explanation:
Most file system implementations have native support for file change notification. The
WatchService API takesadvantage of this support where available.
However, when a file system does not support this mechanism, the WatchService will poll the file system,waiting for events.
Note:
WatchKey : When a Watchable entity is registered with a WatchService a key which is a
WatchKey isgenerated. Initially the key is in ready state waiting to be notified of any events on the Watchable entity. Oncean event occurs the key goes into signaled state and allows to access the events using its pollEvents method.
After processing the poll events the key has to be reset by invoking its reset method.
Reference: The Java Tutorials,Watching a Directory for Changes

Given:
What is the result?

  • A. doc
  • B. index.html
  • C. an IllegalArgumentException is thrown at runtime.
  • D. An InvalidPthException is thrown at runtime.
  • E. Compilation fails.


Answer : B

Explanation:
p.getName(int location) = returns path' name element by index/location (starts with 0)
Example:
path = "project//doc//index.html"
p.getName(0) = project
p.getName(1) = doc
p.getName(2) = index.html

Given the code fragment:
What is the result?

  • A. getName (0): C:\ subpath (0, 2): C:\education\report.txt
  • B. getName(0): C:\ subpth(0, 2): C:\education
  • C. getName(0): education subpath (0, 2): education\institute
  • D. getName(0): education subpath(0, 2): education\institute\student
  • E. getName(0): report.txt subpath(0, 2): insritute\student


Answer : C

Explanation:
Example:
Path path = Paths.get("C:\\home\\joe\\foo");
getName(0)
-> home
subpath(0,2)
Reference: The Java Tutorial, Path Operations

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 : C

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.

Which code fragment demonstrates the proper way to handle JDBC resources?

  • A. try { ResultSet rs = stmt.executeQuery (query); statement stmt = con.createStatement(); while (rs.next()) (/* . . . */) } catch (SQLException e) {}
  • B. try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery (query); while (rs.next()) (/* . . . */) } catch (SQLException e) {}
  • C. try { Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery (query); while (rs.next()) (/* . . . */) } finally { rs.close(); stmt.close(); }
  • D. try { ResultSet rs = stmt.executeQuery (query); Statement stmt = con.createStatement(); while (rs.next()) (/* . . . */) } finally { rs.close(); stmt.close(); }


Answer : C

Given the code fragment:
What is the result of the employees table has no records before the code executed?

  • A. 1 Sam
  • B. 4 Jack
  • C. 3 John 4 Jack
  • D. 1 Sam 3 John 4 Jack


Answer : C

Explanation:
AutoCommit is set to false. The two following statements will be within the same transaction. stmt.executeUpdate("insert into employees values(1,'Sam')"); stmt.executeUpdate("insert into employees values(2,'Jane')");
These two statements are rolled-back through (the savepoint is ignored! the savepoint must be specified (e.g. conn.rollback(save1); ) in the rollback if you want to rollback to the savepoint): conn.rollback() ;
The next two insert statements are executed fine. Their result will be in the output.

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

  • A. The program prints employee IDs
  • B. The program prints customer IDs
  • C. The program prints Error
  • D. Compilation fails on line ***


Answer : C

Explanation:
!!! The given Code prints Error -- the second query clears the ResultSet !? ErrorMessage:
Operation notallowed after ResultSet closed
It would print A, if second Query i set to rs = stmt.executeQuery("SELECT ID FROM
Customer"); // Line ***
It would print B, if Line *** is missing. //
The program compiles and runs fine. Both executeQuery statements will run. The first executeQuery statement
(ResultSet rs = stmt.executeQuery(query);) will set the rs Resultset. It will be used in the while loop. EmployIDswill be printed.
Note:
Executes the given SQL statement, which returns a single ResultSet object.
Parameters:sql - an SQL statement to be sent to the database, typically a static SQL
SELECT statement Returns:a ResultSet object that contains the data produced by the given query; never null

Which two actions can be used in registering a JDBC 3.0 driver?

  • A. Add the driver class to the META-INF/services folder of the JAR file.
  • B. Set the driver class name by using the jdbc.drivers system property.
  • C. Include the JDBC driver class in a jdbcproperties file.
  • D. Use the java.lang.class.forName method to load the driver class.
  • E. Use the DriverManager.getDriver method to load the driver class.


Answer : A,D

Explanation:
A: if your JDBC Driver is NOT JDBC 4-compliant then we can update the driver using "jar"- utility by adding the
"META-INF /services/java.sql.Driver" inside it. as following:
D:Dynamic loading of Java classes at runtime provides tremendous flexibility in the development of enterprisesystems. It provides for the basis of "application servers", and allows even simpler, lighter-weight systems toaccomplish some of the same ends. Within
Java, dynamic-loading is typically achieved by calling the forNamemethod on the class java.lang.ClassAn example provided by the standard Java SE API is the ServiceLoader.
Amongothers, the JDBC 4.0compatible drivers implement this. This way just dropping the
JDBC driver JAR file folder will automatically loadthe driver class during Java application's startup/initialization without the need for any manual Class.forName("com.example.Driver") line in your code.

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

Which two statements are true about RowSet subinterfaces?

  • A. A JdbcRowSet object provides a JavaBean view of a result set.
  • B. A CachedRowSet provides a connected view of the database.
  • C. A FilteredRowSet object filter can be modified at any time.
  • D. A WebRowSet returns JSON-formatted data.


Answer : A,C

Explanation:
A: a JdbcRowSet object can be one of the Beans that a tool makes available for composing an application.
Because a JdbcRowSet is a connected RowSet, that is, it continually maintains its connection to a databaseusing a JDBC technology-enabled driver, it also effectively makes the driver a JavaBeans component.
C: The FilteredRowSet range criterion can be modified by applying a new Predicate object to the
FilteredRowSet instance at any time. This is possible if no additional references to the
FilteredRowSet objectare detected. A new filter has an immediate effect on criterion enforcement within the FilteredRowSet object,and all subsequent views and updates will be subject to similar enforcement.
Reference: javax.sql Interface RowSet

Which code fragment is required to load a JDBC 3.0 driver?

  • A. DriverManager.loadDriver ("org.xyzdata.jdbc.NetworkDriver");
  • B. Class.forName("org.xyzdata.jdbc-NetworkDriver");
  • C. Connection con = Connection.getDriver ("jdbc:xyzdata://localhost:3306/EmployeeDB");
  • D. Connection con = DriverManager.getConnection ("jdbc:xyzdata://localhost:3306/EmployeeDB");


Answer : B

Explanation:
In previous versions (prior to 4.0) of JDBC, to obtain a connection, you first had to initialize your JDBCdriver by calling the method Class.forName. This methods required an object of type java.sql.Driver.
Note:
DriverManager: This fully implemented class connects an application to a data source, which is specified by adatabase URL. When this class first attempts to establish a connection, it automatically loads any JDBC 4.0drivers found within the class path. Note that your application must manually load any JDBC drivers prior toversion 4.0.

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


Answer : A,B,C

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.

Given the code fragment:
Assume that the SQL query matches one record. What is the result of compiling and executing this code?

  • A. The code prints Error.
  • B. The code prints the employee ID.
  • C. Compilation fails due to an error at line 13.
  • D. Compilation fails due to an error at line 14.


Answer : A

Explanation:
The code compiles fine.
A: prints Error: rs.next() fehlt !! Fehlermeldung: Before start of result set mit rs.next() Aufruf : The code would run fine. public int getInt(String columnName) throws SQLException
Retrieves the value of the designated column in the current row of this ResultSet object as an int in the Javaprogramming language

Page:    1 / 10   
Total 150 questions