单选题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 = 0B  The output is x = 7 y = 0C  The output is 

题目
单选题
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 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

第5题:

执行如下语句之后,输出的结果是______。 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

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

第10题:

阅读以下C++代码,填充(1)~(5)的空缺,将解答填入答题纸的对应栏内。 【说明】在下面程序横线处填上适当的字句,使其输出结果为:x=5x=6y=7x=8z=9【程序】#include<iostream.h>class X1{int x;(1):X1(int xx=0){x=xx;}(2)void Output()(cout<<"x="<<x<<end;}};(3)Y1:public X1{int y;public:Y1(int xx=0,int yy=0):X1(xx){y=yy;}(2)void Output(){(4)Output();cout<<"y="<<y<<end1;}};class Z1:pubtic X1{int z:(5):Z1(int xx=0,int zz=0):X1(xx){z=zz;}②void Output(){X1::Output();cout<<"z="<<z<<end1;}};void main(){X1 a(5);Y1 b(6,7);Z1 c(8,9);X1*p[3]={&a,&b,&c};For(int i=0;i<3;i++){p[i]-->Output();cout<<end1;}}


答案:
解析:
(1)public
(2)virtual
(3)class
(4)X1::
(5)public
【解析】

通过对比三个类的定义就可以发现,在类X1和Z1的定义中缺少类的成员属性声明,而类一般将成员变量声明为公有的、私有的或受保护的三种类型中的一种,在类的定义中,我们一般将类的构造函数放在公有的属性下面,在题目中只能选择公有的属性了,因此,第1空和第5空中应该填“public”。对三个类的定义进行仔细观察后,我们同样可以发现,每个类中都定义了一个同名函数Output(),而且在后两个类的函数体中调用了函数Output(),由此,我们应该想到虚函数。虚函数的作用是允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或引用来访问基类和派生类中的同名函数。因此,第2空应该填“virtual”。第3空就简单了,考查类的定义,应该填类的标识符“class”。从程序中我们可以看到,类Y1和Z1都以公有的方式继承类X1。从输出的结果来分析,类Y1和Z1都输出了两个数,但单从类Z1的函数来看,只能输出一个变量的值z,因此,可以发现在类Z1中应该和类Y1一样,都调用了类X1的函数Output(),因此,第4空的答案为“X1::”。

更多相关问题