***** =====================
2.8 What results by running the following code fragment?
int a = 8;
int b = 3;
float f = a++/b--;
System.out.println(f);
A. 3.0
B. 4.5
C. 2.0
D. 2.6
Answer: C
Note: a++/b-- is int, and then implicitly cast to float.
*** ================
2.9 We have the following organization of classes.
class Parent { }
class DerivedOne extends Parent { }
class DerivedTwo extends Parent { }
Which of the following statements is correct for the following expression?
Parent p = new Parent();
DerivedOne d1 = new DerivedOne();
DerivedTwo d2 = new DerivedTwo();
d1 = (DerivedOne)p;
A. Illegal both compile and runtime
B. Legal at compile time, but fails at runtime
C. Legal at compile and runtime
D. None of the above
Answer: B
Note: Since the object contained in p is not actually a DeriveOne object, the assignment will cause Java to throw a ClassCastException.
**** ===================
2.10 Which of the following will compile and run without an error?
A. Integer i = new Integer('A');
B. Integer i = new Integer("7");
C. Character c = new Character("A");
D. Boolean b = new Boolean(null);
E. Integer i = new Integer("0x10");
Answer: ABD
Note: Option E will compile correctly but will throw a NumberFormatException at runtime, as the string argument does not represent a string in decimal format. E???
############
test 3
############
***** =========================
3.1 What 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();
}
}
A. BB
B. CBB
C. BAB
D. None of these
Answer: B
*** ========================
3.2 Which of the following method(s) must be implemented correctly if the class has to behave properly and consistently with all Java collection classes and interfaces?
A. public int hashCode()
B. protected Object clone()
C. public String toString()
D. public boolean equals(Object obj)
E. public long hashCode()
F. Only option E is correct
Answer: AD
*** ===========================
3.3 If an instance of a class needs to be added to a TreeSet, which interface this class must implement?
(Assume that the TreeSet is instantiated with the default no-argument constructor)
A. java.util.SortedSet
B. java.lang.Cloneable
C. java.lang.Comparable
D. java.util.RandomAccess
E. java.io.Serializable
Answer: C
*** ============================
3.4 The ThreadGroup class instance
A. Allows threads to be manipulated as group.
B. Provides support for ThreadDeath listeners.
C. May contain other ThreadGroups.
D. Must contain threads of the same type.
Answer: AC
***** ==========================
3.4 What 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);
}
}
A. The code does not compile.
B. The code compiles cleanly and shows "My Object".
C. The code compiles cleanly and shows "My String"//更精确地匹配
D. The code throws an Exception at Runtime.
Answer: C
***** ==========================
3.5 Which of the following results in a negative value of a?
A. int a = -1; a = a >>> 8;
B. byte a = -1; a = (byte)(a >>> 5);//char only has 16 bytes, that is 65535
C. int a = -1; a = a >> 5;
D. int a = -1; a = a >>> 32;
Answer: BCD
**** ==========================
3.6 Which one statement is true about class Animal above and class Tiger below?
1. package abc;
2.
3. public class Animal
4. {
5. protected static int refCount = 0;
6. public Animal() {refCount++;}
7. protected void runFast() { } // Run as fast as you can
8. static int getRefCount() { return refCount; }
9. }
1. package def;
2.
3. class Tiger extends abc.Animal
4. {
5. Tiger() { refCount++; }
6.
7. public static void main(String args[])
8. {
9. System.out.println("Before: " + refCount);
10. Tiger ashtonish = new Tiger();
11. System.out.println("After: " + refCount);
12. ashtonish.runFast();
13. }
14. }
A. Compilation of Tiger fails at line 5 because static members cannot be overridden.
B. Compilation of Tiger fails at line 12 because method runFast() is protected in the superclass.
C. The program will compile and execute. The output will be
Before: 0
After: 1
D. Compilation will succeed, but an exception will be thrown at line 12, because method runFast() is protected in the superclass.
E. The program will compile and execute. The output will be
Before: 0
After: 2
Answer: E