OJ常见代码分析题

作者 : 开心源码 本文共1735个字,预计阅读时间需要5分钟 发布时间: 2022-05-14 共322人阅读

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

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » OJ常见代码分析题

发表回复