单选题class Foo {  private int x;  publicFoo(intx) {this.x=x; }  public void setX( int x) { this.x = x; }  public int getX() { return x; }  }   public class Gamma {  static Foo fooBar( Foo foo) {  foo = new Foo( 100);  return foo;  }  public static void main

题目
单选题
class Foo {  private int x;  publicFoo(intx) {this.x=x; }  public void setX( int x) { this.x = x; }  public int getX() { return x; }  }   public class Gamma {  static Foo fooBar( Foo foo) {  foo = new Foo( 100);  return foo;  }  public static void main( String[] args) {  Foo foo = new Foo( 300);  System.out.print( foo.getX() + “-“);  Foo fooFoo = fooBar( foo);  System.out.print( foo.getX() + “-“);  System.out.print( fooFoo.getX() + “-“);  foo = fooBar( fooFoo);  System.out.print( foo.getX() + “-“);  System.out.prmt( fooFoo.getX());  }  }  What is the output of this program?()
A

 300-100-100-100-100

B

 300-300-100-100-100

C

 300-300-300-100-100

D

 300-300-300-300-100


相似考题
更多“class Foo {  private int x;  publicFoo(intx) {this.x=x; }  p”相关问题
  • 第1题:

    classFoo{

    privateintx;

    publicFoo(intx){this.x=x;}

    publicvoidsetX(intx){this.x=x;}

    publicintgetX(){returnx;}

    }

    publicclassGamma{

    staticFoofooBar(Foofoo){

    foo=newFoo(100);

    returnfoo;

    }

    publicstaticvoidmain(String[]args){

    Foofoo=newFoo(300);

    System.out.print(foo.getX()+-);

    FoofooFoo=fooBar(foo);

    System.out.print(foo.getX()+-);

    System.out.print(fooFoo.getX()+-);

    foo=fooBar(fooFoo);

    System.out.print(foo.getX()+-);

    System.out.prmt(fooFoo.getX());

    }

    }

    Whatistheoutputofthisprogram?()


    参考答案:B

  • 第2题:

    有如下程序:includeusing namespace std;class Part{public:Part(int x=0):val(x) {c

    有如下程序: #include<iostream> using namespace std; class Part{ public: Part(int x=0):val(x) {cout<<val;} ~Part(){cout<<val;} private: int val; }; class Whole{ public: Whole(int x ,int y, int z=0):p2(x),p1(y),val(z){cout<<val;} ~Whole(){cout<<val;} private: Part p1,p2; int val; }; int main() Whole obj(1,2,3); return 0; } 程序的输出结果是( )。

    A.123321

    B.213312

    C.213

    D.123123


    正确答案:B
    解析:此题考查的是类的构造与析构。在建立类的对象时,构造函数的执行顺序如下:执行基类的构造函数,调用顺序按照各个基类被继承时声明的顺序(自左向右),接着执行成员对象的构造函数,调用顺序按照各个成员对象在类中声明的顺序,最后执行自身的构造函数。析构顺序与之相反。本题中,Whole类中有两个Part类的成员p1和p2,根据它们定义的顺序,先构造p1再构造p2。所以,首先被构造的是p1(y),输出2;第二个被构造的是p2(x),输出1;最后调用析构函数,输出val的值3;析构函数调用顺序与构造函数相反。故最后输出213312。

  • 第3题:

    有以下程序:includeusingnamespacestd;definePI3.14classPoint{private: intx,y;pub

    有以下程序: #include <iostream> using namespace std; #define PI 3.14 class Point { private: int x,y; public: Point(int a,int b) { x=a; y=b; } int getx() { return x; } int gety() { return y; } }; class Circle : public Point { private: int r; public: Circle(int a,int b,int c):Point(a,b) { r=c; } int getr() { return r; } double area() { return PI*r*r; } }; int main() { Circle c1(5,7,10); cout<<cl.area()<<endl; return 0; } 程序执行后的输出结果是

    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。

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

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

  • 第6题:

    请找出下列程序中错误之处 ______。 include classA{private: intx1;protected: int

    请找出下列程序中错误之处 ______。

    #include<iostream.h>

    class A{

    private:

    int x1;

    protected:

    int x2;

    public:

    int x3;

    };

    class B:public A{

    private:

    int y1;

    protected:

    int y2;

    public:

    int y3;

    void disp(){cout<<x1<<y1<<end1:} //A

    void set(int i) {x2=i;} //B

    };

    void main() {

    B bb;

    bb.x3=10; //C

    bb.y3=10; //D

    }

    A.A

    B.B

    C.C

    D.D


    正确答案:A

  • 第7题:

    有以下程序:inClUdeusingnamespacestd;ClassBase{public: Base(intx) {a=x; } voidsh

    有以下程序: #inClUde <iostream> using namespace std; Class Base { public: Base(int x) { a=x; } void show() { cout<<a; } private: int a; }; class Derived : public Base { public: Derived(int i) :Base(i+1),b(i){} void Show() { cout<<b; } private: int b; }; int main() { Base b(5),*pb; Derived d(1); pb=&d; pb->show(); return 0; } 运行后的输出结果是( )。

    A.1

    B.5

    C.2

    D.0


    正确答案:C
    解析:基类Base派生出派生类Derived,在主函数中,定义了基类对象b,基类指针pb,以及派生类对象d,并让基类指针pb指向派生类对象d。在C++中,当派生类的对象赋值给基类对象时,只能使用派生类对象中从基类继承的成员。所以最后执行语句“pb->show();”是调用基类的成员函数show(),输出a的值2。

  • 第8题:

    若有语句:int x,*p=&x;则与该语句等价的语句是()

    • A、intx,*p;*p=&x;
    • B、int x,*p;p=&x;
    • C、int x,*p;*p=x;
    • D、int x,*p;p=x

    正确答案:B

  • 第9题:

    class Foo {  private int x;  publicFoo(intx) {this.x=x; }  public void setX( int x) { this.x = x; }  public int getX() { return x; }  }   public class Gamma {  static Foo fooBar( Foo foo) {  foo = new Foo( 100);  return foo;  }  public static void main( String[] args) {  Foo foo = new Foo( 300);  System.out.print( foo.getX() + “-“);  Foo fooFoo = fooBar( foo);  System.out.print( foo.getX() + “-“);  System.out.print( fooFoo.getX() + “-“);  foo = fooBar( fooFoo);  System.out.print( foo.getX() + “-“);  System.out.prmt( fooFoo.getX());  }  }  What is the output of this program?()

    • A、 300-100-100-100-100
    • B、 300-300-100-100-100
    • C、 300-300-300-100-100
    • D、 300-300-300-300-100

    正确答案:B

  • 第10题:

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

  • 第11题:

    单选题
    10. public class Foo implements java.io.Serializable {  11. private int x;  12. public int getX() { return x; }  12.publicFoo(int x){this.x=x; }  13. private void writeObject( ObjectOutputStream s)  14. throws IOException {  15. // insert code here  16. }  17. }  Which code fragment, inserted at line 15, will allow Foo objects to be correctly serialized and deserialized?()
    A

     s.writeInt(x);

    B

     s.serialize(x);

    C

     s.writeObject(x);

    D

     s.defaultWriteObject();


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

  • 第12题:

    单选题
    1. import java.io.*;  2. public class Foo implements Serializable {  3. public int x, y;  4. public Foo( int x, int y) { this.x = x; this.y = y; }  5.  6. private void writeObject( ObjectOutputStream s)  7. throws IOException {  8. s.writeInt(x); s.writeInt(y)  9. }  10.  11. private void readObject( ObjectInputStream s)  12. throws IOException, ClassNotFoundException {  13.  14. // insert code here  15.  16. }  17. }  Which code, inserted at line 14, will allow this class to correctly serialize and deserialize?()
    A

     s.defaultReadObject();

    B

     this = s.defaultReadObject();

    C

     y = s.readInt(); x = s.readInt();

    D

     x = s.readInt(); y = s.readInt();


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

  • 第13题:

    10.publicclassFooimplementsjava.io.Serializable{11.privateintx;12.publicintgetX(){returnx;}12.publicFoo(intx){this.x=x;}13.privatevoidwriteObject(ObjectOutputStreams)14.throwsIOException{15.//insertcodehere16.}17.}Whichcodefragment,insertedatline15,willallowFooobjectstobecorrectlyserializedanddeserialized?()

    A.s.writeInt(x);

    B.s.serialize(x);

    C.s.writeObject(x);

    D.s.defaultWriteObject();


    参考答案:D

  • 第14题:

    有如下类的定义。横线处的语句是( )。 class MyClass { public: MyClass(int a=0,int b=0) { X=a; Y=b; } void Change()const { X-=10; Y+=10; } private: ______intX, Y;

    A.static

    B.const

    C.mutable

    D.不需要填入内容


    正确答案:C
    解析:C++中const关键字用来表示一个常量,同时const也用来修饰函数。这两个概念需要明确:const所修饰的函数只能是类的成员函数,因为const所修饰的函数中,要由编译器负责保护类的成员变量不被修改。而相对的,mutable则是用来修饰类的成员变量,让该变量在const所修饰的成员函数中可以被修改。而且const修饰的函数只能是类的成员函数,mutable修饰的变量只能是类的成员变量。

  • 第15题:

    有以下程序: include class A { intx; public: A(int a) { x=a;} friend class B;

    有以下程序:

    include<iostream.h>

    class A

    {

    int x;

    public:

    A(int a)

    {

    x=a;

    }

    friend class B;

    }

    class B{

    public:

    void print(A a){

    a. x--;

    cout<<a.x<<end1;

    }

    };

    void main()

    {

    A a(10);

    B b;

    b.print(a) ;

    }

    程序执行后的输出结果是【 】。


    正确答案:9
    9 解析:本题考核友元类的应用。在程序中,类B是类A的友元类,因此,在类B的所有成员函数中均可访问类A的任何成员。在main()中,先定义类A的一个对象a(10)和类B的一个对象b。然后通过对象b调用其成员函数print输出对象a的私有成员x的值减1即9。

  • 第16题:

    有如下程序: #jnClude<iostream> using namespaCe std; Class Part{ publiC: Part(int x=0):val(X){Cout<<val;} ~Part{Cout<<val;} private: int val; t }; Class Whole{ publiC: Whole(int x,int Y,int z=0):p2(x),pl(y),val(z){Cout<<val;} ~Whole{eout<<val;} private: Part pl,p2; int val; }; int main { Whole obj(1,2,3); return 0; } 执行这个程序的输出结果是( )。

    A.123321

    B.213312

    C.213

    D.123123


    正确答案:B
    本题考查带参数的构造函数。本题中定义了一个对象obj,那么执行其构造函数,初始化p1,p2的顺序是由声明这两个成员的顺序决定的,而不是由参数化列表顺序决定,所以本题中,会先执行p1,输出2,然后执行p2输出1,然后输出3,最后一次输出312。所以本题答案为8。

  • 第17题:

    下面程序中错误之处是 ______。 include classA{private:intxl;protected:intx2;publ

    下面程序中错误之处是 ______。

    include<iostream.h>

    class A{

    private:

    int xl;

    protected:

    int x2;

    public:

    int x3;

    };

    class B: public A{

    private:

    int b1;

    protected:

    int b2;

    public:

    int b3;

    void disp(){cout<<x1<<b2<<end1;} //A

    void set(int i){x3=i;} //B

    };

    void main()

    B bb;

    bb. a3=10 //C

    bb. b3=10 //D

    }


    正确答案:√
    1

  • 第18题:

    在下列源代码文件Test.java中, ( )是正确的类定义。

    A.public class test{

    B.public class Test{ public int x=0;public int x=0; public test (intx) public Test (int x){ {this.x=x; this.x=x;} }} }

    C.public class Test extends T1,T2{

    D.protected class Test extends T2{ public int=0;public int x=0; public Test(int x){Public Test (int x){ this.x=x;this.x=x: }} }}


    正确答案:B

  • 第19题:

    在下列源代码文件Test.java中,哪个选项是正确的类定义? ( )

    A.public class test { public int x=0; public test(int x) { this.x=x; } }

    B.public class Test { public int x=0; public Test(int x) { this.x=x; } }

    C.public class Test extends Ti,T2 { public int x=0; public Test(int x) { this.x=x; } }

    D.protected class Test extends T2 { public int x=0; public Test(int x) { this.x=x; } }


    正确答案:B

  • 第20题:

    下列类的定义中正确的是()

    • A、class a{int x=0;int y=1;}
    • B、class b{int x=0;int y=1;};
    • C、class c{intx;int y;}
    • D、class d{intx;int y;};

    正确答案:D

  • 第21题:

    1. class SuperFoo {  2. SuperFoo doStuff(int x) {  3. return new SuperFoo(); 4. }  5. }  6.  7. class Foo extends SuperFoo {  8. //insert code here  9. }   下面哪三项分别插入到第8行,可以编译?()

    • A、int doStuff() { return 42; }
    • B、int doStuff(int x) { return 42; }
    • C、Foo doStuff(int x) { return new Foo(); }
    • D、SuperFoo doStuff(int x) { return new Foo(); }

    正确答案:A,C,D

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

    多选题
    public class TestFive {  private int x;  public void foo() {  int current = x;  x = current + 1;  }  public void go() {  for(int i=0;i<5;i++) {  new Thread() {  public void run() {  foo();  System.out.print(x + “, “);  } }.start();  }}}  Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()
    A

    Move the line 12 print statement into the foo() method.

    B

    Change line 7 to public synchronized void go() {.

    C

    Change the variable declaration on line 3 to private volatile int x;.

    D

    Wrap the code inside the foo() method with a synchronized( this ) block.

    E

    Wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop code here }.


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

  • 第24题:

    单选题
    现有:  1.class SuperFoo{  2.SuperFoo doStuff (int x)  {      3.return new SuperFoo();      4.    }      5.  }      6.  7. class Foo extends SuperFoo  {     8.    //insert code here     9.  }  和四个声明:   Foo doStuff (int x)  {  return new Foo();  }  Foo doStuff (int x)  {  return new SuperFoo();  }      SuperFoo doStuff(int x)  {  return new Foo();  }  SuperFoo doStuff(int y)  {  return new SuperFoo();  }    分别插入到第8行,有几个可以通过编泽?()
    A

     1

    B

     2

    C

     3

    D

     4


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