OCJP(Java 6) 기출문제 연습 15(141~150)
PC나 스마트폰에서도 동일하게 실행되므로 장소를 가리지 않고 OCJP(JAVA 6) 국제 자격증을 목표로 학습할 수 있다.
QUESTION 141
Click the Exhibit button.
1. public class Threads1 {
2. int x = 0;
3. public class Runner implements Runnable {
4. public void run(){
5. int current = 0;
6. for(int i = 0; i<4; i++){
7. current = x;
8. System.out.println(current + ", ");
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args) {
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable r1 = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23.}
Which two are possible results? (Choose two.)
Click the Exhibit button.
1. public class Threads1 {
2. int x = 0;
3. public class Runner implements Runnable {
4. public void run(){
5. int current = 0;
6. for(int i = 0; i<4; i++){
7. current = x;
8. System.out.println(current + ", ");
9. x = current + 2;
10. }
11. }
12. }
13.
14. public static void main(String[] args) {
15. new Threads1().go();
16. }
17.
18. public void go(){
19. Runnable r1 = new Runner();
20. new Thread(r1).start();
21. new Thread(r1).start();
22. }
23.}
Which two are possible results? (Choose two.)
C
the first thread prints 0, 2, 4, 6.
the second thread executes right after the first one and prints 8, 10, 12, 14
ZZZ
the second thread executes right after the first one and prints 8, 10, 12, 14
ZZZ
QUESTION 142
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()?
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()?
E
설명없음
QUESTION 143
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.)
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.)
C,D
설명없음
QUESTION 144
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?
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?
F
설명없음
QUESTION 145
Given:
1. class Mammal {
2. }
3.
4. class Raccoon extends Mammal {
5. Mammal m = new Mammal();
6. }
7.
8. class BabyRaccoon extends Mammal {
9. }
10.
Which four statements are true? (Choose four.)
Given:
1. class Mammal {
2. }
3.
4. class Raccoon extends Mammal {
5. Mammal m = new Mammal();
6. }
7.
8. class BabyRaccoon extends Mammal {
9. }
10.
Which four statements are true? (Choose four.)
A,B,C,F
설명없음
QUESTION 146
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
Which two code fragments will execute the method doStuff() in a separate thread? (Choose two.)
D,F
설명없음
QUESTION 147
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?
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?
B
설명없음
QUESTION 148
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?
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?
C
설명없음
QUESTION 149
Given:
public class Person {
private name;
public Person(String name) {
this.name = name;
}
public int hashCode() {
return 420;
}
}
Which statement is true?
Given:
public class Person {
private name;
public Person(String name) {
this.name = name;
}
public int hashCode() {
return 420;
}
}
Which statement is true?
A
설명없음
QUESTION 150
Given:
import java.util.TreeSet;
public class Explorer2 {
public static void main(String[] args) {
TreeSet < Integer > s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(608, true, 611, true);
s.add(629);
System.out.println(s + " " + subs);
}
}
What is the result?
Given:
import java.util.TreeSet;
public class Explorer2 {
public static void main(String[] args) {
TreeSet < Integer > s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
subs = (TreeSet)s.subSet(608, true, 611, true);
s.add(629);
System.out.println(s + " " + subs);
}
}
What is the result?
E
설명없음