叨叨两句
- 从明天开始,我要制作表格,结构化思考编程步骤
牛客网——java专项练习005
1
下面哪段程序能够正确的实现了GBK编码字节流到UTF-8编码字节流的转换:
byte[] src,dst;正确答案: B
A dst=String.fromBytes(src,"GBK").getBytes("UTF-8")
B dst=new String(src,"GBK").getBytes("UTF-8")C dst=new String("GBK",src).getBytes()D dst=String.encode(String.decode(src,"GBK")),"UTF-8" )操作步骤就是先解码再编码用new String(src,"GBK")解码得到字符串用getBytes("UTF-8")得到UTF8编码字节数组
2
What is the result of compiling and executing the following fragment of code:(C)
Boolean flag = false;if (flag = true){ System.out.println(“true”);}else{ System.out.println(“false”);}
A The code fails to compile at the “if” statement.
B An exception is thrown at run-time at the “if” statement.C The text“true” is displayed.D The text“false”is displayed.E Nothing is displayed.Boolean修饰的变量为包装类型,初始化值为false,进行赋值时会先调用Boolean.valueOf(boolean b)方法自动装箱,然后在if条件表达式中自动拆箱,因此赋值后flag值为true,输出文本true。 如果使用==比较,则输出文本false。if的语句比较,除boolean外的其他类型都不能使用赋值语句,否则会提示无法转成布尔值。
3
final、finally和finalize的区别中,下述说法正确的有?(A B)
A final用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承。
B finally是异常处理语句结构的一部分,表示总是执行。C finalize是Object类的一个方法,在垃圾收集器执行的时候会调用被回收对象的此方法,可以覆盖此方法提供垃圾收集时的其他资源的回收,例如关闭文件等。D 引用变量被final修饰之后,不能再指向其他对象,它指向的对象的内容也是不可变的深入理解java虚拟机中说到:当对象不可达后,仍需要两次标记才会被回收,首先垃圾收集器会先执行对象的finalize方法,但不保证会执行完毕(死循环或执行很缓慢的情况会被强行终止),此为第一次标记。第二次检查时,如果对象仍然不可达,才会执行回收。
4
public class NameList{ private List names = new ArrayList(); public synchronized void add(String name) { names.add(name); } public synchronized void printAll() { for (int i = 0; i < names.size(); i++) { System.out.print(names.get(i) + ””); } } public static void main(String[]args) { final NameList sl = new NameList(); for (int i = 0; i < 2; i++) { new Thread() { public void run() { sl.add(“A”); sl.add(“B”); sl.add(“C”); sl.printAll(); } } .start(); } }}Which two statements are true if this class is compiled and run?正确答案: E G A An exception may be thrown at runtime.B The code may run with no output, without exiting.C The code may run with no output, exiting normally(正常地).D The code may rum with output “A B A B C C “, then exit.E The code may rum with output “A B C A B C A B C “, then exit.F The code may ruin with output “A A A B C A B C C “, then exit.G The code may ruin with output “A B C A A B C A B C “, then exit.
在每个线程中都是顺序执行的,所以sl.printAll();必须在前三句执行之后执行,也就是输出的内容必有(连续或非连续的)ABC。而线程之间是穿插执行的,所以一个线程执行 sl.printAll();之前可能有另一个线程执行了前三句的前几句。E答案相当于线程1顺序执行完然后线程2顺序执行完。G答案则是线程1执行完前三句add之后线程2插一脚执行了一句add然后线程1再执行 sl.printAll();输出ABCA。接着线程2顺序执行完输出ABCABC输出加起来即为ABCAABCABC。
第一次println的字符个数肯定大于等于3,小于等于6;第二次println的字符个数肯定等于6;所以输出的字符中,后6位肯定是第二次输出的,前面剩下的就是第一次输出的。而且第一次的输出结果肯定是第二次输出结果的前缀。所以选E、G。