1、以下代码执行的结果显示是多少()?
- 505000
- 0
- 运行时错误
- 5050
答案:B
解析:count = count++ 原理是 temp = count; count = count+1 ; count = temp; 因此count始终是0
2、检查程序,是否存在问题,如果存在指出问题所在,如果不存在,说明输出结果。
|
- 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 。