You are on page 1of 7

1.

Given:
import java.util.*;
public class Primes2 {
public static void main(String[] args) {
Integer[] primes = {2, 7, 5, 3};
MySort ms = new MySort();
Arrays.sort(primes, ms);
for(Integer p2: primes)
System.out.print(p2 + " ");
}
static class MySort implements Comparator

{
public int compare(Integer x, Integer y) {
return y.compareTo(x);
}
}
}
What is the result?
A) 2 3 5 7
B) 2 7 5 3
C) 7 5 3 2
D) Compilation fails.

2. Given:
class Class1 {
String v1;
}
class Class2 {
Class1 c1;
String v2;
}
public class Class3 {
Class2 c1;
String i3;
}
Which three options correctly describe the relationship between the classes?
A) Class2 has-a i3
B) Class1 has-a v2
C) Class2 has-a v2
D) Class3 has-a v1
E) Class2 has-a Class3
F) Class2 has-a Class1

3. Given this code snippet:

m = new HashMap();
MyKeys m1 = new MyKeys(1);
MyKeys m2 = new MyKeys(2);
MyKeys m3 = new MyKeys(1);
MyKeys m4 = new MyKeys(new Integer(2));
m.put(m1, "car");
m.put(m2, "boat");
m.put(m3, "plane");
m.put(m4, "hovercraft");
System.out.print(m.size());
Map

And this class:


class MyKeys {
Integer key;
MyKeys(Integer k) { key = k; }
public boolean equals(Object o) {
return ((MyKeys)o).key == this.key;
}
}
What is the result?
A) 2
B) 3
C) 4
D) Compilation fails.
E) An exception is thrown at run time.

4. Given:
import java.util.*;
public class MyScan {
public static void main(String[] args) {
String in = "1 a 10 . 100 1000";
Scanner s = new Scanner(in);
int accum = 0;
for(int x = 0; x < 4; x++) {
accum += s.nextInt();
}
System.out.println(accum);
}
}

What is the result?


A) 4
B) 10
C) 111

D) 1111
E) Compilation fails.
F) An exception is thrown at run time.

5. Given:
public class Truthy {
public static void main(String[] args) {
int x = 7;
assert(x == 6) ? "x == 6" : "x != 6";
}
}

What is the result if you try to compile Truthy.java and then run it with assertions enabled?
A) Truthy.java does NOT compile.
B) Truthy.java compiles and the output is "x != 6".
C) Truthy.java compiles and an AssertionError is thrown with no additional output.
D) Truthy.java compiles and an AssertionError is thrown with "x != 6" as additional output.

6.Given the code fragment:


try {
// assume "conn" is a valid Connection
// assume a valid Statement object is created
// assume rollback invocations will be valid
// use SQL to add 10 to a checking account
Savepoint s1 = conn.setSavePoint();
// use SQL to add 100 to the same checking account
Savepoint s2 = conn.setSavePoint();
// use SQL to add 1000 to the same checking account
// insert valid rollback method invocation here
} catch (Exception e) { }

Which two statements are true?


A) If conn.rollback(s1) is inserted, account will be incremented by 10.
B) If conn.rollback(s1) is inserted, account will be incremented by 1010.
C) If conn.rollback(s2) is inserted, account will be incremented by 100.
D) If conn.rollback(s2) is inserted, account will be incremented by 110.
E) If conn.rollback(s2) is inserted, account will be incremented by 1000.

7. Given:

public class Bees {


public static void main(String[] args) {
try {
new Bees().go();
} catch (Exception e ) {
System.out.println("thrown to main");
}
}
synchronized void go() throws InterruptedException {
Thread t1 = new Thread();
t1.start();
System.out.print("1 ");
t1.wait(5000);
System.out.print("2 ");
}
}

What is the result?


A) 1 then 2 with little delay
B) 1 then 2 after 5 seconds
C) 1 thrown to main
D) 1 2 thrown to main
E) Compilation fails.

8. Given:
import java.text.*;
public class Align {
public static void main(String[] args) {
String[] sa = {"111.234", "222.5678"};
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
for(String s: sa)
System.out.println(nf.parse(s));
}
}

What is the result?


A) 111.234
222.567
B) 111.234
222.568
C) 111.234
222.5678
D) Compilation fails.
E) An exception is thrown at run time.

9. Given:

class MyResource1 implements AutoCloseable {


public void autoClose() { System.out.print("1 "); } // A1
}
class MyResource2 implements AutoCloseable {
public void autoClose() { System.out.print("2 "); } // A2
}
public class Triangle {
public static void main(String[] args) {
try ( MyResource1 r1 = new MyResource1(); // B
MyResource2 r2 = new MyResource2();) { // B
System.out.print("t ");
}
finally {
System.out.print("f ");
}
}
}

What is the result?


A) 1 2 t f
B) 2 1 t f
C) t 1 2 f
D) t 2 1 f
E) Compilation fails due to an error on line B.
F) Compilation fails due to errors on lines A1 and A2.
G) Compilation fails due to errors on lines A1, A2, and B.

10. Given:
1. import java.util.*;
2. public class Forever {
3. public static void main(String[] args) {
4. List x1 = new ArrayList();

5. List x3 = new ArrayList<>();


6. }
7. }

What happens when you compile the code?


A) Compilation succeeds.
B) Compilation fails due to multiple errors.
C) Compilation fails due to an error on line 4.
D) Compilation fails due to an error on line 5.

11. Which is most closely associated with the object-oriented design concept known as "has-a"?
A) DAO
B) factory
C) adapter
D) singleton
E) composition

12.Given
import java.util.*;
public class MyDates {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); // line A
}
}
Which two statements are true?
A) Line A is an example of using the DAO pattern.
B) Line A is an example of using the factory pattern.
C) Line A is an example of using the singleton pattern.
D) java.util.Calendar could be an abstract class.
E) Line A could be replaced with: Calendar c = new Calendar();

13. Which method can be used for establishing a connection to a JDBC 4.1 data source?
A) Class.getConnection()
B) Class.getJDBCConnection()
C) DriverManager.forName()
D) DriverManager.getConnection()
E) DriverManager.getJDBCConnection()

14. Given this code fragment:


// assume "myURL" is a valid URL
try {
Connection conn =
DriverManager.getConnection(myURL);
Statement s = conn.createStatement();
// use SQL to add 100 to a checking account
// use more SQL to add 300 to the account
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}
What is the result?
A) Compilation fails.
B) An exception is thrown at run time.
C) The account's value is not changed.
D) The account's value is incremented by 100.
E) The account's value is incremented by 300.
F) The account's value is incremented by 400.

15. Implementing which method will properly extend java.util.ListResourceBundle?

A) addContents()

B) getContents()
C) addResources()
D) getResources()
E) getKeys()

Answers
1. D
2. C, D, F
3. C
4. F
5. A
6. A, D
7. C
8. D
9. F
10. A
11. E
12. B, D
13. D
14. F
15. B

You might also like