Q1. 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.
Q2. Given:
And the command-line invocation:
Java Tracker 12 11
What is the result?
A. General category
B. class InvalidAgeException
C. class java.lang.IllegalArgumentException
D. class java.lang.RuntimeException
Answer: B
Explanation:
The second argument 11 makes the program to throw an InvalidAgeException due to the
line:
if (age < 12)
throw new InvalidAgeException ();
Q3. Give: 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 singlethreaded.
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, with better performance than the original.
Answer: D
Explanation:
join() does not proceed until the task's result has been computed. Here we start to wait beforedoing the computing. The code will not finish.
Q4. Which two compile?
A. interface Compilable {
void compile();
}
B. interface Compilable {
final void compile();
}
C. interface Compilable {
static void compile();
}
D. interface Compilable { abstract void compile(); }
E. interface Compilable { protected abstract void compile (); }
Answer: A,D
Q5. Which method would you supply to a class implementing the Callable interface?
A. callable ()
B. executable ()
C. call ()
D. run ()
E. start ()
Answer: C
Explanation:
public interface Callable<V>
A task that returns a result and may throw an exception. Implementors define a single
method with noarguments called call.
Note:
Interface Callable<V>
Type Parameters:
V - the result type of method call
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.
The Executors class contains utility methods to convert from other common forms to
Callable classes.
Reference:java.util.concurrent
Q6. Given:
ConcurrentMap <String, String> PartList = new ConcurrentMap<>();
Which fragment puts a key/value pair in partList without the responsibility of overwriting an existing key?
A. partList.out(key,"Blue Shirt");
B. partList.putIfAbsent(key,"Blue Shirt");
C. partList.putIfNotLocked (key,"Blue Shirt");
D. partList.putAtomic(key,"Blue Shirt")
E. if (!partList.containsKey(key)) partList.put (key,"Blue Shirt");
Answer: B
Explanation:
putIfAbsent(K key, V value)
If the specified key is not already associated with a value, associate it with the given value.
Reference:java.util.concurrent,Interface ConcurrentMap<K,V>
Q7. Given the following incorrect program:
Which two changes make the program work correctly?
A. Results must be retrieved from the newly created MyTask instances and combined.
B. The threshold value must be increased so that the overhead of task creation does not dominate the cost ofcomputation.
C. The midpoint computation must be altered so that it splits the workload in an optimal manner.
D. The compute () method must be changed to return an Integer result.
E. The compute () method must be enhanced to (fork) newly created tasks.
F. The myTask class must be modified to extend RecursiveAction instead of RecursiveTask
Answer: A,D
Explanation:
Note 1: A RecursiveTask is a recursive result-bearing ForkJoinTask. Note 2: The invokeAll(ForkJoinTask<?>… tasks) forks the given tasks, returning when isDone holds for eachtask or an (unchecked) exception is encountered, in which case the exception is rethrown. Note 3: Using the fork/join framework is simple. The first step is to write some code that performs a segmentof the work. Your code should look similar to this: if (my portion of the work is small enough) do the work directly else split my work into two pieces invoke the two pieces and wait for the results Wrap this code as a ForkJoinTask subclass, typically as one of its more specialized types RecursiveTask (which can return a result) or RecursiveAction.
Q8. Given:
What is the result?
A. false \sales\quarter\ . . \qtrlreport.txt
B. false \quarter\ . . \qtrlreport.txt
C. true . . \ . . \ . . \ annualreport.txt
D. true \ . . \ . . \annualreport.txt
Answer: A
Explanation:
( richtig !! import java.nio.file.Path; import java.nio.file.Paths; ) original-Aufgabe war ( falsch >> import java.io.file.Path; import java.io.file.Paths; ) The relativize method that can be used to construct a relative path between two paths. relativize Path relativize(Path other) Constructs a relative path between this path and a given path. Parameters:other - the path to relativize against this path Returns:the resulting relative path, or an empty path if both paths are equal Note: Relativization is the inverse of resolution. This method attempts to construct a relative path that when resolvedagainst this path, yields a path that locates the same file as the given path. For18example, on UNIX, if this path is "/a/b" and the given path is "/a/b/c/d" then the resulting relative path would be"c/d". Where this path and the given path do not have a root component, then a relative path can beconstructed. A relative path cannot be constructed if only one of the paths have a root component. Where bothpaths have a root component then it is implementation dependent if a relative path can be constructed. If thispath and the given path are equal then an empty path is returned. For any two normalized paths p and q, where q does not have a root component,p.relativize(p.resolve(q)).equals(q) When symbolic links are supported, then whether the resulting path, when resolved against this path, yields apath that can be used to locate the same file as other is implementation dependent. For example, if this path is"/a/b" and the given path is "/a/x" then the resulting relative path may be "../x". If "b" is a symbolic link then isimplementation dependent if "a/b/../x" would locate the same file as "/a/x".
Q9. Given the code fragment: What is the result, if the file myfile.txt does not exist?
A. A runtime exception is thrown at line 4
B. A runtime exception is thrown at line 7
C. Creates a new file and prints no output
D. Compilation fails
Answer: D
Explanation:
!! Compilation fails if FileNotFoundException is tried to catch (Line 12)
(The exception FileNotFoundException is already caught by the alternative IOException)
if this is removed will be thrown a FileNotFoundException at line 4.
Q10. Given the code fragment: Which two try statements, when inserted at line ***, enable you to print files with the extensions.java, .htm, and.jar.
A. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,"*.{java,htm,jar}")){
B. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,"*. [java,htm,jar]")) {
C. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,"*.{java*,htm*,jar*}")) {
D. try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir,"**.{java,htm,jar}")) {
Answer: A,D
Explanation:
"*. {java,htm,jar} and
"**. {java,htm,jar} will match any file with file endings java, htm, or jar.
Q11. 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.
Q12. A valid reason to declare a class as abstract is to:
A. define methods within a parent class, which may not be overridden in a child class
B. define common method signatures in a class, while forcing child classes to contain unique methodimplementations
C. prevent instance variables from being accessed
D. prevent a class from being extended
E. define a class that prevents variable state from being stored when object Instances are serialized
F. define a class with methods that cannot be concurrently called by multiple threads
Answer: B
Explanation:
Note:An abstract method in Java is something like a pure virtual function in C++ (i.e., a virtualfunction that is declared = 0). In C++, a class that contains a pure virtual function is called an abstract classand cannot be instantiated. The same is true of Java classes that contain abstract methods. Any class with an abstract method is automatically abstract itself and must be declared as such. An abstract class cannot be instantiated. A subclass of an abstract class can be instantiated only if it overrides each of the abstract methods of itssuperclass and provides an implementation (i.e., a method body) for all of them. Such a class is often called aconcrete subclass, to emphasize the fact that it is not abstract. If a subclass of an abstract class does not implement all the abstract methods it inherits, that subclass is itselfabstract.static, private, and final methods cannot be abstract, since these types of methods cannot be overridden by asubclass. Similarly, a final class cannot contain any abstract methods. A class can be declared abstract even if it does not actually have any abstract methods. Declaring such a classabstract indicates that the implementation is somehow incomplete and is meant to serve as a superclass forone or more subclasses that will complete the implementation. Such a class cannot be instantiated.
Q13. Given the code fragment:
Assume the method printNums is passed a valid array containing data. Why is this method not producingoutput on the console?
A. There is a compilation error.
B. There is a runtime exception.
C. The variable number is not initialized.
D. Standard error is mapped to another destination.
Answer: D
Explanation:
The code compiles fine.
The code runs fine.
The errorstream can be redirected.
Note:
System.out.println -> Sends the output to a standard output stream. Generally monitor.
System.err.println -> Sends the output to a standard error stream. Generally monitor. err is
the "standard" erroroutput stream. This stream is already open and ready to accept output
data.
Typically this stream corresponds to display output or another output destination specified
by the hostenvironment or user. By convention, this output stream is used to display error
messages or other informationthat should come to the immediate attention of a user even if the principal output stream, the value of thevariable out, has been redirected to a file or other destination that is typically not continuously monitored.
Reference:java.lang.System
Q14. Given:
What is the result?
A. riding riding tolting
B. riding riding cantering
C. tolting cantering tolting
D. tolting cantering cantering
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: E
Explanation:
The compilation fails at:
interface Rideable {
public String ride() { return "riding ";}
}
Error due to: interface methods cannot have body.
Q15. Given the fragment:
Which two valid alternatives to line 3 would decouple this application from a specific implementation ofCustomerDAO?
A. CustomerDAO custDao = CustomerDAO();
B. CustomerDAO custDao = (CustomerDAO) new Object ();
C. CustomerDAO custDao = CustomerDAO.getInstance();
D. CustomerDAO custDao = (CustomerDAO) new CustomerDAOmemoryImp1();
E. CustomerDAO custDao = customerDAOFactory.getInstance();
Answer: C,E
Explanation:
Note: In software development, the term"decoupling"is used to identify the separation of software blocks thatshouldn't depend on each other. Some building blocks are generic and shouldn't know details of others. Special design techniques allow software designers to have as few dependencies as possible. This typicallyreduces the risk of malfunction in one part of a system when the other part changed. It also forces thedeveloper to focus on one thing at a time. Decoupling lowers or minimizes Coupling.