单选题class Account {   private int balance;   public void setBalance(int b) { balance = b; }  public int getBalance() { return balance; }   public void clearBalance() { balance = 0; }   }   哪一个改变可以使 Account 类线程安全?()A 在第2行加 synchronized 修饰符。B 在第3行加 synchroni

题目
单选题
class Account {   private int balance;   public void setBalance(int b) { balance = b; }  public int getBalance() { return balance; }   public void clearBalance() { balance = 0; }   }   哪一个改变可以使 Account 类线程安全?()
A

在第2行加 synchronized 修饰符。

B

在第3行加 synchronized 修饰符。

C

在第3行、第4行和第6行加 synchronized 修饰符。

D

在第4行、第6行和第8行加 synchronized 修饰符。


相似考题
参考答案和解析
正确答案: A
解析: 暂无解析
更多“单选题class Account {   private int balance;   public void setBalance(int b) { balance = b; }  public int getBalance() { return balance; }   public void clearBalance() { balance = 0; }   }   哪一个改变可以使 Account 类线程安全?()A 在第2行加 synchronized 修饰符。B 在第3行加 synchroni”相关问题
  • 第1题:

    下面这个程序的结果是 include class A { private: int a; public: void seta( ) ;

    下面这个程序的结果是

    #include<iostream.h>

    class A

    {

    private:

    int a;

    public:

    void seta( ) ;int geta( ) ;};

    void A: :seta( )

    { a=1;}

    int A: :geta( )

    { retum a;}

    class B

    { private:

    int a;

    public:

    void seta( ) ;int geta( ) ;};

    void B: :seta( )

    {a=2;}

    int B: :geta( )

    {return a;}

    class C: public A,public B

    { private:

    int b;

    public:

    void display( ) ;};

    void C: :display( )

    { int b=geta( ) ;

    cout < < b;}

    void main( )

    { C c;

    c. seta( ) ;

    c. display( ) ;}

    A.1

    B.2

    C.随机输出1或2

    D.程序有错


    正确答案:D
    解析:在类A中有geta( ) 函数,在类B中也有geta( ) 函数,类C继承了类A和类B,这样就产生了二义性,所以程序会出错。

  • 第2题:

    阅读下面实现堆栈类并发控制的部分代码 public class DataStack{ private int idx=0; private int[]data=new int[8]; public void push(int i){ . ________________{ data[idx]=i; idx + +; } } } …… } 在程序下画线处填入正确选项是

    A.synchronized

    B.synchronized(this)

    C.synchronized()

    D.synchronized(idx)


    正确答案:B
    解析:synchronized关键字用于同步方法,而在同步一个代码块时,用度synchronized(this)来实现。

  • 第3题:

    若有以下程序:includeusing namespace std;class A{private:int a; public:void seta

    若有以下程序: #include<iostream> using namespace std; class A { private: int a; public: void seta(int x) { a=x; } void showa() { cout<<a<<","; } }; class B { private: int b; public: void setb(int x) { b=x; } void showb() { cout<<b<<",”; } }; class C:pUblic A,private B { private: int c; public: void setc(int x,int y,int z) { c=z; seta(x); setb(y); } void showc() { showa(); showb(); cout<<c<<end1; } }; int main() { Cc; c.setc(1,2,3); c.showc(); retrun 0; } 程序执行后的输出结果是

    A.1,2,3

    B.1,1,1

    C.2,2,2

    D.3,3,3


    正确答案:A
    解析:本题考核派生类的应用。本题中类A和类B都是基类。而类C从类A公有派生,从类B处私有派生。所以类C中的函数成员可以访问类A和类B中的公有成员。在类C的函数成员setc中,调用基类A的函数成员seta对A的数据成员a赋值,还调用了基类B的函数成员setb对类B的数据成员b赋值,然后对类C自己的数据成员c赋值。在类C的函数成员showc中,调用基类A的函数成员showa显示数据成员a的值,还调用基类B的函数成员showb显示数据成员b的值,然后输出类C自己的数据成员c的值。在主函数main中,先定义派生类的对象c,然后调用setc对c中的数据成员赋值,然后输出赋值结果。所以程序最后输出应该为:1,2,3。

  • 第4题:

    若有以下程序:include using namespace std;class A{private:int a;public:A(int i){

    若有以下程序: #include <iostream> using namespace std; class A { private: int a; public: A(int i) { a=i; } void disp () { cout<<a<<","; } }; class B { private: int b; public: B(int j) { b=j; } void disp () { cout<<b<<","; } }; class C : public B,public A { private: int c; public: C(int k):A(k-2),B(k+2) { c=k; } void disp () { A::disp(); B::disp(); cout<<c<<endl; } }; int main() { C obj(10); obj.disp(); return 0; }

    A.10,10,10

    B.10,12,14

    C.8,10,12

    D.8,12,10


    正确答案:D
    解析:本题考核派生类构造函数的使用。本题中,派生类c具有多重继承关系,所以在派生类C的构造函数中应该包含基类A和B的成员初始化列表。

  • 第5题:

    为了支持压栈线程与弹栈线程之间的交互与同步,应 在下画线处填入的选项是( )。 public class StackTest{ private int idx=0; private int[]data=new int[8] public void push(int i){ synchronized(this){ ; data(idx)=i: idx++: } } }……

    A.this.notify

    B.interrupt

    C.this.wait

    D.sleep


    正确答案:A
    A。【解析】当一个线程使用的同步方法中用到某个变量,而且需要其他线程修改此变量后才能复合本线程的需要,那么可以使用wait方法。wait方法的作用是使本线程等待,并允许其他线程使用此同步方法。当其他线程使用完后应使用notify或者notifyAll方法允许其他线程使用此同步方法。Interrupt方法的作用是在run方法执行完之前就消灭此线程,而sleep方法的作用是延迟一段时间后执行。因为本题是为了支持压栈线程与弹栈线程之间的交互与同步,所以选A。

  • 第6题:

    为了支持压栈线程与弹栈线程之间的交互与同步,在程序的下画线处依次填入的语句是( )。 public class IntStack{ private int idx=0; private int[]data=new int[8]; public void push(int i){ data[idx]=i: idx++; … … }

    A.synchronized notify

    B.synchronized this.wait

    C.synchronized this.notify

    D.Serializable sleep


    正确答案:B
    B。【解析】在Synchronized块中等待共享数据的状态改变时调用wait方法,这样该线程等待并暂时释放共享数据对象的锁。

  • 第7题:

    ( 31 ) 为了支持压栈线程与弹栈线程之间的交互与同步 , 在程序的下划线处依次填入的语句是

    public class IntStack{

    private int idx=0;

    private int[] data=new int[8];

    public void push(int i){

    data[idx]=i;

    idx++;

    }

    __________

    ......

    }

    A ) synchronized()

    notify()

    B ) synchronized()

    this.wait()

    C ) synchronized()

    this.notify()

    D ) synchronized()

    sleep()


    正确答案:B

  • 第8题:

    class Account {   private int balance;   public void setBalance(int b) { balance = b; }  public int getBalance() { return balance; }   public void clearBalance() { balance = 0; }   }   哪一个改变可以使 Account 类线程安全?() 

    • A、在第2行加 synchronized 修饰符。
    • B、在第3行加 synchronized 修饰符。
    • C、在第3行、第4行和第6行加 synchronized 修饰符。
    • D、在第4行、第6行和第8行加 synchronized 修饰符。

    正确答案:D

  • 第9题:

    public class MethodOver  {  public void setVar (int a, int b, float c)  {  }  }   Which two overload the setVar method?()  

    • A、 Private void setVar (int a, float c, int b)  { }
    • B、 Protected void setVar (int a, int b, float c) { }
    • C、 Public int setVar (int a, float c, int b) (return a;)
    • D、 Public int setVar (int a, int b, float c) (return a;)
    • E、 Protected float setVar (int a, int b, float c) (return c;)

    正确答案:A,C

  • 第10题:

    public class SyncTest {  private int x;  private int y;  public synchronized void setX (int i) (x=1;)  public synchronized void setY (int i) (y=1;)  public synchronized void setXY(int 1)(set X(i); setY(i);)  public synchronized Boolean check() (return x !=y;)  }  Under which conditions will check () return true when called from a different class?

    • A、 Check() can never return true.
    • B、 Check() can return true when setXY is called by multiple threads.
    • C、 Check() can return true when multiple threads call setX and setY separately.
    • D、 Check() can only return true if SyncTest is changed to allow x and y to be set separately.

    正确答案:A

  • 第11题:

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

  • 第12题:

    单选题
    public class SyncTest {  private int x;  private int y;  private synchronized void setX( int i ) { x = i; }  private synchronized void setY( int i ) { y = i; }  public void setXY( int i ) { setX(i); setY(i); }  public synchronized boolean check() { return x != y; }  }   Under which condition will check return true when called from a different class? ()
    A

     check can never return true.

    B

     check can return true when setXY is called by multiple threads.

    C

     check can return true when multiple threads call setX and setY separately.

    D

     check can return true only if SyncTest is changed to allow x and y to be set separately.


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

  • 第13题:

    有以下程序:include using namespace std;class A{private:int a;public:A (int i){a

    有以下程序: #include <iostream> using namespace std; class A { private: int a; public: A (int i) { a=i; } void disp() { cout<<a<<","; } }; class B { private: int b; public: B(int j) { b=j; } void disp() { cout<<b<<","; } }; class C : public B,public A { private: int c; public: C(int k) :A(k-2),B(k+2) { c=k; } void disp () { A::disp (); B::disp (); cout<<c<<endl; } }; int main () { C obi (10); obj.disp (); return 0; } 程序执行后的输出结果是

    A.10,10,10

    B.10,12,14

    C.8,10,12

    D.8,12,10


    正确答案:D
    解析:本题考核派生类构造函数的使用。本题中,派生类C具有多重继承关系,所以在派生类C的构造函数中应该包含基类A和B的成员初始化列表。

  • 第14题:

    为了支持压栈线程与弹栈线程之间的交互与同步,在下画线处依次填入的语句是 public class IntStack { private int idx=0; private int[]data=new int[8]; public ______ void push(int i) { data[idx]=i; idx++; ______ } … }

    A.synchronized() notify()

    B.synchronized() this.wait()

    C.synchronized() this.notify()

    D.synchronized() sleep()


    正确答案:C
    解析:一个程序中单独的、并发的线程对同一个对象进行访问的代码段,称为临界区。在Java语言中,临界区可以是一个语句块或是一个方法,并且用synchronized关键字标识。本程序中push()方法即为临界区,所以需要用synchronized关键字标识。this.notify()把当前堆栈对象的wait pool中的一个线程释放到lock pool,等待该堆栈的锁以便运行。

  • 第15题:

    若有以下程序:includeusing namespace std;class A{private:inta;public:voidseta(in

    若有以下程序: #include <iostream> using namespace std; class A { private: int a; public: void seta(int x) { a=x; } void showa() { cout<<a<<","; } }; class B { private: int b; public: void setb (int x) { b=x; } void showb() { cout<<b<<","; } }; class C :public A,private B { private: int c; public: void setc(int x, inc y, int z) { c=z; seta (x); setb (y); } void showc() { showa (); showb (); cout<<c<<end1; } }; int main () { C c; c. setc(1,2,3); c.showc(); return 0; } 程序执行后的输出结果是

    A.1,2,3

    B.1,1,1

    C.2,2,2

    D.3,3,3


    正确答案:A
    解析:本题考核派生类的应用。本题中类A和类B都是基类。而类C从类A公有派生,从类B处私有派生.所以类C中的函数成员可以访问类A和类B中的公有成员。在类C的函数成员setc中,调用基类A的函数成员seta对A的数据成员a赋值,还调用了基类B的函数成员setb对类B的数据成员b赋值,然后对类C自己的数据成员c赋值。在类C的函数成员showc中,调用基类A的函数成员showa显示数据成员a的值,还调用基类B的函数成员showb显示数据成员b的值,然后输出类C自己的数据成员c的值.在主函数main中,先定义派生类的对象c,然后调用setc对c中的数据成员赋值,然后输出赋值结果。所以程序最后输出应该为:1,2,3。

  • 第16题:

    若有以下程序:include using namespace std;class A{private:int a;public:void seta

    若有以下程序:#include <iostream>using namespace std;class A{private: int a;public: void seta(int x) { a=x; } void showa() { cout<<a<<","; }};class B{private: int b;public: void setb(int x) { b=x; } void showb() { cout<<b<<","; }};class C: public A, private B{private: int c;public: void setc(int x, int y, int z) { c=z; seta(x); setb(y); } void showc() { showa(); showb(); cout<<c<<end1; }};int main(){ C c; c.setc(1,2,3); c.showc(); return 0;}程序执行后的输出结果是( )。

    A.1,2,3

    B.1,1,1

    C.2,2,2

    D.3,3,3


    正确答案:A

  • 第17题:

    现有人编写了帐号类Account,用于管理个人的存款余额, public Account{ private double balance; //帐号余额,余额最低为0 public double getBalance(){ return balance; } Public void setBalance(double b){ balance=b; } public double withdrawl(double money) throws Exception { //取款,money如果为负数或余额不足,抛出异常 } } 请用错误推测法为withdrawl()函数至少设计3个测试用例,并写出基于Junit的测试代码 。


    正确答案:
    publicclassTestSampleextendsTestCase{publicvoidtestWithDrawlEqual(doublemoney){Accounta=newAccount();a.setAccount(5);doubleresult=a.withdrawl(5);Assert.assertEquals(0,result);}publicvoidtestWithDrawlNegative(){try{Accounta=newAccount();a.setAccount(5);doubleresult=a.withdrawl(-1);fail();}catch(Exceptionerr){Assert.assertTrue(true);}}publicvoidtestWithDrawlTooMuch(){try{Accounta=newAccount();a.setAccount(5);doubleresult=a.withdrawl(8);fail();}catch(Exceptionerr){Assert.assertTrue(true);}}}

  • 第18题:

    若有以下程序:includeusing namespace std;class A{private:int x;public:int z;void

    若有以下程序:#include<iostream>using namespace std;class A {private: int x;public: int z; void setx(int i) { x=i; } int getx () { return x; }}:class B : public A{private: int m;public: int p; void setvalue(int a, int b, int c) { setx(a) ; z=b; m=c; } void display{) { cout<<getx ()<<", "<<z<<", "<<m<<end1; }};int main(){ B obj; obj. setvalue(2,3,4); obj.display(); return 0;} 程序运行以后的输出结果是

    A.产生语法错误

    B.2,3,4

    C.2,2,2

    D.4,3,2


    正确答案:B
    解析:本题考核继承与派生。当类的继承方式为公有继承时,基类的公有成员和保护成员分别作为派生类的公有成员和保护成员,派生类的其他成员可以直接访问它们。其他外部使用者只能通过派生类的对象访问继承来的公有成员。在本题中,数据成员z和函数setx都是基类A的公有成员,它们经过公有继承以后,在派生类B中还是公有成员,而派生类B中的函数setvalue和display都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是输出已设置的各成员的值。

  • 第19题:

    给定java代码如下所示,在A处新增下列()方法,是对cal方法的重载。public class Test {  public void cal(int x, int y, int z) { } //A } 

    • A、public int cal(int x,int y,float z){return 0;}
    • B、public int cal(int x,int y,int z){return 0;}
    • C、public void cal(int x,int z){}
    • D、public viod cal(int z,int y,int x){}

    正确答案:A,C

  • 第20题:

    public class SyncTest {  private int x;  private int y;  private synchronized void setX( int i ) { x = i; }  private synchronized void setY( int i ) { y = i; }  public void setXY( int i ) { setX(i); setY(i); }  public synchronized boolean check() { return x != y; }  }   Under which condition will check return true when called from a different class? () 

    • A、 check can never return true.
    • B、 check can return true when setXY is called by multiple threads.
    • C、 check can return true when multiple threads call setX and setY separately.
    • D、 check can return true only if SyncTest is changed to allow x and y to be set separately.

    正确答案:B

  • 第21题:

    public class NamedCounter {  private final String name;  private int count;  public NamedCounter(String name) { this.name = name; }  public String getName() { return name; }  public void increment() { coount++; }  public int getCount() { return count; } public void reset() { count = 0; } }  Which three changes should be made to adapt this class to be used safely by multiple threads? ()

    • A、 declare reset() using the synchronized keyword
    • B、 declare getName() using the synchronized keyword
    • C、 declare getCount() using the synchronized keyword
    • D、 declare the constructor using the synchronized keyword
    • E、 declare increment() using the synchronized keyword

    正确答案:A,C,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题:

    单选题
    class Account {   private int balance;   public void setBalance(int b) { balance = b; }  public int getBalance() { return balance; }   public void clearBalance() { balance = 0; }   }   哪一个改变可以使 Account 类线程安全?()
    A

    在第2行加 synchronized 修饰符。

    B

    在第3行加 synchronized 修饰符。

    C

    在第3行、第4行和第6行加 synchronized 修饰符。

    D

    在第4行、第6行和第8行加 synchronized 修饰符。


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

  • 第24题:

    单选题
    public class SyncTest {  private int x;   private int y;   public synchronized void setX (int i) (x=1;)   public synchronized void setY (int i) (y=1;)   public synchronized void setXY(int 1)(set X(i); setY(i);)   public synchronized Boolean check() (return x !=y;)   }   Under which conditions will check () return true when called from a different class?
    A

     Check() can never return true.

    B

     Check() can return true when setXY is called by multiple threads.

    C

     Check() can return true when multiple threads call setX and setY separately.

    D

     Check() can only return true if SyncTest is changed to allow x and y to be set separately.


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