"A script enabled browser is required for this page to function properly"

Oracle Certification

Java SE 7 Programmer II - Sample Questions

Sample Questions

View answers below

Sample questions are provided solely to familiarize candidates with the multiple-choice format and writing style of questions that will be found on the exam. Sample questions may not cover the full spectrum of difficulty that is covered by the exam questions. Success on the sample questions does not predict success on the exam..

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:

Map 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());

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.


Answers

1. D
2. C, D, F
3. C
4. F
5. A
6. A, D
7. C
8. D