单选题现有:  class Banana2  f      static int X=2;  public static void main (String  []  args)  {     int X=2;   Banana2 b=new Banana2();     b.go(x);     }  static  {x+=x; }   void go (int x)  {    ++x;  System. out.println (x);    }         结果为:()A 7B 5C 3D

题目
单选题
现有:  class Banana2  f      static int X=2;  public static void main (String  []  args)  {     int X=2;   Banana2 b=new Banana2();     b.go(x);     }  static  {x+=x; }   void go (int x)  {    ++x;  System. out.println (x);    }         结果为:()
A

7

B

5

C

3

D

2


相似考题
更多“现有:  class Banana2  f      static int X=2;  public static vo”相关问题
  • 第1题:

    interface A{

    int x = 0;

    }

    class B{

    int x =1;

    }

    class C extends B implements A {

    public void pX(){

    System.out.println(x);

    }

    public static void main(String[] args) {

    new C().pX();

    }

    }


    正确答案:

     

    错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

    x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

    对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

    以通过A.x 来明确。

  • 第2题:

    有如下程序:include using namespace std;int s=0;class sample { static int n;publ

    有如下程序: #include <iostream> using namespace std; int s=0; class sample { static int n; public: sample(int i) { n=i; } static void add() { s+=n; } };

    A.2

    B.5

    C.7

    D.3


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。程序中定义一个类sample,它包括一个静态数据成员n和一个静态成员函数add,并在类的构造函数中给类私有静态数据成员n赋值。在主函数main中,定义对象a(2)时,通过构造函数使静态数据成员n的值变为2,在定义对象b(5)时,通过构造函数使静态数据成员n=5(覆盖了前面的n=2),再执行sample::add()使全局变量s=5。

  • 第3题:

    已知有下列类的说明,则下列哪个语句是正确的? ( ) public class Test { private float f=1.0f; int m=2; static int n=1; public static void main(String arg[]) { Test t=new Test(); } }

    A.t.f;

    B.this,n;

    C.Test.m;

    D.Test.f;


    正确答案:A

  • 第4题:

    下面的例子中

    using System;

    class A

    {

    public static int X;

    static A(){

    X=B.Y+1;

    }

    }

    class B

    {

    public static int Y=A.X+1;

    static B(){}

    static void Main(){

    Console.WriteLine("X={0},Y={1}",A.X,B.Y);

    }

    }

    产生的输出结果是什么?


    正确答案:
    答:x=1,y=2

  • 第5题:

    现有:   class TestMain {      static int x = 2;  static { x = 4; }   static public void main(String[] args) {    int y = x + 1;   System.out.println(y);    }    }    和命令行:  java TestMain    结果为:() 

    • A、 3
    • B、 5
    • C、 编译失败
    • D、 运行时异常被抛出

    正确答案:B

  • 第6题:

    class Top {  static int x = 1;  public Top(int y) { x *= 3; }  }  class Middle extends Top {  public Middle() { x += 1; }  public static void main(String [] args) {  Middle m = new Middle();  System.out.println(x);  }  }  结果为:() 

    • A、1
    • B、2
    • C、3
    • D、编译失败

    正确答案:D

  • 第7题:

    现有:  class Top  {  static int x=l;  public Top (inty)  {  x*=3;  }      }  class Middle extends Top {      public Middle()  {x+=1;  )  public  static void main (String  []  args)  {      Middle m = new Middle();      System. out .println (x);      }     }      结果为:()     

    • A、1
    • B、2
    • C、3
    • D、编译失败

    正确答案:D

  • 第8题:

    单选题
    现有:  class Top  {     static int X=l;  public Top()  {  x*=3; }     }  class Middle extends Top  {      public  Middle()    {x+=l;  }  public static void main(String  []  args)  {     Middle m=new Middle();    System.out.println (x);    }     }  结果是什么?()
    A

      2

    B

      3

    C

      4

    D

    编译失败


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

  • 第9题:

    单选题
    class TestMain {       static int x = 2;    static { x = 4; }   public static void main(String... args) {     int y = x + 1;   System.out.println(y);    }  }    和命令行:  java TestMain   结果为:()
    A

     3

    B

     5

    C

     编译失败

    D

     运行时异常被抛出


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

  • 第10题:

    单选题
    现有:  class Passer f  static final int X=5;  public  static void main (String  []  args)  {      new  Passer().go (x);      System. out .print (x);      }  void go (int x)  {     System. out .print(x++);     }     结果是什么?()
    A

    55

    B

    56

    C

    65

    D

    66


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

  • 第11题:

    单选题
    现有:  class Top  {  static int x=l;  public Top (inty)  {  x*=3;  }      }  class Middle extends Top {      public Middle()  {x+=1;  )  public  static void main (String  []  args)  {      Middle m = new Middle();      System. out .println (x);      }     }      结果为:()
    A

    1

    B

    2

    C

    3

    D

    编译失败


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

  • 第12题:

    单选题
    现有:  class Banana2  f      static int X=2;  public static void main (String  []  args)  {     int X=2;   Banana2 b=new Banana2();     b.go(x);     }  static  {x+=x; }   void go (int x)  {    ++x;  System. out.println (x);    }         结果为:()
    A

    7

    B

    5

    C

    3

    D

    2


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

  • 第13题:

    下面程序执行的结果是【 】 include using namespace std; class A{ public: static in

    下面程序执行的结果是【 】

    include<iostream>

    using namespace std;

    class A{

    public:

    static int x;

    A(inty){cout<<x+y;}

    };

    int A::x=2;

    void main(){

    A a(5);

    }


    正确答案:7
    7 解析:程序的静态变量初始化为2,而构造函数招待过程中y变量为初始化为5,故程序执行的结果为7。

  • 第14题:

    有以下程序:includeusing namespace std;class sample{private:int x;static int y;

    有以下程序: #include<iostrearn> using namespace std; class sample { private: int x; static int y; public: sample (int A) ; static void print (sample s); }; sample::sample(int A) { x=a; y+=x; }

    A.x=10,y=20

    B.x=20,y=30

    C.x=30,y=20

    D.x=30,y=30


    正确答案:B
    解析:本题考核静态数据成员和静态成员函数的应用。类sample中定义两个私有成员x和y,其中y为静态数据成员。并定义函数print()为静态成员函数。在主函数中,定义对象s1(10)时,通过构造函数使对象s1的私有成员x=10,静态数据成员y=10。定义s2(20)时,通过构造函数使对象s2的私有成员x=20,静态数据成员y=10+20=30。程序最后调用静态成员函数print输出对象s2的私有成员x的值20,对象s1、s2共享的静态数据成员y的值30。

  • 第15题:

    下面程序段的输出结果为 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。

  • 第16题:

    以下程序的运行结果为:public class A {static int k=3;public static void main(String[] args) {int k=4;A x1=new A();x1.k++;A x2=new A();x2.k++;k++;System.out.println(x1.k);}}

    A. 3

    B. 4

    C.5

    D.6

    E.7


    正确答案:C

  • 第17题:

    现有: 1.class Passer f 2.static final int X=5; 3.public static void main (String [] args) { 4.new Passer( ).go (x); 5.System. out .print (x); 6. ) 7.void go (int x) { 8.System. out .print(x++); 9.} 10.}结果是什么?()

    • A、55
    • B、56
    • C、65
    • D、66

    正确答案:A

  • 第18题:

    class TestMain {   static int x = 2;   static { x = 4; }   static public void main(String[] args) {   int y = x + 1;   System.out.println(y);  }  }   和命令行:   java TestMain   结果为:()

    • A、 3
    • B、 5
    • C、 编译失败
    • D、 运行时异常被抛出

    正确答案:B

  • 第19题:

    class TestMain {       static int x = 2;    static { x = 4; }   public static void main(String... args) {     int y = x + 1;   System.out.println(y);    }  }    和命令行:  java TestMain   结果为:()  

    • A、 3
    • B、 5
    • C、 编译失败
    • D、 运行时异常被抛出

    正确答案:B

  • 第20题:

    单选题
    public class TestSeven extends Thread {  private static int x;  public synchronized void doThings() {  int current = x;  current++;  x = current;  }  public void run() {  doThings();  }  }  Which is true?()
    A

     Compilation fails.

    B

     An exception is thrown at runtime.

    C

     Synchronizing the run() method would make the class thread-safe.

    D

     The data in variable “x” are protected from concurrent access problems.

    E

     Declaring the doThings() method as static would make the class thread-safe.

    F

     Wrapping the statements within doThings() in a synchronized(new Object()) {} block would make the class thread-safe.


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

  • 第21题:

    单选题
    现有:   class TestMain {       static int x = 2;    static { x = 4; }   public static void main(String... args) {     int y = x + 1;   System.out.println(y);   }   }    和命令行:  java TestMain    结果为:()
    A

     3

    B

     5

    C

     编译失败

    D

     运行时异常被抛出


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

  • 第22题:

    单选题
    现有:   class TestMain {      static int x = 2;  static { x = 4; }   static public void main(String[] args) {    int y = x + 1;   System.out.println(y);    }    }    和命令行:  java TestMain    结果为:()
    A

     3

    B

     5

    C

     编译失败

    D

     运行时异常被抛出


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

  • 第23题:

    多选题
    现有:  public  class  TestDemo{     private int X-2;      static int y=3;  public  void method(){      final int i=100;      int j  =10;     class Cinner {  public void mymethod(){      //Here     }     }     }     } 在Here处可以访问的变量是哪些?()
    A

    X

    B

    y

    C

    j

    D

    i


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

  • 第24题:

    多选题
    public class TestDemo{   private int x = 2;   static int y = 3;   public void method(){   final int i=100;   int j = 10;   class Cinner{   public void mymethod(){  //Here  }  }  }  }   在Here处可以访问的变量是哪些?()
    A

    x

    B

    y

    C

    i

    D

    j


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