lljs的J@Whiz1.4笔记

来源:互联网 发布:淘宝活性炭都是假的吗 编辑:程序博客网 时间:2024/05/17 23:01

Test1

1、  要想让assertion对整个包都有效用:java -ea:包名... <MAIN_CLASS_NAME>

2、  AssertionError有七个构造方法,参数类型有:int,char,long,boolean,double,floatObject

3、  char is the only unsigned integral primitive type in Java.

4、  接口不能用abstract修饰

5、  子类要覆盖父类中的方法,其权限不能低于父类的访问权限,可以和父类方法的访问权限一样或高于父类的访问权限

6、  64转换为16进制输入:(1System.out.println(Integer.toString(64, 16));2System.out.println(Integer.toHexString(64));

7、  Finalize()的原形:protected void finalize() throws Throwable

8、  在线程中执行yield()方法,如果没有比当前线程优先级高的线程则继续执行当前线程

9、  本地方法不能有方法体(和抽象方法、接口中的方法一样不能有方法体)

10、              Substring(begin,end)取的是end – begin个字符,字符是从0开始计的

11、              抽象方法不能用abstractfinal修饰

12、              wait()      sleep()     suspend()              yield()均可以引起一个线程的停止

13、              trim()是消除字符中前导和后导空格

14、              两个线程同时运行,顺序依赖于操作系统和JVM

15、              Null可以做为Boolean的参数,输入的Boolean值为false,还可以用非truefalse的字符进行初始化,默认值为false

16、               

 

Test2

1、如果在一个Thread类中找不到public void run(){},启动线程没有任何错误,但也没有任何输出

2、在switch语句块里,不管default放在哪,和执行顺序没有任何关系

3、当类里没有任何构造方法的时候才会调用默认的无参构造方法

4static-->static(OK)static-->nonstatic(error)nonstatic-->static(error)

5HashMap is thread-safe whereas Hashtable is not

10Which of the following assignment statements is invalid?

Adouble D = 45456.444;

Blong L = 45784;

Cint I = L;

Dint J = (int)D;

答案:C

因为不能将高精度的类型转换到低精度的类型

 

14What will happen when you attempt to compile and run the following code snippet?

 

Hashtable table = new Hashtable();

       table.put("Java", "Platform Independence");

       table.put("Java", "Great Language!");

       System.out.println("Java : " + table.get("Java"));

 

AThe code will not compile

BIt will print-Java : Platform Independence

CIt will print-Java : Great Language!

DIt will print-Java : Platform IndependenceGreat Language!

ERuntime Exception

 

答案:C

解释:

C is the correct choice. Hashtable implements interface Map. The put method specified by Map states that if the map previously contained a mapping for a particular key ("Java" here), the old value would be replaced by the new value specified. Hence, "Great Language!" replaces the value "Platform Independence" for the key "Java".

Also, the put method of Hashtable returns previous value associated with specified key, or null if there was no mapping for key.

中文解释:因为key(“Java”, "Great Language!”)覆盖了前面的("Java", "Platform Independence");

 

15AssertionError是继承于Error类,属于lang包,lang包是自动引入的,所以不用再引入什么包了,记住了???

 

16Given the following code segment. In what order will the output be produced?

 

class classC extends classA

{

       public classC()

       {

               System.out.println("C");

        }

}

 

class classA

{

       public classA()

       {

               System.out.println("A");

        }

}

 

class classB extends classC

{

       public classB()

       {

              System.out.println("B");

       }

}

 

public class ABC

{

       public static void main(String args[])

       {

              System.out.println("Main");

              classB c = new classB();

 

       }

}

AC, B, B, Main

BMain, A, C, B

CMain, A, B, C

DMain, A, B, A

正确答案:B

解释:B is correct. Java's construction order is from bottom to up, from the child class constructor the parent's constructor will be called. Thus, the order would be Main, A, C, B.

中文解释:这样的题只要记住一句话:在new一个父类之前,要首先初始化父类(只执行父类的构造方法),然后才初始化子类(执行构造方法)。多简单呀

 

20Given the following code, what will be the output?

class Value

{

       public int i = 15;

}

 

public class Test2

{

       public static void main(String argv[])

       {

              Test2 t = new Test2();

              t.first();

         }

 

       public void first()

       {

                int i = 5;

                Value v = new Value();

              v.i = 25;

              second(v, i);

                  System.out.println(v.i);

 

       }

 

       public void second(Value v, int i)

       {

              /*if(v instanceof Value)

                     System.out.println("true");*///这里将会输出true

              i = 0;

                v.i = 20;

              Value val = new Value();

                v =  val;

              System.out.println(v == val);//这里将会输入true,看来valv是指向的一个内在地址了,怪不得把val赋给v后,输入v.i还是15呢,呵呵

              但在输出i的时候怎么输出的不是5呢,JAVA里的引用都传值,不是传的地址,下面这行语句输出的是局部变量i的值I = 0

               System.out.println(v.i + " " + i);

                

       }

}

 

21Which of the following are static methods of Thread class?

Asleep

Byield

Cwait

Dnotify

答案:AB

解释:sleepyield属于Thread的方法,但waitnotify属于Object包,Thread继承于Object,怪不得在Thread类里可以用waitnotify呢。^_^

 

关于interface:

1.interface前面只能有两种修饰符:abstractpublic

2.varibles前面只能有3种修饰符:finalstaticpublic。默认为finalstaticpublic(如果interfacepublic的话)

3.method起面只能有两种修饰符:abstractpublic.默认为abstractpublic(如果interfacepublic的话)

4.Interfaces 没有 constructors

5.因为varibles默认为final,所以必须要赋初值。

 

23Given the following code:

 

public interface MyClass

{

     void myMethod();

}

 

The class which implements MyClass

AShould have myMethod which must necessarily be public.

BShould have myMethod which could be "friendly" or public.

CShould have myMethod which should not throw any checked exceptions.

DShould have myMethod which cannot be synchronized.

 

解释:A and C are correct answers. A is correct as by default methods in an interface are public. You can't override a method to another method which is more private then the original method, i.e. a public method can be overridden to only public. B is incorrect for the same reason. Also the overridden method can't throw any Exception if its not thrown in the original method. D is incorrect as you can implement a non synchronized method to synchronized method.

*******************************************************************************

Static 方法总结:

一个final类不能定义为abstract的,因为abstract的类需要被其他类继承的,晕了吧。

再记一条:static 方法不能被非static 方法覆盖(或重写),static方法也不能覆盖非static的方法

 

34What will be printed out if this code is run with the following command line?

 

java AnyProg Well Done

 

public class AnyProg

{

       public static void main(String argv[])

       {    

              System.out.println(argv[2]);

       }

}

答案:Exception raised: "java.lang.ArrayIndexOutOfBoundsException: 2"

数组的大小是定义时的数减一,一共才两个值你让它显示第三个,找不到呀它就去现异常了

 

 

351. String s = "abcd";

2. Integer x = new Integer(3);

3. String s2 = s + 4;

4. s2 = null;

5. s = null;

问在s2在第几行符合垃圾收集的条件??

答:在第4行时,没有任何对象引用s2

*********************************************************

关于内部类的几点:

(1)    Variables defined inside inner classes cannot be static unless the inner class itself is static

如果你非要在非静态内部类里定义的话,编译提示:内部类不能有静态声明

(2)    Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables.

(3)    An inner class can actually be a subclass of the outer class

(4)    Inner classes can be declared as private. Top level, outer classes cannot.

(5)    如果在一个方法中定义了一个内部类,想让内部类能够存取包装它的方法中的变量的话,就必须把变量设为最终类型(final),否则将会出现错误:从内部类中访问局部变量;需要被声明为最终类型

 

**************************************

Static的方法不能被非Static 的方法所覆盖

 

关于集合框架的东东:

(1)       HashtableHashMap都继承于Map,他们能够存取所有Object类型的对象数据

Map中的方法:

public Object put(Object key, Object value)

public Object get(Object key)

HashMapHashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key,由于非线程安全,效率上可能高于Hashtable

HashMap允许将null作为一个entrykey或者value,而Hashtable不允许。

HashMapHashtablecontains方法去掉了,改成containsvaluecontainsKey。因为contains方法容易让人引起误解。

Hashtable继承自Dictionary类,而HashMapJava1.2引进的Map interface的一个实现。

最大的不同是,Hashtable的方法是Synchronize的,而HashMap不是,在多个线程访问Hashtable时,不需要自己为它的方法实现同步,而HashMap 就必须为之提供外同步。

HashtableHashMap采用的hash/rehash算法都大概一样,所以性能不会有很大的差异。

 

关于线程:

(1)    如果一个Thread类没有写run()的话,程序编译通过OK,调用start()方法的时候没有任何动作,正常运行。

 

关于同步的解释:Ensures only one thread at a time may access a method or object.

 

Test3

1Vector v = new Vector(100);

v.addElement("99");

2、在定义float型数据时,如果是一个没有带小数点的可以不在数后加f ,但如果有小数点则要加上f ,否则编译不通过,提示可能损失精度

3hashCode()原形:public int hashCode()

4、再说一次,接口不能有构造方法

5、不能在定义前使用一个变量

6、如果是一个类成员变量(static int I = 10),无论修改多少次只保存最后一次修改的值

7"transient" is a keyword used for preventing variables in an object from being serialized. It cannot be applied to methods.  "volatile" is used to inform the compiler that a variable may be changed asynchronously with regard to its current definition (e.g., from a native method outside the JVM).

8、内部类可以存取包装它的类中的所有成员,无论是什么修饰符修饰的。

9、关于Assertion
       assert statements in a program may not always be executed

       Assertions can be enabled or disabled programmatically

10、两数相除,如果除数是int 型的0则会抛出AirthmeticException异常,但除数是浮点型的数据时不会抛出异常,会输出Infinity

11、Byte只有两个构造方法:public Byte(byte value)            
public Byte(String s)
     throws NumberFormatException

12、八进制、十进制、十六进制混合相加,把各个进制化成对应的十进制进行相加

13、关于try..catch…finally的几点:
      
1A try block can be followed either by a catch block or a finally block, or both

       (2) A catch block must always be associated with a try block

       (3) A finally block can never stand on its own (that is, without being associated with try block)

14What will be printed when you execute the following code?

class C

{

       C()

       {

              System.out.print("C");

       }

}

 

class A

{

        C c = new C();

       A()

       {

              this("A");

              System.out.print("A");

       }

        A(String s)

       {

             System.out.print(s);

       }

}

 

class B extends A

{

        B()

       {

              super("B");

              System.out.print("B");

       }

 

        public static void main(String[] args)

       {

                new  B();

        }

}

你可能会认为他输出CAB,但结果是CBB

如果在子类中存在super()语句的话,就会调用父类的有参构造函数

 

21What will happen when you compile and run the following code?

 

public class Test

{

       public void myMethod(Object o)

       {

              System.out.println("My Object");

       }

 

       public void myMethod(String s)

       {

              System.out.println("My String");

       }

 

       public static void main(String args[])

       {

              Test t = new Test();

              t.myMethod(null);

       }

 

}

AThe code does not compile.

BThe code compiles cleanly and shows "My Object".

CThe code compiles cleanly and shows "My String"

DThe code throws an Exception at Runtime.

答案:

C is correct as when null is passed as an argument to the method myMethod(String s) will be called. Had myMethod(String s) not present , myMethod(Object o) would be called.

 

24What will happen when you compile and run the following code?

 

public class MyClass

{

       static int x;

      

       public static void main(String args[])

       {

              x = 5;

              MyClass m1 = new MyClass();

              MyClass m2 = new MyClass();

              MyClass m3 = new MyClass();

 

              m1.x = 10;

              m2.x = 20;

              m3.x = 30;

 

              System.out.println(m1.x);

              System.out.println(m2.x);

              System.out.println(m3.x);

 

       }

}

输出结果是:

30

30

30

因为x 是类成员变量,所以对象m1,m2,m3都指导向一个x

 

 

Test4

Test5

1、  Thread类中sleep()方法的睡眠时间是用毫秒表示的,不是用的秒

2、  Math.random()的返回类型是double

3、  一个带有私有构造方法的类是不能被其他类继承的,因为在子类初始化的时候会调用父类的构造方法,而父类的构造方法是私有的,将出现错误。

4、  不能强行进行垃圾回收,但可以用System.gc()或者Runtime.getRuntime().gc()来建议垃圾回收

5、  八进制数据是从0~7,如果在八进制数中出现7以上的数则出现错误

6、  在方法头部用throws 异常名,然后再在方法体内抛出异常没有错误

7、  StringBuffer没有覆盖equals方法

 

 

 

 

 

Test6

1、  如果在方法内定义一个变量不赋予它初值,则编译会出错;但如果在类中定义变量则编译通过。

2、  获得数组的大小是用length,而获得字符串的长度用length(),已经错了不是一次了

3、  VectorHashtable是安全的,HashMap没有Hashtable安全

4、  构造方法不通常都是public

5、  0.0 == -0.0编译OK       0 == -0编译OK

6、  在求余和除法运算时,如果除数为0编译没有错误,在运行时会抛出ArithmeticException

7、  Local variables cannot be declared as static.

8、  有父类Parent和子类ChildParent p = new Child()p属于父类(Parent)

9、  移位运算:int i = 1;       i << 32和没有移位结果一样,(32 % 32 = 0)
i << 33
i << 1结果一样,因为33int型的模(32)等于1

 

Test7

1、  当定义一个Thread的子类时,MyThread  mt = new MyThread();类中有run(),但在调用的时候却调用了mt.run();没有调用start()启动线程,会调用MyThread类中的run()方法

2、  本地方法没有方法体,可以定义static

3、  类中如果有静态块(static codes),当类被载入到JVM时自动执行。

4、  在一个Thread的子类中同时存在start()run()start()被重写

5、  可以把一个子类强制类型转换后赋给父类,但不能把一个父类赋给子类

6、   

 

 

Test8

1、  定义一个二维数组,如果第二维没有初始化,在用第二维数组的时候会抛出异常

2、  要覆盖Object下的public boolean equals(Object obj){}方法,必须再覆盖public int hashCode()才能够完全覆盖equals()方法的

 

Flow Control And Assertion

1、What results from the following code fragment?

1.  int i = 5;

2.  int j = 10;

3.  System.out.println(i + ~j);

结果是多少呢,你可能要算了会,一个简单的方法

~j = (-j)-1

也就是说,输出5 + (-11) = -6

2、What will be the result when you attempt to compile and run the following code?

 

public class Conv

{

    public static void main(String argv[])

    {

        Conv c = new Conv();

        String s = new String("ello");

        c.amethod(s);

    }

   

    public void amethod(String s)

    {

        char c = 'H';

        c += s;

        System.out.println(c);

    }

}

此程序在编译时会有错误,因为c是char型的,而s是String型的,char型的长度小于String的,不行把String强制类型转换到char

3、What will happen when you attempt to compile and run the following code?

(Assume that the code is compiled and run with assertions enabled.)

     

    public class AssertTest

    {

        public void methodA(int i)

        {

            assert i >= 0 : methodB();

            System.out.println(i);

        }

   

        public void methodB()

        {

            System.out.println("The value must not be negative");

        }

 

        public static void main(String args[])

        {

            AssertTest test = new AssertTest();

            test.methodA(-10);

        }  

    }

此程序会编译错误

assert Expression1;

assert Expression1 : Expression2;

而程序中assert i >= 0 : methodB();中的methodB()是一个void返回类型,Expresstion2必须是一个表达式才可以,所以会导致编译错误

 

4、在除法、求余运算中,如果除数为0则会抛出AirthmticException

5、0.0 == -0.0 编译OK,但5 == -5  NOT OK

6、int i = (int)(Math.random() * 100);  i的值只能是0~99,不可能到100

 

7、What results from compiling and running the following code fragment?

int a = 3;

int b = 5;

System.out.println(a + ' ' + b);//可以这样写System.out.println(3 + 32 + 5);

执行结果为40                ' '对应的ASCII码为32

***在这样的运算中,把低位类型的数据转换为在此表达式中的高位类型