单选题public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting && title == “Hello”)) { return 1; }  (value == 1 & title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA()

题目
单选题
public class ClassA {  public int getValue() {  int value=0;  boolean setting = true;  String title=”Hello”;  (value || (setting && title == “Hello”)) { return 1; }  (value == 1 & title.equals(”Hello”)) { return 2; }  }  } And:  ClassA a = new ClassA();  a.getValue();  What is the result?()
A

 1

B

 2

C

 Compilation fails.

D

 The code runs with no output.

E

 An exception is thrown at runtime.


相似考题
更多“public class ClassA {  public int getValue() {  int value=0;”相关问题
  • 第1题:

    下列程序的运行结果是【 】。 include class SomeClass { public: SomeClass(int va

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

    include <iostream. h>

    class SomeClass

    {

    public:

    SomeClass(int value) { some_value=value;};

    void show_data(void) { cout<<data<<"<<~some_value<<endl; };

    static void set_data(int value) {data=value; }

    private:

    static int data;

    int some_value

    };

    int SomeClass::data

    void main(void)

    {

    SomeClass my_class(1001),your_class(2002);

    your_class. set_data(4004);

    my_elass. show_data()

    }


    正确答案:4004 1001
    4004 1001 解析:本题考查静态成员变量在不同对象间的共享现象。无论哪个对象修改了其静态变量的值,其他对象再访问该变量时已经发生了变化。

  • 第2题:

    有如下程序:includeusing namespace std;classA{public:A(){cout<<"A";}};class B{pu

    有如下程序: #include<iostream> using namespace std; classA { public: A(){cout<<"A";} }; class B{public:B(){cout<<"B";}}; class C:public A { B b; public: C(){cout<<"C";} }; int main(){C obj;return 0;} 执行后的输出结果是( )

    A.ABC

    B.BAC

    C.ACB

    D.CBA


    正确答案:A

  • 第3题:

    有以下程序:includeusing namespace std;classA{private: int x;public: A(int a) {x

    有以下程序: #include<iostream> using namespace std; class A { private: int x; public: A(int a) { x=a; } friend class B; }; class B { public: void print(A a) { a.x--; cout<<a, x<<end1; } }; int main () { A a(10); B b; b.print (a) ; return 0; } 程序执行后的输出结果是( )。

    A.9

    B.10

    C.11

    D.12


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

  • 第4题:

    下列程序的运行结果是【 】。includeclass Sample{int a;public: Sample(int aa=0) {a

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

    include<iostream, h>

    class Sample

    {

    int a;

    public:

    Sample(int aa=0) {a=aa;}

    ~Sample() {cout<<"Sample="<<a<<;}

    class Derived: public Sample

    {

    int b;

    public:

    Derived(int aa=0, int bb=0): Sample(aa) {b=bb;}

    ~De rived() {cout <<"Derived="<<b<<'';}

    void main()

    {

    Derived dl (9)

    }


    正确答案:Derived=0 Sample=9
    Derived=0 Sample=9 解析:本题考察派生类和基类的构造函数,析构函数的执行顺序。

  • 第5题:

    有如下类声明: class Base{ protected: int amount; public: Base(int n=0):amount(n){} int getAmountconst{retum amount;} }; class Derived:public Base{ protected: int value; public: Derived(int m,int n):value(m),Base(n){} int getDataconst{return value+amount;} }: 已知x是一个Derived对象,则下列表达式中正确的是( )。

    A.x.value+X.getAmount

    B.x.getData一x.getAmount

    C.x.getData一x.amount

    D.x.value+X.amount


    正确答案:B
    本题考查公有继承中派生类对象对基类的访问属性。在公有继承中,派生类对象只能访问基类的公有成员,而不能}方问基类的保护成员和私有成员。题中x是派生类的对象,只能访问基类中公有的Base和getAmount成员,而不能访问保护类型的amount成员,故选项C、D错误。而类对象对类成员的访问也存在类似的情况,即类对象只能访问类的公有成员,而value是Derived的保护成员,所以A选项也错误。故答案为B。

  • 第6题:

    下面程序段的输出结果为 package test; public class A { int x=20; static int y=6; public static void main(String args[]) { Class B b=new Class B(); b.go(10); System.out.println(”x=”+b.x); } } class Class B { int x; void go(int y) { ClassA a=new ClassA(); x=a.y; } }

    A.x=10

    B.x=20

    C.x=6

    D.编译不通过


    正确答案:C
    解析:本题考查在Java中静态变量(类变量)的用法。在题目程序段中生成了一个staticinty=6类变量,在ClassA中调用的b.go(10),只不过是在ClassB中的一个局部变量,通过调用ClassB中的go方法可以生成一个ClassA对象,并给这个新生成的对象赋以ClassA中的类变量y的值。从main()方法作为入口执行程序,首先生成一个ClassB的对象,然后b.go(10)会调用ClassA,会给x和y赋值,x=a.y后,x值为6,再返回去执行System.out.println(”x=”+b.x)语句,输出为x=6,可见,正确答案为选项C。

  • 第7题:

    在下列源代码文件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

  • 第8题:

    public class ClassA{ public int getValue(){ int value=0; boolean setting=true; String title="Hello"; if(value||(setting && title=="Hello")){return 1;} if(value==1&title.equals("Hello")){return 2;} } } And: ClassA a=new ClassA(); a.getValue(); What is the result?()

    • A、1
    • B、2
    • C、Compilation fails.
    • D、The code runs with no output.
    • E、An exception is thrown at runtime.

    正确答案:C

  • 第9题:

    interface A { public int getValue() }  class B implements A {  public int getValue() { return 1; }  }  class C extends B {  // insert code here  }  Which three code fragments, inserted individually at line 15, make use of polymorphism?()

    • A、 public void add(C c) { c.getValue(); }
    • B、 public void add(B b) { b.getValue(); }
    • C、 public void add(A a) { a.getValue(); }
    • D、 public void add(A a, B b) { a.getValue(); }
    • E、 public void add(C c1, C c2) { c1.getValue(); }

    正确答案:B,C,D

  • 第10题:

    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

  • 第11题:

    单选题
    class ClassA {  public int numberOfinstances;  protected ClassA(int numberOfinstances) {  this.numberOflnstances = numberOfinstances;  }  }  public class ExtendedA extends ClassA {  private ExtendedA(int numberOfinstances) {  super(numberOflnstances);  }  public static void main(String[] args) {  ExtendedA ext = new ExtendedA(420);  System.out.print(ext.numberOflnstances);  }  }  Which is true?()
    A

     420 is the output.

    B

     An exception is thrown at runtime.

    C

     All constructors must be declared public.

    D

     Constructors CANNOT use the private modifier.

    E

     Constructors CANNOT use the protected modifier.


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

  • 第12题:

    单选题
    public class ClassA{ public int getValue(){ int value=0; boolean setting=true; String title="Hello"; if(value||(setting && title=="Hello")){return 1;} if(value==1&title.equals("Hello")){return 2;} } } And: ClassA a=new ClassA(); a.getValue(); What is the result?()
    A

    1

    B

    2

    C

    Compilation fails.

    D

    The code runs with no output.

    E

    An exception is thrown at runtime.


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

  • 第13题:

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

  • 第14题:

    请按下面注释的提示,将类B的构造函数定义补充完整。

    classA

    {

    int a;

    public:

    A(int aa=0) {a=aa;)

    };

    Class B:public A

    {

    int b;

    A c;

    public:

    //用aa初始化基数A,用aa+1初始化类对象成员c

    B(int aa): (b=aa+2)

    };


    正确答案:A(aa)c(aaq+1)或c(aa+1)A(aa)。
    A(aa),c(aaq+1)或c(aa+1),A(aa)。 解析: 题中要求用aa初始化基类A,用aa+1初始化类对象成员c,用的初始化列表的形式,即A(aa),c(aa+1)或c(aa+1),A(aa)。

  • 第15题:

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

    A.pblic 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 T1,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
    解析:本题主要考查类声明格式为[修饰符]class类名[extends父类名][implements类实现的接口列表],选项A中源文件名与程序名不相同,Java不支持多重继承所以选项C错误,选项D中类的访问权限不对,应为public。

  • 第16题:

    有如下类定义: Class MyClass{ int value; public: MyClass(int n): value(n){} int getValue()const{return value;} }; 则类MyClass的构造函数的个数是

    A.1个

    B.2个

    C.3个

    D.4个


    正确答案:A

  • 第17题:

    下面程序段的输出结果为( )。 package test; public class ClassA { int x=20: static int y=6; public static void main(String args[]) { ClassB b=new ClassB; go(10); System.out.println("x="+b.x); } } class ClassB { int X; void go(int y) { ClassA a=new ClassA; x=a.Y ; } }

    A.x=10

    B.x=20

    C.x=6

    D.编译不通过


    正确答案:C
    C。【解析】本题考查在Java中静态变量(类变量)的用法。在题目程序段中生成了一个staticinty=6类变量,在ClassA中调用的b.go(10),只不过是在ClassB中的一个局部变量,通过调用ClassB中的90方法可以生成一个ClassA对象,并给这个新生成的对象赋以ClassA中的类变量Y的值。从main方法作为入口执行程序,首先生成一个ClassB的对象,然后b.go(10)会调用ClassA,会给X和Y赋值,X=a.Y后,X值为6,再返回去执行System.out.println("x="+b.x)语句,输出为x=6,可见,正确答案为选项C。

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

    给定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题:

    Given the following interface definition, which definitions are valid?()   interface I {   void setValue(int val);   int getValue();   }    DEFINITION a:   (a) class a extends I {  int value;   void setValue(int val) { value = val;  }   int getValue() {  return value;  }   }   DEFINITION b:   (b) interface b extends I {   void increment();   }   DEFINITION c:   (c) abstract class c implements I {   int getValue() {  return 0;  }  abstract void increment();   }   DEFINITION d:   (d) interface d implements I {  void increment();  }   DEFINITION e:   (e) class e implements I {  int value;   public void setValue(int val) { value = val; }  }  

    • A、Definition a.
    • B、Definition b.
    • C、Definition c.
    • D、Definition d.
    • E、Definition e.

    正确答案:B,C

  • 第21题:

    public class Foo {  public int a;  public Foo() { a = 3; }  public void addFive() { a += 5; }  }  and:  public class Bar extends Foo { public int a;  public Bar() { a = 8; }  public void addFive() { this.a +=5; }  }  invoked with:  Foo foo = new Bar();  foo.addFive();  System.out.println(”Value: “+ foo.a);  What is the result?() 

    • A、 Value: 3
    • B、 Value: 8
    • C、 Value: 13
    • D、 Compilation fails.
    • E、 The code runs with no output.
    • F、 An exception is thrown at runtime.

    正确答案:A

  • 第22题:

    单选题
    public class Foo {  public int a;  public Foo() { a = 3; }  public void addFive() { a += 5; }  }  and:  public class Bar extends Foo { public int a;  public Bar() { a = 8; }  public void addFive() { this.a +=5; }  }  invoked with:  Foo foo = new Bar();  foo.addFive();  System.out.println(”Value: “+ foo.a);  What is the result?()
    A

     Value: 3

    B

     Value: 8

    C

     Value: 13

    D

     Compilation fails.

    E

     The code runs with no output.

    F

     An exception is thrown at runtime.


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

  • 第23题:

    单选题
    Which line contains a constructor in this class definition?()   public class Counter { // (1)   int current, step;   public Counter(int startValue, int stepValue) { // (2)   set(startValue);   setStepValue(stepValue);  }   public int get() { return current; } // (3)   public void set(int value) { current = value; } // (4)   public void setStepValue(int stepValue) { step = stepValue; } // (5)  }
    A

    Code marked with (1) is a constructor

    B

    Code marked with (2) is a constructor

    C

    Code marked with (3) is a constructor

    D

    Code marked with (4) is a constructor

    E

    Code marked with (5) is a Constructor


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

  • 第24题:

    单选题
    10. public class ClassA {  11. public void methodA() {  12. ClassB classB = new ClassB();  13. classB.getValue();  14. }  15. }  And:  20. class ClassB {  21. public ClassC classC;  22.  23. public String getValue() {  24. return classC.getValue();  25. }  26. }  And:  30. class ClassC {  31. public String value;  32.  33. public String getValue() {  34. value = “ClassB”;  35. return value;  36. }  37. }  Given:  ClassA a = new ClassA();  a.methodA();  What is the result?()
    A

     Compilation fails.

    B

     ClassC is displayed.

    C

     The code runs with no output.

    D

     An exception is thrown at runtime.


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