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?
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?
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?
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?
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?
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?
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?
Answer : C
Given the code fragment:
What is the result of the employees table has no records before the code executed?
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?
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?
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?
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?
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?
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?
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?
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