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

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

 

    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.现有:

    1.  class  Ifs  {

    2.public  static void main (String  []  args)  {

    3.boolean state=false;

    4.    int i=2;

    5.if( (++i>2)  &&  (state=true))

    6.i++;

    7.if( (++i>4)  l l  (state=false))

    8.i++;

    9.System.out .println (i);

    10.    }

    11.  }

 

    

    结果为:

    A.  6

    B.  5

    C.  4

    D.编译失败

    

9.现有:

    3.  public class Tester {

    4.public static void main (String[] args)  {

    5.    intx-5;

    6.    Integer xl=x;  Integer x2=x;

    7.int x3=new Integer(5);

    8.    system..ut.print(x1.equals(x));

    9.    system..ut.print(xl==x);

    lu.    system..ut.print(x2.equals(xl));

    11.    system..ut.print(x2==xl);

    12-    system..ut.print(x2==x3);

    13-    system..ut.print(x2.equals(x3));

    “    l4.}

    15.  }

    结果为:

    A.编译失败

    B.  falsefalsetruetruetruetrue

    C. truetruetruetruetruetrue

    D.  falsefalsetruetruetruefalse

    E.  truefalsetruefalsefalsetrue

    F.运行时异常被抛出

    

10.现有:

    1.  class Rectangle  {

    2'    public static V.id main(string  []  args)    {

    3.   int[]x=(1,2,3);

    4.   x[1]=  (x[1]>1)  ?x[2]:  O;

    5.    System.out.println(x[1]);

    6.    }

    7.    }

 

    结果为:

    A.3

    B.2

    C.1

    D.0

    

11.现有:

    1.class Output  (

    2.public static void main (String[]  args)    {

    3.    int i=5:

    4.System.out.print( "4"+i+"");

    5.System.out.print (i+5+"7");

    6.System.out.println  (i+"8");

    7.    }

    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.现有:

    1.  class WhileTests  {

    2.public  static void main (String  []  args)  {

    3.    int X=5;

    4.    while (++x<4)  {

    5.--x;

    6.    }

    7.System.out.println( "x="+x);

    8.    }

    9.  }

 

    结果是什么?

    A.X=6

    B.  X=5

    C.X=2

    D.编译失败

    

14.现有:

    1.  class Test2  f

    2.public static void main (String  []  args)  {

    3.    boolean X= true;

    4.    boolean y=false;

    5.    short Z=20;

    6.

    7.if((x==true)  &&  (y=true))  z++;

    8.    if((y==true) ||  (++z==22))  z++;

    9.

    10.    System. out .println( "z="+z);

    11.    }

    12.  }

    结果是什么?

    A.Z=21

    B.  z=22

    C.  z=23

    D.  Z= 24

    

15.现有:

    1.  class Foo  {

    2.public static void main (String  []  args)  {

    3.    int x=O;

    4.    int y=4;

    5.for (int  z=0;  z<3;  Z++;  X++)  {

    6.    if(x>1&++y<10)

    7.    y++;

    8.    }

    9.System. out .println (y);

    10.    }

    11.  }

    结果是什么?

    A.7

    B.8

    C.10

    D.12

 

                            I D

                            2B

                            3C

                            4CD

                            5B

                            6B

                            7D

                            8A

9C

10A

                            11 C

                            12 C

                            13 A

                            14 B

                            15 B

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值