QUESTION 21
Given:
class Line {
public class Point {
public int x, y;
}
public Point getPoint() {
return new Point();
}
}
class Triangle {
public Triangle() {
// insert code here
}
}
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();
Correct Answer: D Section: All Explanation
Explanation/Reference:
QUESTION 22
Given:
class TestA {
public void start() { System.out.println("TestA"); }
}
public class TestB extends TestA {
public void start() { System.out.println("TestB"); }
public static void main(String[] args) { ((TestA)new TestB()).start();
}
}
What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Correct Answer: B Section: All Explanation
Explanation/Reference:
QUESTION 23
Given:
11. public static void main(String[] args) {
12. Object obj = new int[] { 1, 2, 3 };
13. int[] someArray = (int[])obj;
14. for (int i : someArray) System.out.print(i + " ");
15. }
What is the result?
A. 1 2 3
B. Compilation fails because of an error in line 12.
C. Compilation fails because of an error in line 13.
D. Compilation fails because of an error in line 14.
E. A ClassCastException is thrown at runtime.
Correct Answer: A Section: All Explanation
Explanation/Reference:
QUESTION 24
Click the Exhibit button.
public class Threads1 {
int x = 0;
public class Runner implements Runnable {
public void run(){
int current = 0;
for(int i = 0; i<4; i++){ current = x;
System.out.println(current + ", "); x = current + 2;
}
}
}
public static void main(String[] args) {
new Threads1().go();
}
public void go(){
Runnable r1 = new Runner(); new Thread(r1).start(); new Thread(r1).start();
}
}
Which two are possible results? (Choose two.)
A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
Correct Answer: C Section: All Explanation
Explanation/Reference:
QUESTION 25
Given:
foo and bar are public references available to many other threads. foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait(). From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?
A. foo.notify();
B. bar.notify();
C. foo.notifyAll();
D. Thread.notify();
E. bar.notifyAll();
F. Object.notify();
Correct Answer: E Section: All Explanation
Explanation/Reference:
QUESTION 26
Given:
public class PingPong implements Runnable {
synchronized void hit(long n) {
for (int i = 1; i < 3; i++) System.out.print(n + "-" + i + " ");
}
public static void main(String[] args) { new Thread(new PingPong()).start(); new Thread(new PingPong()).start();
}
public void run() { hit(Thread.currentThread().getId());
}
}
Which two statements are true? (Choose two.)
A. The output could be 8-1 7-2 8-2 7-1
B. The output could be 7-1 7-2 8-1 6-1
C. The output could be 8-1 7-1 7-2 8-2
D. The output could be 8-1 8-2 7-1 7-2
Correct Answer: CD Section: All Explanation
Explanation/Reference:
QUESTION 27
Click the Exhibit button.
class Computation extends Thread {
private int num;
private boolean isComplete;
private int result;
public Computation(int num){ this.num = num; }
public synchronized void run() { result = num * 2; isComplete = true; notify();
}
public synchronized int getResult() {
while ( ! isComplete ){
try {
wait();
} catch (InterruptedException e) {
}
}
return result;
}
public static void main(String[] args) { Computation[] computations = new Computation[4]; for (int i = 0; i < computations.length; i++) {
computations[i] = new Computation(i); computations[i].start();
}
for (Computation c : computations) { System.out.println(c.getResult() + " ");
}
}
}
What is the result?
A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output "0 6".
E. The code may run with output "2 0 6 4".
F. The code may run with output "0 2 4 6".
Correct Answer: F Section: All Explanation
Explanation/Reference:
QUESTION 28
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
A. new Thread() {
public void run() { doStuff(); }
};
B. new Thread() {
public void start() { doStuff(); }
};
C. new Thread() {
public void start() { doStuff(); }
}.run();
D. new Thread() {
public void run() { doStuff(); }
}.start();
E. new Thread(new Runnable() {
public void run() { doStuff(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doStuff(); }
}).start();
Correct Answer: DF Section: All Explanation
Explanation/Reference:
QUESTION 29
Given:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public boolean equals(Object o) {
if ( ! ( o instanceof Person) ) return false; Person p = (Person) o;
return p.name.equals(this.name);
}
}
Which statement is true?
A. Compilation fails because the hashCode method is not overridden.
B. A HashSet could contain multiple Person objects with the same name.
C. All Person objects will have the same hash code because the hashCode method is not overridden.
D. If a HashSet contains more than one Person object with name="Fred", then removing another Person, also with name="Fred", will remove them all.
Correct Answer: B Section: All Explanation
Explanation/Reference:
QUESTION 30
Given:
import java.util.*;
public class SortOf {
public static void main(String[] args) { ArrayList<Integer> a = new ArrayList<Integer>(); a.add(1); a.add(5); a.add(3); Collections.sort(a);
a.add(2);
Collections.reverse(a); System.out.println(a);
}
}
What is the result?
A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.
G. An exception is thrown at runtime.
Correct Answer: C Section: All Explanation
Explanation/Reference:
Source from : Oracle.Exactexams.1z0-851.v2013-11-27.by.SomeBo
'Certificate > OCAJP' 카테고리의 다른 글
[1Z0-851D] Question 01~10 (0) | 2017.08.13 |
---|---|
[1Z0-851C] Question 51~60 (0) | 2017.08.12 |
[1Z0-851C] Question 41~50 (0) | 2017.08.12 |
[1Z0-851C] Question 31~40 (0) | 2017.08.12 |
[1Z0-851C] Question 11~20 (0) | 2017.08.12 |
[1Z0-851C] Question 01~10 (0) | 2017.08.12 |
[1Z0-851B] Question 51~60 (0) | 2017.08.11 |
[1Z0-851B] Question 41~50 (0) | 2017.08.11 |