单选题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 synch

题目
单选题
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.print(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 ruin with output “0 2 4 6”.


相似考题
更多“class Computation extends Thread {  private int num;  privat”相关问题
  • 第1题:

    下列程序的运行结果是【 】。 include class test { private: int num; public: tes

    下列程序的运行结果是【 】。

    include <iostream. h>

    class test

    {

    private:

    int num;

    public:

    test()

    int TEST() {return num+100;}

    ~test()

    };

    test::test(){num=0;}

    test::~test(){cout<<"Destructor is active"<<endl;}

    void main()

    {

    test x[3]

    cout<<x[1]. TEST()<<endl;

    }


    正确答案:100
    100 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

  • 第2题:

    阅读以下说明和Java代码,回答问题1和问题2,将解答填写在对应栏内。

    【Java代码】

    class usethread implements (1) {

    int num

    usethread(int n){

    num=n;

    }

    public void (2) {

    for(int i=0;i<3;i++)

    System.out.println("running:"+num);

    System.out.println("finished:"+num);

    }

    public class multhread{

    public static void main(String args[]) (3) InterruptedException{

    Thread m1=new Thread(new usethread(1));

    Thread m2=new Thread(new usethread(2));

    m1.start();

    m2.start();

    m1.join();

    m2.join();

    }

    }

    【问题1】

    补充完整上面Java代码中(n)处。

    【问题2】

    写出上面Java代码运行的结果。


    正确答案:(1)Runnable (2)run() (3)throws 程序输出结果: running:1 running:2 running:1 running:2 running:1 running:2 finished:1 finished:2
    (1)Runnable (2)run() (3)throws 程序输出结果: running:1 running:2 running:1 running:2 running:1 running:2 finished:1 finished:2 解析:本题考查Java中线程的相关知识。
    题目要求按照程序给出的内容来完成填空和输出程序的运行结果。本题的关键是考查我们对线程的了解程度。线程的创建方法有两种,即通过类Thread和接口Runnable创建的方法。刚刚创建的线程还不能与其他的线程并发运行,当调用了方法start后,线程进入就绪态,在被Java虚拟机调度后才进入运行态。进入运行态的线程自动执行成员方法run(),在执行完这个成员方法后线程就又自动进入死亡态。下面来具体分析程序。
    第(1)空在定义类usethread语句中,从后面的关键字implements可以推断出类继承了一个接口,而在Java中,接口一般只有成员变量和成员方法的定义而没有成员方法的具体实现。根据后面的程序new Thread(new usethread(1))可以知道创建了线程对象,而这种创建线程对象的方法是通过接口Runnable来实现的,因此类usethread肯定是继承了接口Runnable,所以此空答案为Runnable。
    第(2)空是一个函数体的函数名,而函数体的作用是循环进行输出,从上面对线程的分析可以知道,此函数一定是run()函数,因此此空答案为run()。
    第(3)空是入口函数后面的语句,结合Java程序的特点,再从此空后面的内容不难推断出,此处是要显式生成异常来处理程序中的异常。而在Java中,一般用关键字throws来显式生成异常,因此此空答案为throws。
    对于问题2,我们可以根据程序来分析,程序中创建了两个线程,根据上面的分析我们可以知道,这两个线程都自动调用了函数run(),因此程序输出结果为:
    running:1
    running:2
    running:1
    running:2
    running:1
    running:2
    finished:1
    finished:2

  • 第3题:

    类A定义如下: class A { private int x=10; int getx() { return x;} } class B extends A { private int x=15; //需要覆盖getx()方法 } 在下述方法中可以在类B中覆盖getx()方法的是 ( )

    A.int getx(){…}

    B.int getx(float f){…}

    C.float getx(){…}

    D.double getx(float f){…}


    正确答案:A
    解析:在Java中子类的方法覆盖父类中的方法时要求两个方法的名称、返回值类型以及参数表必须相同。在本题中,选项B、D中方法的参数表不同于父类中的getx()方法的参数表;而选项C中方法的返回值类型与父类中getx()方法不同,所以选项A正确。

  • 第4题:

    下面程序的结果是 include class test{private: int num; publi

    下面程序的结果是 #include<iostream.h> class test{ private: int num; public: test( ); int getint( ) {return num;} ~test( );}; test::test( ) { num=0;} test::~test( ) { cout<<"Destructor is active"<<endl;} void

    A.Exiting main Destructor is active Destructor is active Destructor is active

    B.Exiting main Destructor is active Destructoris active

    C.Exiting main Destructoris active

    D.Exiting main


    正确答案:A
    解析:C++语言中析构函数是在程序退出不用该类的对象时进行调用。

  • 第5题:

    下列程序编译错误,是由于划线处缺少某个语句,该语句是______。 include class A { pr

    下列程序编译错误,是由于划线处缺少某个语句,该语句是______。

    include<iostream.h>

    class A

    {

    private:

    int numl;

    public:

    A( ):numl(0){}

    A(int i):numl(i){}

    };

    class B

    {

    private:

    int num2;

    public:

    B( ):num2(0){}

    B(int i):num2(i){}

    int my_math(A obj1, B obj2);

    };

    int B::my_math(A obj1,B obj2)

    {

    return(obj1.numl+obj2.num2);

    }

    void main(void)

    {

    A objl(4);

    B obj,obj2(5);

    cout<<"obj1+obj2:"<<obj.my_math(obj1,obj2);

    }


    正确答案:friend class B;
    friend class B; 解析:在B类中出现了对A类中私有成员numl的直接访问,这是不允许的。所以必须要把类B设成类A的友员才可以通过编译。

  • 第6题:

    ( 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

  • 第7题:

    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

  • 第8题:

    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

  • 第9题:

    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

  • 第10题:

    多选题
    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.


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

  • 第11题:

    多选题
    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
    解析: 暂无解析

  • 第12题:

    单选题
    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.print(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 ruin with output “0 2 4 6”.


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

  • 第13题:

    下列程序的运行结果是______。 include class test { private: int hum; public: tes

    下列程序的运行结果是______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test( );

    int TEST( ){return num+100;)

    ~test( );

    };

    test::test( ){num=0;)

    test::~test( ){cout<<"Destructor is active"<<endl;)

    void main( )

    {

    test x[3];

    cout<<x[1].TEST( )<<endl;

    }


    正确答案:100 Destructor is active Destructor is active Destructor is active
    100 Destructor is active Destructor is active Destructor is active 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

  • 第14题:

    根据下面的主程序,完成类的一种构造函数的最简单形式。 include class base { privat

    根据下面的主程序,完成类的一种构造函数的最简单形式。

    include<iostream.h>

    class base

    {

    private:

    int num;

    public:

    ______;

    };

    void main( )

    {

    base try(6);

    }


    正确答案:[10] base(int n){}
    [10] base(int n){} 解析:注意,根据main函数中对类base的使用情况可知,base类必须提供整型单参构造函数,同时要求构造其最简单的形式,故函数体为空。

  • 第15题:

    阅读以下说明和C++码,填入(n)处。

    [说明]

    建立一个分数类,使之具有下述功能:建立构造函数,它能防止分母为0,当分数不是最简形式时进行约分以及避免分母为负数。

    [C++代码]

    include<iostream.h>

    include<math.h>

    class Num

    {

    public:

    Num (int a,int b);

    private:

    int num1;

    int num2;

    }:

    Num:: Num (int a,int b)

    {

    if( (1) )

    {

    cout<<"ERROR"<<endl;

    return;

    }

    int min=fabs(a)<fabs (b)?fabs (a): fabs (b);

    int x=1;

    for (int i=1;i<=min;i++)

    if( (2) )

    x=i;

    a/=X;

    b/=x;

    if( (3) )

    {

    a=-a;

    b=-b;

    }

    (4)

    (5)

    }


    正确答案:(1)b==0 (2)a%i==0&&b%i==0 (3)b0 (4)num1=a; (5)num2=b;
    (1)b==0 (2)a%i==0&&b%i==0 (3)b0 (4)num1=a; (5)num2=b; 解析:本题中的代码实现了简单的一个分数类的定义,它的构造函数中能防止分母为0,当分数不是最简形式时进行约分以及避免分母为负数。根据功能设计要求,仔细阅读代码可以知道a为分子,b为分母。(1)是判断分母b为零的处理;(2)处实现了当分数不是最简形式时进行约分;(3)处避免分母为负数;(4)、(5)处返回处理完毕的数据。

  • 第16题:

    分析下面程序,哪一行代码能正确赋值?()class Demo {public void method() {final int num1 = 10;static int num2 = 20;abstract int num3 = 30;private int num4 = 40;}}

    A.final int num1 = 10;

    B.static int num2 = 20;

    C.abstract int num3 = 30;

    D.private int num4 = 40;


    答案:A
    解析:final可以修饰局部变量


    解析:虽然是运行时期异常,但是也可以使用try…catch语句进行处理。一旦进入处理语句就不会再回去执行

  • 第17题:

    如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test(int);

    void show( );

    };

    test::test(int n){num=n;}

    test::show( ){cout<<num<<endl;}

    void main( )

    {

    test T(10):

    T.show( );

    }


    正确答案:void test::show( ){coutnumendl;}
    void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。

  • 第18题:

    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

  • 第19题:

    Which Man class properly represents the relationship "Man has a best friend who is a Dog"?()

    • A、class Man extends Dog{}
    • B、class Man implements Dog{}
    • C、class Man{private BestFriend dog;}
    • D、class Man{private Dog bestFriend;}
    • E、class Man{private Dog;}
    • F、class Man{private BestFriend;}

    正确答案:D

  • 第20题:

    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

  • 第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 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,A
    解析: 暂无解析

  • 第24题:

    单选题
    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
    解析: 暂无解析