1、运行一下代码,将得到什么打印结果:
int i=3;int j=0;double k=3.2;if(j<k)if(i==j)System.out.println(i);elseSystem.out.println(j);//执行此语句,得到结果0elseSystem.out.println(k);2、以下代码是否编译通过?如果能编译通过,运行时得到什么打印结果?
int i=4;switch(i){ default; System.out.println("default");//执行此语句case 0: System.out.println("zero");//执行此语句break;case 1:System.out.println("one");case 2:System.out.println("two");}3、以下那些代码是合法的?
a)int i;for(i=5,int j=10;i<10;j--){}//错b)int i,j;for(i=0,j=10;i<10,j>0;i++,j--){}//错c)int i,k;for(i=0,k=9;(i<10&&k>0);i++,j--){}//错d)int i,jfor(i=0;j=10;i<10;i++,j--){}//错4、运行以下代码,将得到什么打印结果?
int i=1;switch(i){default:System.out.println("default");case 0:System.out.println("zero");break;case 1;System.out.println("one");//执行此语句case2;System.out.println("two");//执行此语句}5、以下代码是合法的?
a)
float x=1;//数据类型错误switch(x){case 1:System.out.println("Got a 1");//错}b)
long y=1;//数据类型错误switch(y){case 1:System.out.println("Got a 1");//错}c)
byte x=1;//低精转高精switch(x){case 1/1:System.out.println("Got a 1");//正确}d)
int x=1;int c=1;switch(c){case x;//需常量System.out.println("Got a 1");//错break;e)
short x=1;//数据类型错误switch(x){case 3.2/3:System.out.println("Got a 1");break;}f)
shout x=1;//数据类型错误switch(x){case 1,2,3:System.out.println("Got a 1");//错break;}6、以下代码是否编译通过?如果能编译通过,运行时将得到什么打印结果?
public class Myswitch{ public static void main(String args[]){ Myswitch ms=new Myswitch(); ms.amethod(); } public void amethod(){ for(int a=0,b=0;a<2;b=++a,System.out.println("b="+b)){ System.out.println("a="+a); } }}结果:a=0
b=1
a=1
b=2
7、以下代码是否编译通过?如果能编译通过,运行时将得到什么打印结果?
void looper(){ int x=0; one: while(x<10){ two: System.out.println(++x); if(x>3) break two; }}//无法编译,break two;无法返回到two标签8、以下代码是否编译通过?如果能编译通过,运行时将得到什么打印结果?
public class Hope{ public static void main(String args[]){ Hope h=new Hope(); } protected Hope(){ int i=1; do{ System.out.println(i); }while(++i<3); }}结果:1 2
tobehero666.png