SCJP程序员认证考试

单选题public class WhileFoo {  public static void main (String []args)   {  int x= 1, y = 6;  while (y--)  {x--;}  system.out.printIn(“x=” + x “y =” + y);  }  }   What is the result?()AThe output is x = 6 y = 0BThe output is x = 7 y = 0CThe output is x = 6 y

题目
单选题
public class WhileFoo {  public static void main (String []args)   {  int x= 1, y = 6;  while (y--)  {x--;}  system.out.printIn(“x=” + x “y =” + y);  }  }   What is the result?()
A

 The output is x = 6 y = 0

B

 The output is x = 7 y = 0

C

 The output is x = 6 y = -1

D

 The output is x = 7 y = -1

E

 Compilation will fail.

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

第1题:

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

第2题:

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

第3题:

以下程序的运行结果为( )。 public class Sun { public static void main(String args[]) { String x="A",y="B",z="C"; for(int m=1;m<=2;m++) { x=y; y=z; z=x; } System.out.println(x+y+z); } }

A.ABC

B.BCA

C.BCB

D.CBC


正确答案:D
解析:本题考查对for循环语句的理解。 for循环语句在循环中分别将变量x、y、z的初值“A”、“B”、“C”重新赋值,第1次循环分别赋给变量x、y、z的值为“B”、“C”和“B”,第2次循环分别赋给变量x、y、z的值为“C”、“B”和“C”。故本题答案是D。

第4题:

执行如下语句之后,输出的结果是______。 public class ex36 { public static void main(String[] args) { int x=-6, y=6; x=x+y--; System.out.println (x); } }

A.-12

B.12

C.-1

D.0


正确答案:D

第5题:

有以下源程序: 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的值。

第6题:

设x和y均为int型变量,则执行下面的循环后,y值为( )。 public class Sun { public static void main(String args[ ]) { int x, y; for (y=1, x=1; y<=50; y++) { if(x>=10) break; if (x%2==1) { x+=5; continue; } x-=3; } System.out.println (y); } }

A.2

B.4

C.6

D.8


正确答案:C
解析:该题考查for循环和if条件语句的嵌套应用。当y=1,x=1时,不满足第1个if语句向下继续执行第2个if语句,x=6,继续执行for循环:当y=2,x=6时,不满足第1个和第2个if语句,x=3,继续执行for循环;当y=3,x=3时,不满足第1个if语句向下继续执行第2个if语句,x=8,继续执行for循环;一直执行下去,直到y=6,x=12是满足第1个 if语句退出for循环。故本题答案是C。

第7题:

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

第8题:

下面程序的输出为( )。 public class Test { public static void main (String args[]) { int x,y; x=1; y=2; System.out.println("The output is"+x+y); } }

A.The output is xy

B.The output is 3

C.The output is 12

D.The output is x=1 y=2


正确答案:C
解析:Java表达式的同级运算符从左到右进行,括号可以改变优先级。“+”在Java中既是算术的加号,也可以作为字符串的连接符号。“The output is”+x+y中,字符串“The output is”先与x做字符串的连接,结果字符串“The output is 1”再与y做字符串连接。

第9题:

执行如下语句之后,输出的结果是______。 public class ex24 { public static void main(String[] args) { int x=5,y=3; x+=X-- *--y; System.out.println{x); } }

A.0

B.1

C.true

D.false


正确答案:C

第10题:

以下程序的输出结果为:public class test {public static void main(String args[]) {int x=1,y=1,z=1;if (x--==1&&y++==1||z++== 1、System.out.println("x="+x+",y="+y+",z="+z);}}

A. x=0,y=2,z=1

B. x=1,y=2,z=1

C. x=0,y=1,z=1

D. x=0,y=2,z=2


正确答案:A

更多相关问题