Q1. Given the integer implements comparable:
What is the result?
A. 4 1
B. 1 2
C. 32
D. 21
E. 2 3
Answer: D
Explanation:
binarySearch
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
Searches the specified list for the specified object using the binary search algorithm.
The list must be sorted into ascending order according to the natural ordering of its
elements (as by the sort(List) method) prior to making this call. If it is not sorted, the results
are undefined.
Parameters:
list - the list to be searched.
key - the key to be searched for.
Returns:
the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1).
Q2. 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
Q3. Given the code fragment:
What is the result?
A. Java 7
B. Java 6
C. Java 7, Java 6
D. Java 7 java 6
E. Java
Answer: C
Explanation:
regex: Java / one or more anything !!! / ends with a digit so it is the source string
Q4. 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
Q5. Given:
What is the result?
A. An exception is thrown at runtime on line 9.
B. An exception is thrown at runtime on line 12
C. onetwonull
D. onetwothree
E. twoonenull
F. threetwoone
Answer: D
Explanation:
addFirst void addFirst(E e) Inserts the specified element at the front of this deque if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted deque, it is generally preferable to
use method offerFirst
(E).
pollLast
E pollLast()
Retrieves and removes the last element of this deque, or returns null if this deque is empty.
Returns:
the tail of this deque, or null if this deque is empty
Q6. Given the database table:
And given this class:
Assume that the SQL integer queries are valid. What is the result of compiling and executing this codefragment?
A. A
B. B
C. C
D. D
E. E
Answer: C
Explanation:
The code will compile now.
The three rows with PRICE in between 5.5 and 9.5 will be displayed.
Q7. 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
Q8. 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()));
Answer: C
Q9. Given:
What is the result?
A. peach orange apple
B. peach orange
C. apple orange
D. The program does not compile.
E. The program generates an exception at runtime.
Answer: D
Explanation:
int compare(T obj1, T obj2) 0 if equal positive if obj1 greater negative if obj2 greater The compiler has a problem with the line: public boolean compare(String s1, String s2) { return s1.length() > s2.length(); error: <anonymous comparetest.CompareTest$1> is not abstract and does not override abstract method compare(String,String) in Comparator
new Comparator<String>() {
Error: compare(String,String) in <anonymous comparetest.CompareTest$1> cannot
implement compare(T,T)
in Comparator
public boolean compare(String s1, String s2) {
return type boolean is not compatible with int
where T is a type-variable:
T extends Object declared in interface Comparator
Q10. Given the directory structure that contains three directories: company, Salesdat, and Finance:
Company
-Salesdat
* Target.dat
-Finance
*
Salary.dat
*
Annual.dat
And the code fragment: If Company is the current directory, what is the result?
A. Prints only Annual.dat
B. Prints only Salesdat, Annual.dat
C. Prints only Annual.dat, Salary.dat, Target.dat
D. Prints at least Salesdat, Annual.dat, Salary.dat, Target.dat
Answer: A
Explanation:
IF !! return FileVisitResult.CONTINUE;
The pattern *dat will match the directory name Salesdat and it will also match the file
Annual.dat.
It will not be matched to Target.dat which is in a subdirectory.