单选题package test1;  public class Test1 {  static int x = 42;  }  package test2;  public class Test2 extends test1.Test1 {  public static void main(String[] args) { System.out.println(“x = “ + x);  }  }  What is the result?()A  x = 0B  x = 42C  Compilation 

题目
单选题
package test1;  public class Test1 {  static int x = 42;  }  package test2;  public class Test2 extends test1.Test1 {  public static void main(String[] args) { System.out.println(“x = “ + x);  }  }  What is the result?()
A

 x = 0

B

 x = 42

C

 Compilation fails because of an error in line 2 of class Test2.

D

 Compilation fails because of an error in line 3 of class Test1.

E

 Compilation fails because of an error in line 4 of class Test2.

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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

第2题:

有以下源程序: package test; public class ClassA { int x=20; static int y=6; public static void main(String args[]) { ClassB b=new ClassB(); b.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
解析:本题考查在Java中静态变量(类变量)的用法规则。对于static修饰的成员变量和成员方法,可以直接使用类名对它们进行访问。对于类变量,也就是static修饰的变量,在生成类的第一个实例对象时,Java运行时,系统对这个对象的每个类变量分配一块内存,以后再生成该类的实例对象时,所有实例对象将共享同一个类变量,每个实例对象对类变量的改变都会直接影响到其他实例对象,类变量除了可以通过类名直接访问外,还可以通过实例对象来访问。在本例中生成了一个staticinty=6类变量,在ClassA中调用的b.go(10),只不过是ClassB中的一个局部变量,通过调用ClassB中的go方法可以实现生成一个ClassA对象,并给这个新生成的对象赋予ClassA中的类变量y的值。

第3题:

下面程序段的输出结果为 package test; public class ClassA { int x=20; static int y=6; public static void main(String args[]) { ClassB b=new ClassB(); b.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
解析:本题考查在Java中静态变量(类变量)的用法。在题目程序段中生成了一个static int y=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。

第4题:

现有:  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、编译失败

正确答案:C

第5题:

package test1;  public class Test1 {  static int x = 42;  }  package test2;  public class Test2 extends test1.Test1 {  public static void main(String[] args) { System.out.println(“x = “ + x);  }  }  What is the result?() 

  • A、 x = 0
  • B、 x = 42
  • C、 Compilation fails because of an error in line 2 of class Test2.
  • D、 Compilation fails because of an error in line 3 of class Test1.
  • E、 Compilation fails because of an error in line 4 of class Test2.

正确答案:C

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

以下程序调试结果为:

public class Test {

int m=5;

public void some(int x) {

m=x;

}

public static void main(String args []) {

new Demo().some(7);

}

}

class Demo extends Test {

int m=8;

public void some(int x) {

super.some(x);

System.out.println(m);

}

}

A.5

B.8

C.7

D.无任何输出

E.编译错误


正确答案:B

第8题:

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 来明确。

第9题:

interface DeclareStuff{  public static final int EASY = 3;  void doStuff(int t); }  public class TestDeclare implements DeclareStuff {  public static void main(String [] args) {  int x=5;  new TestDeclare().doStuff(++x);  }  void doStuff(int s) {  s += EASY + ++s;  System.out.println(”s “ + s);  }  }  What is the result?() 

  • A、 s 14
  • B、 s 16
  • C、 s 10
  • D、 Compilation fails.
  • E、 An exception is thrown at runtime.

正确答案:D

第10题:

class Super {  public int i = 0;  public Super(String text) {  i = 1; }  }  public class Sub extends Super {  public Sub(String text) {  i = 2;  }   public static void main(String args[]) {  Sub sub = new Sub(“Hello”);  System.out.println(sub.i);  }  }  What is the result?()  

  • A、 0
  • B、 1
  • C、 2
  • D、 Compilation fails.

正确答案:C

更多相关问题