private synchronized Object o;
void go() {synchronized() { /* code here */ }
public synchronized void go() { /* code here */ }
private synchronized(this) void go() { /* code here */ }
void go() {synchronized(Object.class) { /* code here */ }
void go() {Object o = new Object();synchronized(o) { /* code here */ }
第1题:
下列程序的输出结果为( )。 public class Reentrant { public synchronized void a() { b(); System.out.println("here I am,in a()"); } public synchronized void b() { System.out.println("here I am,in b()"); } public static void main(String args[]) { Reentrant r=new Reentrant(); r.a(); } }
A.here I am,in a()/here I am,in b()
B.here I am,in b()/here I am,in a()
C.here I am,in a()
D.here I am,in b()
第2题:
class One { void foo() {} } class Two extends One { //insert method here } Which three methods, inserted individually at line 14, will correctly complete class Two?()
第3题:
现有: 1. class Propeller2 { 2. pulolic static void main (String[]args)//add code here? 3. { new Propeller2().topGo(); } 4. 5.void topGo() //add code here? 6. { middleGo(); } 7. 8.void middleGo() //add code here? 9. { go(); System.out.println ("late middle"); } void go() //add code here? 12. {throw new Exception(); } 13. } 为使代码通过编译,需要在哪一行加入声明throws Exception?()
第4题:
public class Team extends java.util.LinkedList { public void addPlayer(Player p) { add(p); } public void compete(Team opponent) { /* more code here */ } } class Player { /* more code here */ } Which two are true?()
第5题:
Which three will compile and run without exception?()
第6题:
public class Threads4 { public static void main (String[] args) { new Threads4().go(); } public void go() { Runnable r = new Runnable() { public void run() { System.out.print(”foo”); } }; Thread t = new Thread(r); t.start(); t.start(); } } What is the result?()
第7题:
Given: 10. class One { 11. void foo() { } 12. } 13. class Two extends One { 14. //insert method here 15. } Which three methods, inserted individually at line 14, will correctly complete class Two?()
第8题:
An exception may be thrown at runtime.
The code may run with no output, without exiting.
The code may rum with output “A B A B C C “, then exit.
The code may ruin with output “A A A B C A B C C “, then exit.
The code may rum with output “A B C A B C A B C “, then exit.
The code may ruin with output “A B C A A B C A B C “, then exit.
第9题:
private synchronized Object o;
void go() {synchronized() { /* code here */ }
public synchronized void go() { /* code here */ }
private synchronized(this) void go() { /* code here */ }
void go() {synchronized(Object.class) { /* code here */ }
void go() {Object o = new Object();synchronized(o) { /* code here */ }
第10题:
private synchronized Object o;
void go() { synchronized() { /* code here */ } }
public synchronized void go() { /* code here */ }
private synchronized(this) void go() { /* code here */ }
void go() { synchronized(Object.class) { /* code here */ } }
void go() { Object o = new Object(); synchronized(o) { /* code here */ } }
第11题:
public synchronized int getProgress()
public synchronized void setIndeterminate (boolean indeterminate)
public synchronized void setProgress(int progress)
Public final synchronized void incrementProgressBy(int diff)
第12题:
The instance gets garbage collected.
The code on line 33 throws an exception.
The code on line 35 throws an exception.
The code on line 31 throws an exception.
The code on line 33 executes successfully.
第13题:
下列程序的输出结果为( )。 public class Reentrant { public synchronized void a() { b(); System.out.println("here I am, in a()"); } public synchronized void b() { System.out.println("here I am, in b()"); } public static void main(String args[ ]) { Reentrant r=new Reentrant(); r.a(); } }
A.here I am, in a()/here I am, in b()
B.hereI am, in b()/here I am, in a()
C.here I am, in a()
D.here I am, in b()
第14题:
void waitForSignal() { Object obj = new Object(); synchronized (Thread.currentThread()) { obj.wait(); obj.notify(); } } Which is true?()
第15题:
31. // some code here 32. try { 33. // some code here 34. } catch (SomeException se) { 35. // some code here 36. } finally { 37. // some code here 38. } Under which three circumstances will the code on line 37 be executed?()
第16题:
import java.util.*; public class NameList { private List names = new ArrayList(); public synchronized void add(String name) { names.add(name); } public synchronized void printAll() { for (int i = 0; i
第17题:
Which three will compile and rim without exception?()
第18题:
public class TestFive { private int x; public void foo() { int current = x; x = current + 1; } public void go() { for(int i=0;i<5;i++) { new Thread() { public void run() { foo(); System.out.print(x + “, “); } }.start(); }}} Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()
第19题:
public void foo() { /* more code here */ }
private void foo() { /* more code here */ }
protected void foo() { /* more code here */ }
int foo() { /* more code here */ }
void foo() { /* more code here */ }
第20题:
int foo() { /* more code here */ }
void foo() { /* more code here */ }
public void foo() { /* more code here */ }
private void foo() { /* more code here */ }
protected void foo() { /* more code here */ }
第21题:
This code will compile.
This code demonstrates proper design of an is-a relationship.
This code demonstrates proper design of a has-a relationship.
A Java programmer using the Team class could remove Player objects from a Team object.
第22题:
Move the line 12 print statement into the foo() method.
Change line 7 to public synchronized void go() {.
Change the variable declaration on line 3 to private volatile int x;.
Wrap the code inside the foo() method with a synchronized( this ) block.
Wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop code here }.
第23题:
只在第11行
在第8行和第11行
在第5行、第8行和第11行
在第2行、第5行、第8行和第11行
第24题:
int foo() { /* more code here */ }
void foo() { /* more code here */ }
public void foo() { /* more code here */ }
private void foo() { /* more code here */ }
protected void foo() { /* more code here */ }