2.1. Given:
1. enum Animals{
2. DOG(“woof”),CAT(“meow”),FISH(“burble”);
3. String sound;
4. Animal(String s){ sound = s;}
5. }
6. class TestEnum{
7. static Animal a;
8. public static void main(String[] args){
9. System.out.println(a.DOG.sound +” ” +a.FISH.sound);
10. }
11. }
What is the result?
A. woof burble
B. Multiple compilation errors
C. Compilation fails due to an error on line 2
D. Compilation fails due to an error on line 3
E. Compilation fails due to an error on line 4
F. Compilation fails due to an error on line 9
提示:枚举可以有构造方法和变量
3.1. Given the following code:(构造方法)
what is the result? Select all right answer:
A. Hello
B. Hello World
C. Compilation fails
D. Hello World 5
E. The code runs with no output
F. An exception is thrown at runtime
C考查构造器之间的调用,通过this关键字实现构造器的调用. 在同一个类的不同构造器中调用该类的其他构造器需要使用this(…)的形式,而且必须是在构造器的第一行调用.在本示例中,类Hello中存在两个构造器,一个带参数,一个无参数,再带参数的构造器中试图通过Hello()调用无参构造器导致编译错误.
如果将Hello()改为this()语句如果不放在第一行也会导致编译错误
When //doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime