单选题现有:  class TestFoo f      int x;  String y;  int  getX()  {  return  x;  }     String getY()  {return y;  }      void setX (int  x)  {      int Z=7:      this.x=x;      }      可以添加多少个修饰符来封装此类?()A 5B 4C 3D 2

题目
单选题
现有:  class TestFoo f      int x;  String y;  int  getX()  {  return  x;  }     String getY()  {return y;  }      void setX (int  x)  {      int Z=7:      this.x=x;      }      可以添加多少个修饰符来封装此类?()
A

5

B

4

C

3

D

2


相似考题
更多“单选题现有:  class TestFoo f      int x;  String y;  int  getX()  {  return  x;  }     String getY()  {return y;  }      void setX (int  x)  {      int Z=7:      this.x=x;      }      可以添加多少个修饰符来封装此类?()A 5B 4C 3D 2”相关问题
  • 第1题:

    有以下程序: include using namespace std; class Point' { public: void SetPoint(

    有以下程序: #include <iostream> using namespace std; class Point' { public: void SetPoint(int x,int y); void Move(int xOff,int yOff); int GetX() { return X; } int GetY() { return Y; } private: int X,Y; }; void Point::SetPoint(int x, int y) { X=x; Y=y; } void Point: :Move(int xOff, int yOff) X+=xOff; Y+=yOff; } int main () { Point p1; p1.SetPoint(1,2); p1.Move (5, 6); cout<<"Point1 is ("<<p1.GetX()<<','<<p1.GetY()<<")"<<end1; return 0; } 执行后的输出结果是( )。

    A.Point1 is (6,8)

    B.Point1 is (1,2)

    C.Point1 is (5,6)

    D.Point1 is (4,4)


    正确答案:A
    解析:本题考核对象的定义与使用。程序中定义了一个类Point,在主函数中定义了一个Point类的对象p1,然后通过对象p1调用其成员函数SetPoint()和Move()实现移位的操作。

  • 第2题:

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

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

  • 第3题:

    有如下类定义: class Test { private int x; public int y; public void setX (int m) {x=m;} public int getX( ) {return x;} }现用Test t=new Text();生成一个对象t,则如下语句中,错误的是( )。

    A.t.x=10;

    B.t.y=10;

    C.t. setX(10);

    D.int m=t.getX( );


    正确答案:A
    解析:本题考查public和private修饰符的访问权限。public指明变量和方法为公有,可以通过其对象直接访问,因此,选项B、C、D都是正确的访问方法。private指明变量为私有访问,只能被该类自己访问或调用,因此,A选项中不能通过其对象来访问私有变量。

  • 第4题:

    有以下程序:includeusing namespace std;definePl 3.14Class Point{private:int x,y

    有以下程序: #include<iostream> using namespace std; #definePl 3.14 Class Point {private: int x,y; public: Point(int a,intB) {X=a; y:b;} int getx() <return x;} int gety() {return y;}}; class Circle:public Point {pri

    A.314

    B.157

    C.78.5

    D.153.86


    正确答案:A
    解析: 本程序设计了一个点类Point,包含了横,纵两个坐标数据x和y,由它派生出了圃类Circle,并加入了新的数据成员,即一个半径r和一个求圆面积的函数成员area。在主函数main中,首先定义了一个圃Circle类的对象c1,并通过它的构造函数初始化其数据成员。由此可知,其半径r的值为10,所以其面积为PI*10*10=314,即对象c1的函数成员area的返回值为314。

  • 第5题:

    若有以下程序: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的对象对它们进行访问。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

  • 第6题:

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

    若有以下程序: #include <iostream> using namespace std; class Base { private: int x; protected: int y; public: int z; void setx(int i) { x=i; int getx () { return x; } }

    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的对象对它们进行访问。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

  • 第7题:

    现有:  Public/project]class TestFoo {   int x;   String y;   int getX() { return x; } (4,5,6是方法)   String getY() { return y; }   void setX(int x) {   int z = 7;   this.x = x;   }   }   Private代表属性封装可以添加多少个修饰符来封装此类?()

    • A、2
    • B、3
    • C、4
    • D、5

    正确答案:A

  • 第8题:

    现有:  class TestFoo f      int x;  String y;  int  getX()  {  return  x;  }     String getY()  {return y;  }      void setX (int  x)  {      int Z=7:      this.x=x;      }      可以添加多少个修饰符来封装此类?()     

    • A、5
    • B、4
    • C、3
    • D、2

    正确答案:D

  • 第9题:

    10. class Inner {  11. private int x;  12. public void setX( int x) { this.x = x; }  13. public int getX() { return x; }  14. }  15.  16. class Outer {  17. private Inner y;  18. public void setY( Inner y) { this.y = y; }  19. public Inner getY() { return y; }  20. }  21.  22. public class Gamma {  23. public static void main( String[] args) { 24. Outer o = new Outer(); 25. Inner i = new Inner();  26.int n=10;  27. i.setX(n);  28. o.setY(i);  29. // insert code here  30. System.out.println( o.getY().getX());  31. }  32. }  Which three code fragments, added individually at line 29, produce the output 100?()

    • A、 n = 100;
    • B、 i.setX( 100);
    • C、 o.getY().setX( 100);
    • D、 i = new Inner(); i.setX( 100);
    • E、 o.setY( i); i = new Inner(); i.setX( 100);
    • F、 i = new Inner(); i.setX( 100); o.setY( i);

    正确答案:B,C,F

  • 第10题:

    单选题
    现有:   class TestFoo {   int x;   String y;   int getX() { return x; }   String getY() { return y; }   void setX(int x) {   int z = 7;   this.x = x;   }    }    可以添加多少个修饰符来封装此类?()
    A

     2

    B

     3

    C

     4

    D

     5


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

  • 第11题:

    多选题
    给定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){}


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

  • 第12题:

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

  • 第13题:

    执行下列代码后,输出的结果为( )。 class Base { int x = 30; void setX( ) {x=1O;} } class SubClass extends Base { int x=40; void setX ( ) {x=20;} int getX( ) {return super. x; } } public class Test { public static void main(String[ ] args) { SubClass sub=new SubClass( ); sub. setX( ); System. out. println(sub, getX( ) ); } }

    A.10

    B.20

    C.30

    D.40


    正确答案:C
    解析:本题主要考查有关类的继承方面的知识。Java中,类是分层次的,当子类的成员变量与父类的成员变量名字相同时,子类的成员变量会隐藏父类的成员变量,当子类的成员方法与父类的成员方法名字、参数列表、返回值类型都相同时,子类的方法是父类的方法的重写。这样,在子类的对象调用方法时,是按照子类中方法定义执行,隐藏父类的方法的定义。当子类隐藏了父类的变量,并重写了父类的方法后,又要使用父类变量或父类被重写的方法时,可通过super来实现对父类变量的访问和父类方法的调用。因此,本题中在main ()中调用setX ()时,是调用的SubClass类中的setX ()函数,同时将SubClass类中的i变量值设为20。当main ()函数中调用getX ()函数时,并不是取了SubClass类中的i的值,而是取的Base类中i变量的值,此时i的值为其初始值30。

  • 第14题:

    下列函数定义中,会出现编译错误的是

    A.max(int x,int y,int*z) {*z=x>y?xy;}

    B.int max(int x,y) {int z; z=x>y?xy; return z; }

    C.max(int x,int y) {int z; z=x>y?xy;return(z); }

    D.int max(int x,int y) { return(x>y?xy);}


    正确答案:B
    解析:本题考核的知识点是函数的定义方法。选项B中在说明形参时,省略了第二个参数的类型,故选项B编译不能通过。

  • 第15题:

    执行下面程序,显示的结果为( )。 public class Test { public static void main (String args[]) { Test t=newTest(); System.out.println (Loverload ("2","3")); } int overload (intx,int y) {return x+y;} String overload (String x,Stnng y){return x+y;} }

    A.2

    B.3

    C.5

    D.23


    正确答案:D
    解析:本题考查方法重载相关知识。方法的重载是指多个方法可以享用相同的名字,但参数的数量或类型必须不完全相同、即方法体有昕不同。使用该方法时,编译系统会根据实参类型选择执行相应的方法。本题中,在调用overload()方法时,实参为字符串,因此会调用String overload (String x,String y)方法,该方法返回两实参连接后的结果,所以返回值为“23”。

  • 第16题:

    若有以下程序: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都是公有成员,可以通过对象对它们进行访问。所以程序中对各成员的访问是正确的。本程序的功能是对类中各数据成员进行赋值,然后查看赋值是否正确。

  • 第17题:

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

  • 第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题:

    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

  • 第20题:

    现有:   class TestFoo {   int x;   String y;   int getX() { return x; }   String getY() { return y; }   void setX(int x) {   int z = 7;   this.x = x;   }    }    可以添加多少个修饰符来封装此类?() 

    • A、 2
    • B、 3
    • C、 4
    • D、 5

    正确答案:A

  • 第21题:

    多选题
    10. class Inner {  11. private int x;  12. public void setX( int x) { this.x = x; }  13. public int getX() { return x; }  14. }  15.  16. class Outer {  17. private Inner y;  18. public void setY( Inner y) { this.y = y; }  19. public Inner getY() { return y; }  20. }  21.  22. public class Gamma {  23. public static void main( String[] args) { 24. Outer o = new Outer(); 25. Inner i = new Inner();  26.int n=10;  27. i.setX(n);  28. o.setY(i);  29. // insert code here  30. System.out.println( o.getY().getX());  31. }  32. }  Which three code fragments, added individually at line 29, produce the output 100?()
    A

    n = 100;

    B

    i.setX( 100);

    C

    o.getY().setX( 100);

    D

    i = new Inner(); i.setX( 100);

    E

    o.setY( i); i = new Inner(); i.setX( 100);

    F

    i = new Inner(); i.setX( 100); o.setY( i);


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

  • 第22题:

    单选题
    现有:  Public/project]class TestFoo {   int x;   String y;   int getX() { return x; } (4,5,6是方法)   String getY() { return y; }   void setX(int x) {   int z = 7;   this.x = x;   }   }   Private代表属性封装可以添加多少个修饰符来封装此类?()
    A

    2

    B

    3

    C

    4

    D

    5


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

  • 第23题:

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