Java认证考试

Which three will compile and rim without exception?()A、 private synchronized Object o;B、 void go() { synchronized() { /* code here */ } }C、 public synchronized void go() { /* code here */ }D、 private synchronized(this) void go() { /* code here */ }E、 voi

题目

Which three will compile and rim without exception?()

  • A、 private synchronized Object o;
  • B、 void go() { synchronized() { /* code here */ } }
  • C、 public synchronized void go() { /* code here */ }
  • D、 private synchronized(this) void go() { /* code here */ }
  • E、 void go() { synchronized(Object.class) { /* code here */ } }
  • F、 void go() { Object o = new Object(); synchronized(o) { /* code here */ } }
如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

Given:Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()

A.move the line 12 print statement into the foo() method

B.change line 7 to public synchronized void go() {

C.change the variable declaration on line 2 to private volatile int x;

D.wrap the code inside the foo() method with a synchronized( this ) block

E.wrap the for loop code inside the go() method with a synchronized block synchronized(this){ //for loop code here }


参考答案:A, D

第2题:

下列程序的输出结果为( )。 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()


正确答案:B
解析:此题程序中类Reentrant定义了两个带有synchronized的方法,分别是a()和b()。在Reentrant类的main()方法中,Reentrant类的实例r调用了方法a(),在a()中调用b()。a()的执行过程中,线程的控制将请求并获得r的锁,并开始执行a()方法。由b()的定义可知,线程获得r的对象锁才能运行该方法,而此时r的锁已经由该线程获得,根据Java对象锁的可重入性,该线程将再次获得r的锁,并开始运行方法b()。

第3题:

What happens when you try to compile and run the following program?

class Mystery{String s;public static void main(String[] args){

Mystery m=new Mystery();m.go();}void Mystery(){s=”constructor”;}void go(){System.out.println(s);}

}()

A.this code will not compile

B.this code compliles but throws an exception at runtime

C.this code runs and “constructor” in the standard output

D.this code runs and writes “null” in the standard output


参考答案:D

第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?()

  • A、 This code will compile.
  • B、 This code demonstrates proper design of an is-a relationship.
  • C、 This code demonstrates proper design of a has-a relationship.
  • D、 A Java programmer using the Team class could remove Player objects from a Team object.

正确答案:A,D

第5题:

class One {  void foo() {}  }  class Two extends One {   //insert method here  }  Which three methods, inserted individually at line 14, will correctly complete class Two?()

  • A、 int foo() { /* more code here */ }
  • B、 void foo() { /* more code here */ }
  • C、 public void foo() { /* more code here */ }
  • D、 private void foo() { /* more code here */ }
  • E、 protected void foo() { /* more code here */ }

正确答案:B,C,E

第6题:

下列程序的输出结果为( )。 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()


正确答案:B
解析:此题程序中类Reentrant定义了两个带有synchronized的方法,分别是a()和b()。在Reentrant类的main()方法中,Reentrant类的实例r调用了方法a(),在a()中调用b()。a()的执行过程中,线程的控制将请求并获得r的锁,并开始执行a()方法。由b()的定义可知,线程获得r的对象锁才能运行该方法,而此时r的锁已经由该线程获得,根据Java对象锁的可重入性,该线程将再次获得r的锁,并开始运行方法b()。

第7题:

以下哪个方法可以用来获得进度条的当前进度值?()

  • A、public synchronized int getProgress()
  • B、public synchronized void setIndeterminate (boolean indeterminate)
  • C、public synchronized void setProgress(int progress)
  • D、Public final synchronized void incrementProgressBy(int diff)

正确答案:A

第8题:

Given:Which three methods, inserted individually at line 14, will correctly complete class Two?()

A.int foo() { /* more code here */ }

B.void foo() { /* more code here */ }

C.public void foo() { /* more code here */ }

D.private void foo() { /* more code here */ }

E.protected void foo() { /* more code here */ }


参考答案:B, C, E

第9题:

现有:  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?()     

  • A、只在第11行
  • B、在第8行和第11行
  • C、在第5行、第8行和第11行
  • D、在第2行、第5行、第8行和第11行

正确答案:D

第10题:

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 System.out.print(names.get(i) +“ “); }  }  public static void main(String[] args) {  final NameList sl = new NameList();  for(int i=0;i<2;i++) {  new Thread() {  public void ruin() {  sl.add(”A”);  sl.add(”B”);  sl.add(”C”);  sl.printAll();  }  }.start();  }  }  }  Which two statements are true if this class is compiled and run?()

  • A、 An exception may be thrown at runtime.
  • B、 The code may run with no output, without exiting.
  • C、The code may rum with output “A B A B C C “, then exit.
  • D、The code may ruin with output “A A A B C A B C C “, then exit.
  • E、 The code may rum with output “A B C A B C A B C “, then exit.
  • F、The code may ruin with output “A B C A A B C A B C “, then exit.

正确答案:E,F

更多相关问题