黑猴子的家:Java SE 练习题第三章

尾部都有答案

第三章练习题(流程控制与数组)

(1)现有

Class TestApp{
    public  static  void main (String[]  args){
        for (int  i=0; i<lO;i++){
            if(i==3)
                Break;
            System. out .print (i);
        }
    }
}

程序运行后的输出是哪项?

A.0123
B.012456789
C.  0123456789
D.  012

(2)程序

Class   TestApp{
    public  static  void main (String[]  args){
        int x=6;
        if (x>l)
            System. out. println("x>l");
        else if (x>5)
            System. out .println("x>5");
        else if (x<10)
            System. out. println("x<lO");
        else if (x<29)
            System. out .println( "x<29");
        else
            System. out.println(“以上都不是”);
    }
}

上述程序运行后的结果是哪项?

A.x>5
B.x>l
C.  x<10
D.x<29 

(3)现有

Class TestApp{
    public  static  void main (String[]  args){
        int [5]   myarray={ 10, 11, 12, 13, 14);
        int sum=0;
        for (int x  :  myarray)
            sum+=x;
        System. out. println( "sum= "+sum);
    }
}

上述程序运行后的结果是哪项?

A. sum=10
B. sum=70
C. sum=60
D. 运行时抛出异常

(4)下列有关数组的声明中,正确的是哪项?(选两项)

A. int s[10];
B. int[10]  s;
C. int[5] s={l,2,3,4,5};
D. int s[];

(5)已知数组array,其最后一个元素的下标是?

A. array. size
B. array. length-l
C. array. size-l
D. array.length

(6)程序

Class    TestApp{
    public  static void main (String[]  args){
        int X=5:
        switch (x){
            Case l:
            Case 2:
            Case 3:
                System. out.println(“一季度”);
                Break;
            Case 4:
            Case 5: 
            Case 6:
                System. out.println(“二季度”);
                Break;
            Default:
                System. out.println(“三季度以上”);
                Break;
        )
    }
)

上述程序运行后的结果是哪项?

A. 一季度
B. 二季度
C. 三季度以上
D. 无输出

(7)为将数组myArray的长度由3改为6,现采取以下编码

int[]myArray = new int [3];
myArray = new  int[6];

代码执行后,以下叙述哪项是正确的?

A. 数组myArray的长度已由3改为6,其中前3个元素的值不变,后3个元素
   的值为空

B. 数组myArray的长度已由3改为6,其中前3个元素的值不变,后3个元素
   需再经过初始化后才能使用

C. 数组myArray的长度没有变化

D. 数组myArray的长度已由3改为6,原来3个元素的值全部丢失

(8)现有

class  Ifs  {
    public  static void main (String  []  args)  {
        boolean state=false;
        int i=2;
        if( (++i>2)  &&  (state=true))
            i++;
        if( (++i>4)  l l  (state=false))
            i++;
        System.out .println (i);
    }
} 

结果为

A. 6
B. 5
C. 4
D. 编译失败

(9)现有

public class Tester {
    public static void main (String[] args)  {
        intx-5;
        Integer xl=x;  Integer x2=x;
        int x3=new Integer(5);
        system..ut.print(x1.equals(x));
        system..ut.print(xl==x);
        system..ut.print(x2.equals(xl));
        system..ut.print(x2==xl);
        system..ut.print(x2==x3);
        system..ut.print(x2.equals(x3));
    }
}

结果为

A. 编译失败
B. falsefalsetruetruetruetrue
C. truetruetruetruetruetrue
D. falsefalsetruetruetruefalse
E. truefalsetruefalsefalsetrue
F. 运行时异常被抛出

(10)现有

class Rectangle  {
    public static V.id main(string  []  args)    {
        int[]x=(1,2,3);
        x[1]=  (x[1]>1)  ?x[2]:  O;
        System.out.println(x[1]);
    }
} 

结果为

A.3
B.2
C.1
D.0

(11)现有

class Output  (
    public static void main (String[]  args)    {
        int i=5:
        System.out.print( "4"+i+"");
        System.out.print (i+5+"7");
        System.out.println  (i+"8");
    }
}

结果为

A. 9 9722
B. 9 55758
C. 45 10758
D. 45 9722

(12)以下哪种初始化数组的方式是错误的?

A. String[]names={"zhang",   "wang",   "li");
B. String  names[]  =new  String [3];
    names [O]  ="zhang";  names [1]  ="wang";   names [2]  ="li";
C. String[3] names={"zhang", "wang", "li"};
D. 以上皆正确

(13)现有

class WhileTests  {
    public  static void main (String  []  args)  {
        int X=5;
        while (++x<4)  {
            --x;
        }
        System.out.println( "x="+x);
    }
}

结果是什么?

A. X=6
B. X=5
C. X=2
D. 编译失败

(14)现有

class Test2  {
    public static void main (String  []  args)  {
        Boolean X= true;
        Boolean y=false;
        short Z=20;
        if((x==true)  &&  (y=true))  z++;
        if((y==true) ||  (++z==22))  z++;
        System. out .println( "z="+z);
    }
}

结果是什么?

A. Z=21
B. z=22
C. z=23
D. Z= 24

(15)现有

class Foo  {
    public static void main (String  []  args)  {
        int x=O;
        int y=4;
        for (int  z=0;  z<3;  Z++;  X++)  {
            if(x>1&++y<10)
            y++;
        }
        System. out .println (y);
    }
}

结果是什么?

A. 7
B. 8
C. 10
D. 12 

参考答案

1、D
2、B
3、C
4、CD
5、B
6、B
7、D
8、A
9、C
11、C
12、C 
13、A
14、B
15、B 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值