OJ常见代码分析题

1、运行一下代码,将得到什么打印结果:

int i=3;
int j=0;
double k=3.2;
if(j<k)
if(i==j)
System.out.println(i);
else
System.out.println(j);//执行此语句,得到结果0
else
System.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,j
for(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


11997621-6bd854590246e979.png
tobehero666.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值