public class SyncTest (  private int x;  private int y;  private synchronized void setX (int i) (x=1;)  private synchronized void setY (int i) (y=1;)  public void setXY(int 1)(set X(i); setY(i);)  public synchronized Boolean check() (return x !=y;)  )  Un

题目

public class SyncTest (  private int x;  private int y;  private synchronized void setX (int i) (x=1;)  private synchronized void setY (int i) (y=1;)  public 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 setseparately.
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

若有以下程序:includeusingnamespacestd;classA{private:int x; public:int x;void s

若有以下程序: #include<iostream> usingnamespacestd; classA { private: int x; public: int x; 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都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是输出已设置的各成员的值。

第2题:

有以下程序:include using namespacestd;class A{public:A(int i,int j){ a=1; b=j;}

有以下程序:#include <iostream>using namespace std;class A{public: A(int i,int j) { a=1; b=j; } void move (int x,int y) { a+=x; b+=y; } void show() cout<<a<<","<<b<<end1 } private: int a,b; }; class B : private A { public: B(int i,int 3):A (i,j) {} void fun() { move (3,5); } void f1() { A::show(); } }; int main() { B d(3,4); d.fun(); d.f1(); return 0; } 程序执行后的输出结果是

A.3,4

B.6,8

C.6,9

D.4,3


正确答案:C
解析:本题考核派生类的应用。本题中,类B是类A的私有派生类,在类B的成员函数fun中调用基类A的成员函数move,并传入实参3和5。在类B的成员函数f1中调用基类A的成员函数show,来显示基类数据成员a和b的值。主函数main中,定义了派生类B的对象d,并赋初值3和4.然后调用对象d的成员函数fun和f1,通过上述对函数fun和f1的功能的描述可知,程序最后输出6和9。

第3题:

有如下程序:includeusing namespace std;Class TestClass{private:int x,y;public:Te

有如下程序: #include<iostream> using namespace std; Class TestClass { private: int x,y; public: TestClass(int i,int j) { x=i; y=j; } void print() { cout<<“print1”<<endl; } void print()const { cout<<”prin

A.printl

B.print2

C.printl print2

D.程序编译时出错。


正确答案:B
解析: 本题定义TestClass型的常对象a,然后调用对象a中的成员函数print()。因为在C++中,如果一个对象被声明为常对象,则不能调用镇对象中的非const型的成员函数。所以,这里调用的是对象中的const型成员函数“void print()const”,输出为print2。

第4题:

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

若有以下程序 #include <iostream> using namespace std; class A { public: A(int i,int j) { a=i; b=j; } void move(int x, int y) { a+=x; b+=y; } void show() { cout < <a < <" , " <<b<< end1; } private: int a,b; }; class B : private A { public: B(int i,int j) :A(i,j) {} void fun ( ) { move (3, 5); } void f1 ( ) { A::show(); } }; int main ( ) { B d(3,4); d. fun ( ); d.f1(); return 0; } 程序执行后的输出结果是 ( )。

A.3,4

B.6,8

C.6,9

D.4,3


正确答案:C
解析:本题考核派生类的应用。本题中,类B是类A的私有派生类,在类B的成员函数fun()中调用基类A的成员函数move(),并传入实参3和5。在类B的成员函数f1()中调用基类A的成员函数show(),来显示基类数据成员a和b的值。主函数中,定义了派生类B的对象d,并赋初值3和4。然后调用对象d的成员函数fun()和f1(),通过上述对函数fun()和f1()的功能的描述可知,程序最后输出6和9。

第5题:

若有以下程序:include using namespace std;class A{private: int x;protected: int

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

A.产生语法错误

B.7,6,5

C.5,6,7

D.7,5,6


正确答案:C
解析:本题考核保护继承中对类成员的访问权限。①在保护继承中,基类公有成员和保护成员都以保护成员身份出现在派生类中,而基类私有成员不可访问。②基类的公有成员和保护成员被继承以后作为派生类的保护成员,这样,派生类的其他成员可以直接访问它们。③由保护派.生的类声明的对象,不能访问任何基类的成员。在本题中,基类A中的数据成员y和函数setx,经过保护继承以后,在派生类B中成为保护成员,派生类B的对象不能访问它们。而派生类B中的函数setvalue和display都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

第6题:

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

有以下程序: #include <iostream> using namespace std; class A { public: A(int i,int j) { a=i; b=j; } void move(int x,int y) { a+=x; b+=y; } void show() { cout<<a<<","<<b<<end1; } private: int a,b; }; class B: private A { public: B(int i,int j): A(i,j) {} void fun() { move(3,5); } void fl () { A::show(); } }; int main() { B d(3,4); d.fun(); d.f1(); return 0; } 程序执行后的输出结果是

A.3,4

B.6,8

C.6,9

D.4,3


正确答案:C
解析:本题考核派生类的应用。本题中,类B是类A的私有派生类,在类B的成员函数fun中调用基类A的成员函数move,并传入实参3和5。在类B的成员函数fl中调用基类A的成员函数show,来显示基类数据成员a和b的值。主函数main中,定义了派生类B的对象d,并赋初值3和4。然后调用对象d的成员函数fun和fl,通过上述对函数fun和n的功能的描述可知,程序最后输出6和9。

第7题:

有以下程序:include using namespace std;class A{private: int x,y;public: void se

有以下程序: #include <iostream> using namespace std; class A { private: int x,y; public: void set (int i,int j) { x=i; y=j; } int get_y() { return y; } }; class box { private: int length,width; A label; public: void set(int 1,int w, int s,int p) { length=1; width=w; label.set(s,p); } int get_area() { return length*width; } }; int main() { box small; small.set(2,4,1,35); cout<<small.get_area()<<end1; return 0; } 运行后的输出结果是( )。

A.8

B.4

C.35

D.70


正确答案:A
解析:本题考核成员对象的应用。类box的成员函数set()为设置对象的坐标值和对象的长、宽值。成员函数setarea返回该对象的面积。程序最后输出为8。

第8题:

本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java.awt.*; import java.awt.event.*; import javax swing.*; class MousePanel extends JPanel extends MouseMo- tionListener {public MousePanel {addMouseListener(new MouseAdapter {public void mousePressed(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; current=find(x,y); if(current<0) add(x,y); } public void mouseClicked(MouseEvent evt) {int X=evt.getX; int Y=evt.getY; if(evt.getClickCount>=2) {remove(current); } } }); addMouseMotionListener(this); } public void paintComponent(Graphics g) {super.paintComponent; for(int i=0;i<nsquares;i++) draw(g,i); } public int find(int X,int y) (for(int i=0;i<nsquares;i++) if(squares[i].x-SQUARELENGTH/2<= x X<=squares[i].x+SQuARELENGTH/2 squares[i].Y-SQUARELENGTH/2< =Y y<=squares[i].Y+SQUARELENGTH /2) return i ; return-1 ; } public void draw(Graphics g,int i) {g.drawRect(squares[i].X-SQUARE- LENGTH/2。 squares[i].Y-SQUARELENGTH/2, SQUARELENGTH, SQUARELENGTH); } public void add(int X,int Y) {if(nsquares<MAXNSQUARES) {squares[nsquares]=new Point(x,y); current=nsquares ; nsquares++; repaint; } } public void remove(int n) {if(n<0 ‖ n>=nsquares)return; Nsquares- -; squares[n]=squares[nsquares]; if(current= =n)current= -l; repaint; } public void mouseMoved(MouseEvent evt) {} public void mouseDragged(MouseEvent evt) {} private static final int SQUARELENGTH=10: private static final int MAXNSQUARES=100; private Point[]squares=new Point[MAX- NSQUARES]; private int nsquares=0; private int current=-l; } class MouseFrame. extends JFramc {public MouseFrame {setTitle("java3"); setSize(300,200); addWindowListener(new WindowAdapter {public void windowClosing(WindowEvent e) {System.exit(0); } }); Container contentPane=getContentPane; contentPane.add(MousePanel); } } public class java3 {public static void main(String[]args) {JFrame. frame=new MouseFrame; frame.show; } }


正确答案:
第l处:extends JPanel implements MouseMotionListener.
第2处:super.paintComponent(g)
第3处:contentPane.add(new MousePanel)
【解析】第1处是继承Jpanel实现鼠标移动监听器接口;第2处以g为参数重新绘制组件;第3处在contentPane内容面板中添加一个MousePanel鼠标面板。

第9题:

若有以下程序:include using namespace std;class Base{ int x;protected: int y;pub

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

A.1,2,3,4

B.产生语法错误

C.4,3,2,1

D.2,3,4,5


正确答案:A
解析:本题中,基类Base中的保护成员y和公有成员setx和getx,经过私有继承以后,称为派生类Inherit的私有成员,所以可以在派生类Inherit的函数成员中对它们进行访问。类Inherit中的函数成员setvalue和display都是公有成员,所以可以通过Inherit的对象对它们进行访问。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

第10题:

若有以下程序: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。

更多相关问题