单选题public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?()ACompilation fails.BAn exception is thrown at 

题目
单选题
public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?()
A

 Compilation fails.

B

 An exception is thrown at runtime.

C

 Synchronizing the run() method would make the class thread-safe.

D

 The data in variable “x” are protected from concurrent access problems.

E

 Declaring the doThings() method as static would make the class thread-safe.

F

 Wrapping the statements within doThings() in a synchronized(new Object()) {} block would make the class thread-safe.


相似考题
更多“public class TestSeven extends Thread {  private static int ”相关问题
  • 第1题:

    在下列程序的划线处应填入的语句是class Person { private int a;}public class Man extends Person{ public int b; public static void main (String arg []){ Person p=new Person(); Man t=new Man(); int i: }}

    A.i=w;

    B.i=b

    C.i=p.a;

    D.i=t.b;


    正确答案:D
    解析:选项A)w没有被声明过,不能使用。选项B)虽然b是类Man的public成员变量,但是在静态方法中不能使用类中的非静态成员。选项C)a是类Person的private成员,在类外不能直接引用。选项D)b是类Man的public成员变量,且是int 型,可以通过类的实例变量t用并赋值给一个int型变量。

  • 第2题:

    interface A{

    int x = 0;

    }

    class B{

    int x =1;

    }

    class C extends B implements A {

    public void pX(){

    System.out.println(x);

    }

    public static void main(String[] args) {

    new C().pX();

    }

    }


    正确答案:

     

    错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

    x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

    对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

    以通过A.x 来明确。

  • 第3题:

    请完善程序(程序文件名:Java_3.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。

    [题目要求]

    生成下面左边图形界面,单击图中的New按钮,弹出如右图所示的对话框。

    源程序:

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class Java_3 {

    public static void main(String[] args) {

    MulticastFrame. frame=new MulticastFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.show();

    }

    }

    class MulticastFrame. extends JFrame. {

    public MulticastFrame() {

    setTitle("MulticastTest");

    setSize(WIDTH,HEIGHT);

    MulticastPanel panel=new MulticastPanel();

    Container contentPane=getContentPane();

    contentPane.add( (1) );

    }

    public static final int WIDTH=300;

    public static final int HEIGHT=200;

    }

    class MulticastPanel extends JPanel }

    public MulticastPanel() {

    JButton newButton=new JButton("New");

    add(newButton);

    ActionListener newListener=new ActionListener() {

    public void actionPerformed(ActionEvent event) {

    makeNewFrame();

    }

    };

    newButton.addActionListener(newListener);

    closeAllButton=new JButton("Close all");

    add(closeAllButton);

    }

    private void makeNewFrame() {

    final BlankFrame. frame=new BlankFrame();

    frame.show();

    ActionListener closeAllListener=new ActionListener() {

    public void actionPerformed(ActionEvent event) {

    frame. (2) (); //使窗口隐藏或消除

    }

    };

    closeAllButton.addActionListener( (3) );

    }

    private JButton closeAllButton;

    }

    Class BlankFrame. extends JFrame. {

    public BlankFrame() {

    (4) ++;

    setTitle("Frame"+counter);

    setSize(WIDTH,HEIGHT);

    setLocation(SPACING*counter,SPACING*counter);

    }

    public static final int WIDTH=200;

    public static final int HEIGHT=150;

    public static final int SPACING=30;

    private static int counter=0;

    }


    正确答案:panel hide closeAllListener counter
    panel hide closeAllListener counter 解析: 通过下面的程序可以看出新的窗口的标题为Frame和数字,数字是自增的,所以自增的变量名称为counter。
    [程序解析] 程序在窗口中用按钮新建窗口,并且可以通过按钮关闭窗口。本程序采用的是swing类,Swing构件和AWT构件不同,Swing构件不能直接添加到顶层容器中,它必须添加到一个Swing顶层容器相关联的内容面板上。对JFrame添加构件有两种方式:①用getContentPane()方法获得JFrame的内容面板,再对其加入构件,Java上机考试中经常采用这种方式,而且也是一个考点。本程序就是采用的这种方法。②建立一个JPanel或JDesktopPane之类的中间容器,把构件添加到容器中,再用setContentPane()方法把该容器置为JFrame的内容面板。

  • 第4题:

    class A {  protected int method1(int a, int b) { return 0; }  }  Which two are valid in a class that extends class A?() 

    • A、 public int method1(int a, int b) { return 0; }
    • B、 private int method1(int a, int b) { return 0; }
    • C、 private int method1(int a, long b) { return 0; }
    • D、 public short method1(int a, int b) { return 0: }
    • E、 static protected int method1(int a, int b) { return 0; }

    正确答案:A,C

  • 第5题:

    Which three demonstrate an “is a” relationship?() 

    • A、 public class X {  }     public class Y extends X { }
    • B、 public interface Shape { }     public interface Rectangle extends Shape{ }
    • C、 public interface Color { }     public class Shape { private Color color; }
    • D、 public interface Species { }     public class Animal { private Species species; }
    • E、 public class Person { }    public class Employee {      public Employee(Person person) { }
    • F、 interface Component { }     class Container implements Component {   private Component[] children; }

    正确答案:A,B,F

  • 第6题:

    class Top {  static int x = 1;  public Top(int y) { x *= 3; }  }  class Middle extends Top {  public Middle() { x += 1; }  public static void main(String [] args) {  Middle m = new Middle();  System.out.println(x);  }  }  结果为:() 

    • A、1
    • B、2
    • C、3
    • D、编译失败

    正确答案:D

  • 第7题:

    Which two demonstrate an “is a” relationship?()   

    • A、 public interface Person { }  public class Employee extends Person { }
    • B、 public interface Shape { }  public class Employee extends Shape { }
    • C、 public interface Color { }  public class Employee extends Color { }
    • D、 public class Species { }  public class Animal (private Species species;)
    • E、 interface Component { }  Class Container implements Component ( Private Component[ ] children;  )

    正确答案:D,E

  • 第8题:

    public class Starter extends Thread {  private int x= 2;  public static void main(String[] args) throws Exception {  new Starter().makeItSo();  }  public Starter() {  x=5;  start();  }  public void makeItSo() throws Exception {  join();  x=x- 1;  System.out.println(x);  }  public void run() { x *= 2; }  }  What is the output if the main() method is rum?() 

    • A、 4
    • B、 5
    • C、 8
    • D、 9
    • E、 Compilation fails.
    • F、 An exception is thrown at runtime.
    • G、 It is impossible to determine for certain.

    正确答案:D

  • 第9题:

    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()

    • A、 public class Circle implements Shape { private int radius; }
    • B、 public abstract class Circle extends Shape { private int radius; }
    • C、 public class Circle extends Shape { private int radius; public void draw(); }
    • D、 public abstract class Circle implements Shape { private int radius; public void draw(); }
    • E、 public class Circle extends Shape { private int radius;public void draw() {/* code here */} }
    • F、 public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }

    正确答案:B,E

  • 第10题:

    多选题
    Which are syntactically valid statement at// point x?()     class Person {     private int a;  public int change(int m){  return m;  }     }  public class Teacher extends Person {     public int b;  public static void main(String arg[]){     Person p = new Person();     Teacher t = new Teacher();    int i;  // point x     }    }
    A

    i = m;

    B

    i = b;

    C

    i = p.a;

    D

    i = p.change(30);

    E

    i = t.b.


    正确答案: D,E
    解析: A:m没有被申明过,不能使用。 
    B:虽然b是类Teacher的public成员变量,但是在静态方法中不能使用类中的非静态成员。 
    C://a是类Person的private成员,在类外不能直接引用。 
    D://change(int m)方法是public方法,并且返回一个int型值,可以通过类的实例变量p引用并赋值给一个int型变量。 
    E://b是类Teacher的public成员变量,且是int型,可以通过类的实例变量t引用并赋值给一个int型变量

  • 第11题:

    单选题
    class ClassA {  public int numberOfinstances;  protected ClassA(int numberOfinstances) {  this.numberOflnstances = numberOfinstances;  }  }  public class ExtendedA extends ClassA {  private ExtendedA(int numberOfinstances) {  super(numberOflnstances);  }  public static void main(String[] args) {  ExtendedA ext = new ExtendedA(420);  System.out.print(ext.numberOflnstances);  }  }  Which is true?()
    A

     420 is the output.

    B

     An exception is thrown at runtime.

    C

     All constructors must be declared public.

    D

     Constructors CANNOT use the private modifier.

    E

     Constructors CANNOT use the protected modifier.


    正确答案: E
    解析: 暂无解析

  • 第12题:

    单选题
    What produces a compiler error?()
    A

     class A { public A(int x) {} }

    B

     class A {} class B extends A { B() {} }

    C

     class A { A() {} } class B { public B() {} }

    D

     class Z { public Z(int) {} } class A extends Z {}


    正确答案: A
    解析: 暂无解析

  • 第13题:

    在下面程序的下画线处应填入的选项是 public class Test______{ public static void main(String args[]) { Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run() { for(int i=0;i<5;i++) System.out.println("i="+i); } }

    A.implements Runnable

    B.extends Thread

    C.implements Thread

    D.extends Runnable


    正确答案:A
    解析:创建线程有两种方法:实现java.lang.Runnahle接口和继承Thread类并重写run()方法。从创建线程实例的语句Thread tt=new Thread(t);可以看出本程序将Test类的实例t作为参数传递给Thread类的构造方法,因此是通过实现Runnable接口创建线程。

  • 第14题:

    下列程序创建了一个线程并运行,横线处应填入的正确代码是( )。 public class Try extends Thread{ public static void main(String args[]){ Thread t=new Try; ; } public void runf System.out.println(”Try!"); } }

    A.t.start

    B.t.class

    C.t.thread

    D.t.static


    正确答案:A
    A。【解析】start是类Thread的方法,其中start方法用于启动线程,使之从新建状态转入就绪状态并进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的主线程独立地开始自己的生命周期了。

  • 第15题:

    ( 30 )在程序的下划线处应填入的选项是

    public class Test _________{

    public static void main(String args[]){

    Test t = new Test();

    Thread tt = new Thread(t);

    tt.start();

    }

    public void run(){

    for(int i=0;i<5;i++){

    system.out.println( " i= " +i);

    }

    }

    }

    A ) implements Runnable

    B ) extends Thread

    C ) implements Thread

    D ) extends Runnable


    正确答案:A

  • 第16题:

    Which statements concerning the following code are true?()   class a {   public a() {}   public a(int i) { this(); }   }   class b extends a {   public boolean b(String msg) { return false; }   }   class c extends b  {  private c() { super(); }   public c(String msg) { this(); }   public c(int i) {}   }  

    • A、The code will fail to compile.
    • B、The constructor in a that takes an int as an argument will never be called as a result of constructing an     object of class b or c.
    • C、Class c has three constructors.
    • D、Objects of class b cannot be constructed.
    • E、At most one of the constructors of each class is called as a result of constructing an object of class c.

    正确答案:B,C

  • 第17题:

    public class A extends Thread {  A() {  setDaemon(true);  }  public void run() {  (new B()).start();  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“A done”);  }  class B extends Thread {  public void run() {  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“B done”);  }  }  public static void main(String[] args) {  (new A()).start();  }  }   What is the result?()  

    • A、 A done
    • B、 B done
    • C、 A done B done
    • D、 B done A done
    • E、 There is no exception that the application will print anything.
    • F、 The application outputs “A done” and “B done”, in no guaranteed order.

    正确答案:B

  • 第18题:

    Which the two demonstrate an “is a” relationship?()

    • A、 public interface Person {}  Public class Employee extends Person {}
    • B、 public interface Shape {}  public interface Rectangle extends Shape {}
    • C、 public interface Color {}  public class Shape { private Color color; }
    • D、 public class Species {}  public class Animal { private Species species; }
    • E、 interface Component {} Class Container implements Component {private Component [] children;

    正确答案:B,E

  • 第19题:

    What produces a compiler error?()  

    • A、 class A { public A(int x) {} }
    • B、 class A {} class B extends A { B() {} }
    • C、 class A { A() {} } class B { public B() {} }
    • D、 class Z { public Z(int) {} } class A extends Z {}

    正确答案:D

  • 第20题:

    public class Parent {     int change() {…}     }  class Child extends Parent {     }  Which methods can be added into class Child?()    

    • A、 public int change(){}
    • B、 int chang(int i){}
    • C、 private int change(){}
    • D、 abstract int chang(){}

    正确答案:A,B

  • 第21题:

    单选题
    public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?()
    A

     Compilation fails.

    B

     An exception is thrown at runtime.

    C

     Synchronizing the run() method would make the class thread-safe.

    D

     The data in variable “x” are protected from concurrent access problems.

    E

     Declaring the doThings() method as static would make the class thread-safe.

    F

     Wrapping the statements within doThings() in a synchronized(new Object()) {} block would make the class thread-safe.


    正确答案: E
    解析: 暂无解析

  • 第22题:

    多选题
    public abstract class Shape {  private int x;  private int y;  public abstract void draw();  public void setAnchor(int x, int y) {  this.x = x;  this.y = y;  }  }  Which two classes use the Shape class correctly?()
    A

    public class Circle implements Shape { private int radius; }

    B

    public abstract class Circle extends Shape { private int radius; }

    C

    public class Circle extends Shape { private int radius; public void draw(); }

    D

    public abstract class Circle implements Shape { private int radius; public void draw(); }

    E

    public class Circle extends Shape { private int radius;public void draw() {/* code here */} }

    F

    public abstract class Circle implements Shape { private int radius;public void draw() { / code here */ } }


    正确答案: B,E
    解析: 暂无解析

  • 第23题:

    多选题
    Which two demonstrate an “is a” relationship?()
    A

    public interface Person { }  public class Employee extends Person { }

    B

    public interface Shape { }  public class Employee extends Shape { }

    C

    public interface Color { }  public class Employee extends Color { }

    D

    public class Species { }  public class Animal (private Species species;)

    E

    interface Component { }  Class Container implements Component ( Private Component[ ] children;  )


    正确答案: D,B
    解析: 暂无解析

  • 第24题:

    多选题
    public class TestDemo{   private int x = 2;   static int y = 3;   public void method(){   final int i=100;   int j = 10;   class Cinner{   public void mymethod(){  //Here  }  }  }  }   在Here处可以访问的变量是哪些?()
    A

    x

    B

    y

    C

    i

    D

    j


    正确答案: D,A
    解析: 暂无解析