Sample exam questions Sample questions by Oracle

1Z0-804 (OCPJP 7) sample exam questions by Oracle

1z0-804 sample exam questions
Written by Mushfiq Mammadov

1. Given:

class MySort implements Comparator<Integer> {
    public int compare(Integer x, Integer y) {
        return y.compareTo(x);
    }
}

And the code fragment:

    Integer[] primes = {2, 7, 5, 3};
    MySort ms = new MySort();
    Arrays.sort(primes, ms);
    for (Integer p2 : primes) {
        System.out.print(p2 + " ");
    }

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;
}
class Class3 {
    Class2 c1;
    String v3;
}

Which three options correctly describe the relationship between the classes?

A) Class2 has-a v3
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:

class MyKeys {
    Integer key;
    MyKeys(Integer k) {
        key = k;
    }
    public boolean equals(Object o) {
        return ((MyKeys) o).key == this.key;
    }
}

And 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, "bus");
System.out.print(m.size());

What is the result?

A) 2
B) 3
C) 4
D) Compilation fails.

 

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) 11
B) 111
C) 1111
D) An exception is thrown at runtime.

 

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 object
// 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 1110.

 

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) The program prints 1 then 2 after 5 seconds
B) The program prints: 1 thrown to main
C) The program prints: 1 2 thrown to main
D) The program prints:1 then t1 waits for its notification.

 

8. Given:

import java.text.*;
public class Align {
    public static void main(String[] args) throws ParseException {
        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) An exception is thrown at runtime.

 

Answers

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

 

Source: Oracle, Exam 804: Java SE 7

About the author

Mushfiq Mammadov

Leave a Comment