【266天】我爱刷题系列(25)

叨叨两句

  1. 从明天开始,我要制做表格,结构化思考编程步骤

牛客网——java专项练习005

1

下面哪段程序可以正确的实现了GBK编码字节流到UTF-8编码字节流的转换:
byte[] src,dst;java

正确答案: 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)ui

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.this

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。