25 questions
Given that you are working with a JDBC 4.0 driver, which three are required for this JDBC driver to be compliant?
Must include a META-INF/services/java.sql.Driver file
Must provide implementations of Driver, Connection, Statement, and ResultSet interfaces
Must support scrollable ResultSets
Must support PreparedStatement and CallableStatement
Must support transactions
Which three are available through an instance of DatabaseMetaData?
The number of rows returned
The name of the JDBC driver
The default transaction isolation level
The last query used
The names of stored procedures in the database
Given this code fragment:
Statement stmt = conn.createStatement();
ResultSet rs;
String query = "<QUERY HERE>";
stmt.execute(query);
if ((rs = stmt.getResultSet()) != null) {
System.out.println("Results");
}
if (stmt.getUpdateCount() > -1) {
System.out.println("Update");
}
Which query statements entered into <QUERY HERE> produce the output that follows the query string (in the following answer), assuming each query is valid? (Choose all that apply.)
"SELECT * FROM Customer"
Results
. "INSERT INTO Book VALUES ('1023456789', 'One Night in Paris', '1984-10-20',
'Hardcover', 13.95)"
Update
"UPDATE Customer SET Phone = '555-234-1021' WHERE CustomerID = 101"
Update
"SELECT Author.LastName FROM Author"
Results
. "DELETE FROM Book WHERE ISBN = '1023456789'"
Update
Given:
String call = "{CALL REMOVEBOOKS(?, ?)}";
String titleToRemove = null;
int maxBooks = 0;
CallableStatement cstmt = conn.prepareCall(call);
String titles = "%Hero%";
int numBooksRemoved;
// Code added here
If REMOVEBOOKS is a stored procedure that takes an INOUT integer parameter as its first argument and an IN String parameter as its second argument, which code blocks, when placed at the line
// Code added here, could correctly execute the stored procedure and return a result?
cstmt.setInt(0, maxBooks);
cstmt.setString(1, titleToRemove);
cstmt.registerOutParameter(0, java.sql.Types.INTEGER);
cstmt.execute();
numBooksRemoved = cstmt.getInt(0);
cstmt.setInt(1, maxBooks);
cstmt.setString(2, titleToRemove);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.executeQuery(query);
numBooksRemoved = cstmt.getInt(1);
cstmt.setInt(1, maxBooks);
cstmt.setString(2, titleToRemove);
cstmt.execute();
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
numBooksRemoved = cstmt.getInt(1);
cstmt.setInt(1, maxBooks);
cstmt.setString(2, titleToRemove);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
ResultSet rs = cstmt.executeQuery();
rs.next();
numbBooks = rs.getInt(1);
cstmt.setInt(1, maxBooks);
cstmt.setString(2, titleToRemove);
cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
cstmt.execute();
numBooksRemoved = cstmt.getInt(1);
Assume you have a class that holds two private variables: a and b. Which of the following pairs can prevent concurrent access problems in that class? (Choose all that apply.)
public synchronized int read(){return a+b;}
public synchronized void set(int a, int b){this.a=a;this.b=b;}
public int read(){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}
public int read(){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(b){this.a=a;this.b=b;}}
public synchronized(this) int read(){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}
public int read(){synchronized(this){return a+b;}}
public void set(int a, int b){synchronized(this){this.a=a;this.b=b;}}
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}
Which letters will eventually appear somewhere in the output? (Choose all that apply.)
A
C
D
E
F
This class is intended to allow users to write a series of messages so that
each message is identified with a timestamp and the name of the thread that wrote the message:
public class Logger {
private StringBuilder contents = new StringBuilder();
public void log(String message) {
contents.append(System.currentTimeMillis());
contents.append(": ");
contents.append(Thread.currentThread().getName());
contents.append(message);
contents.append("\n");
}
public String getContents() { return contents.toString(); }
}
How can we ensure that instances of this class can be safely used by multiple threads?
This class is already thread-safe
Replacing StringBuilder with StringBuffer will make this class thread-safe
Synchronize the log() method only
Synchronize the getContents() method only
Synchronize both log() and getContents()
Given:
public class TestObj {
public static void main(String[] args) {
Object o = new Object() {
public boolean equals(Object obj) {
return true;
}
}
System.out.println(o.equals("Fred"));
}
}
What is the result?
An exception occurs at runtime
Compilation fails because of an error on line 3
Compilation fails because of an error on line 4
Compilation fails because of an error on line 8
Compilation fails because of an error on a line other than 3, 4, or 8
Given:
public class Car {
class Engine {
// insert code here
}
public static void main(String[] args) {
new Car().go();
}
void go() {
new Engine();
}
void drive() { System.out.println("hi"); }
}
Which, inserted independently at line 5, produce the output "hi"? (Choose all that apply.)
{ Car.drive(); }
{ this.drive(); }
{ Car.this.drive(); }
Engine() { this.drive(); }
Engine() { Car.this.drive(); }
Given:
import java.util.*;
public class Pockets {
public static void main(String[] args) {
String[] sa = {"nickel", "button", "key", "lint"};
Sorter s = new Sorter();
for(String s2: sa) System.out.print(s2 + " ");
Arrays.sort(sa,s);
System.out.println();
for(String s2: sa) System.out.print(s2 + " ");
}
class Sorter implements Comparator<String> {
public int compare(String a, String b) {
return b.compareTo(a);
}
}
}
What is the result?
Compilation fails
button key lint nickel
nickel lint key button
nickel button key lint
button key lint nickel
nickel button key lint
nickel lint key button
An exception is thrown at runtime
Given:
public static void main(String[] args) {
// INSERT DECLARATION HERE
for (int i = 0; i <= 10; i++) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j <= 10; j++)
row.add(i * j);
table.add(row);
}
for (List<Integer> row : table)
System.out.println(row);
}
Which statements could be inserted at // INSERT DECLARATION HERE to allow this code to
compile and run? (Choose all that apply.)
List<List<Integer>> table = new List<List<Integer>>();
List<List<Integer>> table = new ArrayList<List<Integer>>();
List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();
List<List, Integer> table = new ArrayList<List, Integer>();
None of the above
Given:
import java.util.*;
public class GeoCache {
public static void main(String[] args) {
String[] s = {"map", "pen", "marble", "key"};
Othello o = new Othello();
Arrays.sort(s,o);
for(String s2: s) System.out.print(s2 + " ");
System.out.println(Arrays.binarySearch(s, "map"));
}
static class Othello implements Comparator<String> {
public int compare(String a, String b) { return b.compareTo(a); }
}
}
Which are true? (Choose all that apply.)
Compilation fails
The output will contain a –1
An exception is thrown at runtime
The output will contain "key map marble pen"
The output will contain "pen marble map key"
Given the proper import statement(s) and:
TreeSet<String> s = new TreeSet<String>();
TreeSet<String> subs = new TreeSet<String>();
s.add("a"); s.add("b"); s.add("c"); s.add("d"); s.add("e");
subs = (TreeSet)s.subSet("b", true, "d", true);
s.add("g");
s.pollFirst();
s.pollFirst();
s.add("c2");
System.out.println(s.size() +" "+ subs.size());
Which are true? (Choose all that apply.)
The size of s is 4
The size of s is 5
The size of subs is 1
The size of subs is 2
The size of subs is 3
import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}
And the command line:
java Regex2 "\d*" ab34ef
What is the result?
334
2334
0123456
01234456
12334567
Given:
public class Stone {
public static void main(String[] args) {
String s = "abc";
System.out.println(">" + doStuff(s) + "<");
}
static String doStuff(String s) {
s = s.concat(" ef h ");
return s.trim();
}
}
What is the result?
>abcefh<
>efhabc<
>abc ef h<
\>>ef h abc<
>abc ef h <
Given:
import java.util.regex.*;
public class Archie {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
int count = 0;
while(m.find())
count++;
System.out.print(count);
}
}
And given the command-line invocation:
java Archie "\d+" ab2c4d67
What is the result?
0
3
4
8
Compilation fails
public class MultipleResources {
class Lamb implements AutoCloseable {
public void close() throws Exception {
System.out.print("l");
} }
class Goat implements AutoCloseable {
public void close() throws Exception {
System.out.print("g");
} }
public static void main(String[] args) throws Exception {
new MultipleResources().run();
}
public void run() throws Exception {
try (Lamb l = new Lamb();
System.out.print("t");
Goat g = new Goat();) {
System.out.print("2");
} finally {
System.out.print("f");
} } }
What is the result?
2lgf
tglf
None of the above; main() throws an exception
Compilation fails
t2lgf
Given:
public class Animals {
class Lamb implements AutoCloseable {
public void close() {
throw new RuntimeException("a");
} }
public static void main(String[] args) throws IOException {
new Animals().run();
}
public void run() throws IOException {
try (Lamb l = new Lamb();) {
throw new IOException();
} catch(Exception e) {
throw e;
} } }
Which exceptions will the code throw?
IOException with suppressed RuntimeException a
RuntimeException c with suppressed RuntimeException a
IOException with suppressed RuntimeException c
RuntimeException a with suppressed RuntimeException c
Compilation fails
Given:
try { int x = Integer.parseInt("two"); }
Which could be used to create an appropriate catch block? (Choose all that apply.)
ClassCastException
IllegalStateException
NumberFormatException
IllegalArgumentException
ExceptionInInitializerError
Given:
public class Gotcha {
public static void main(String[] args) {
// insert code here
}
void go() {
go();
}
}
And given the following three code fragments:
I. new Gotcha().go();
II. try { new Gotcha().go(); }
catch (Error e) { System.out.println("ouch"); }
III. try { new Gotcha().go(); }
catch (Exception e) { System.out.println("ouch"); }
When fragments I–III are added, independently, at line 5, which are true?
(Choose all that apply.)
They will all compile
All will complete normally
None will complete normally
Some will not compile
Only one will complete normally
Given:
public class Frisbee {
// insert code here
int x = 0;
System.out.println(7/x);
5. }
6. }
And given the following four code fragments:
I. public static void main(String[] args) {
II. public static void main(String[] args) throws Exception {
III. public static void main(String[] args) throws IOException {
IV. public static void main(String[] args) throws RuntimeException {
If the four fragments are inserted independently at line 2, which are true? (Choose all that apply.)
All four will compile and execute without exception
All four will compile and execute and throw an exception
Some, but not all, will compile and execute without exception
Some, but not all, will compile and execute and throw an exception
When considering fragments II, III, and IV, of those that will compile, adding a try/catch block around line 4 will cause compilation to fail
Given:
public class Circles {
public static void main(String[] args) {
int[] ia = {1,3,5,7,9};
for(int x : ia) {
for(int j = 0; j < 3; j++) {
if(x > 4 && x < 8) continue;
System.out.print(" " + x);
if(j == 1) break;
continue;
}
continue;
}
}
}
What is the result?
1 3 9
5 5 7 7
1 3 3 9 9
1 1 3 3 9 9
1 1 1 3 3 3 9 9 9
1.import java.util.*;
2.class Fortress {
3.private String name;
4.private ArrayList<Integer> list;
5.Fortress() { list = new ArrayList<Integer>(); }
6.String getName() { return name; }
7.void addToList(int x) { list.add(x); }
8.ArrayList getList() { return list; }
}
Which lines of code (if any) break encapsulation? (Choose all that apply.)
Line 4
Line 5
Line 7
Line 8
The class is already well encapsulated
Given:
class Feline {
public static void main(String[] args) {
long x = 42L;
long y = 44L;
System.out.print(" " + 7 + 2 + " ");
System.out.print(foo() + x + 5 + " ");
System.out.println(x + y + foo());
}
static String foo() { return "foo"; }
}
What is the result?
9 foo47 4244foo
72 foo47 86foo
72 foo47 4244foo
72 foo425 86foo
72 foo425 4244foo
Given:
1.class Beta { }
2.class Alpha {
3.static Beta b1;
4.Beta b2;
5.}
6.public class Tester {
7.public static void main(String[] args) {
8.Beta b1 = new Beta();
9.Beta b2 = new Beta();
10.Alpha a1 = new Alpha();
11.Alpha a2 = new Alpha();
12.a1.b1 = b1;
13.a1.b2 = b1;
14.a2.b2 = b2;
15.a1 = null;
16.b1 = null;
17.b2 = null;
18.// do stuff
}
}
When line 18 is reached, how many objects will be eligible for garbage collection?
0
1
2
4
5