错题整理——细节小题目

1、以下代码执行的结果显示是多少()?

https://uploadfiles.nowcoder.net/images/20170815/5994168_1502783973150_A3AECBB367EA2A16CE5EC419126BC346

  • 505000
  • 0
  • 运行时错误
  • 5050

答案:B

解析count = count++  原理是 temp = count count = count+1 count = temp   因此count始终是0

2、检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。

package algorithms.com.guan.javajicu; 

public class Inc { 

    public static void main(String[] args) { 

       Inc inc = new Inc(); 

       int i = 0; 

       inc.fermin(i); 

       i= i ++; 

       System.out.println(i);

    

    } 

    void fermin(int i){ 

       i++; 

    } 

}

  • 0
  • 1
  • 2
  • 3

解析:1、最后打印的是i++的值          2、fermin方法没有影响到i的值

https://www.nowcoder.com/questionTerminal/b507628f3f0b4c85a00a7ed0de830413

3、以下代码对其执行后,NumberList ayList<Integer>();

NumberList.add(2);

NumberList.add(4);

NumberList.add(1);

NumberList.add(3);

NumberList.add(5);

for(int i =0;i<NumberList.size();++i){

    int v = NumberList.get(i);

    if(v%2==0){

        NumberList.remove(v);

    }

}

System.out.println(NumberList);

  • 2,4,1,3,5
  • 2,1,3,5
  • 4,1,3,5
  • 会出现越界情况

解析:这题主要考察两点:

1.ArrayList删除元素后,剩余元素会依次向前移动,因此下标一直在变,size()也会减小;

2.remove()方法调用的是remove(int index),而不是remove(Object o),因此删除的是index索引处的元素;

该题具体流程:1.i=0,v=2,remove(2)删除掉了元素1,因此NumberList剩余元素为【2,4,3,5】;

                  2.i=1,v=4,remove(4),此时线性表中只有四个元素,不可能删除索引为4的元素,因此会报数组下标越界异常。

 

4、What is the result of compiling and executing the following fragment of code:

Boolean flag = false;

if (flag = true)

{

    System.out.println(“true”);

}

else

{

    System.out.println(“false”);

}
The code fails to compile at the “if” statement.

  • An exception is thrown at run-time at the “if” statement.
  • The text“true” is displayed.
  • The text“false”is displayed.
  • Nothing is displayed.

解析:if(flag=true){ } 中,只是一个赋值语句,隐含条件为  if((flag=true)==true){ }

 

5、下面代码将输出什么内容:()

public class SystemUtil{

    public static boolean isAdmin(String userId){

        return userId.toLowerCase()=="admin";

    }

    public static void main(String[] args){

        System.out.println(isAdmin("Admin"));

    }

}

答案:false

解析: 在源码中 toLowerCase 是重新 new String()

           == 是比较对象是否是同一个对象,所以为 false 。

https://uploadfiles.nowcoder.net/images/20170314/184838_1489466288980_4A47A0DB6E60853DEDFCFDF08A5CA249  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值