scjp考证试题
1.Which statement is true?
A. Memory is reclaimed by calling Runtime.gc().
B. Objects are not collected if they are accessible from live threads.
C. Objects that have finalize() methods are never garbage collected.
D. Objects that have finalize() methods always have their finalize() methods called before the program ends.
E. An OutOfMemory error is only thrown if a single block of memory cannot be found that is large enough for a particular requirement.
Answer: B
答案为什么是B,其他为什么不正确?
2. class A {
protected int method1(int a, int b) { return 0; }
}
Which two are valid in a class that extends class A? (Choose two)
A. public int method1(int a, int b) { return 0; }
B. private int method1(int a, int b) { return 0; }
C. private int method1(int a, long b) { return 0; }
D. public short method1(int a, int b) { return 0: }
E. static protected int method1(int a, int b) { return 0; }
Answer: A, C
答案为什么是A,C其他为什么不正确?
3.public class Test {
public static void main(String[] args) {
int x = 0;
assert (x > 0): "assertion failed";
System.out.println("finished");
}
}
What is the result? 结果是什么
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is output.
Answer: A
答案为什么是A,其他为什么不正确?
4. class A {
}
class Alpha {
private A myA = new A();
void dolt( A a ) {
a = null;
}
void tryIt() {
dolt( myA );
}
}
Which two statements are correct? (Choose two)
A. There are no instanced of A that will become eligible for garbage collection.
B. Explicitly setting myA to null marks that instance to be eligible for garbage collection.
C. Any call on tryIt() causes the private instance of A to be marked for garbage collection.
D. Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.
Answer: B, D
求解释
5. public float getSalary(Employee e) {
21. assert validEmployee(e);
22. float sal = lookupSalary(e);
23. assert (sal>0);
24. return sal;
25. }
26. private int getAge(Employee e) {
27. assert validEmployee(e);
28. int age = lookupAge(e);
29. assert (age>0);
30. return age;
31. }
Which line is a violation of appropriate use of the assertion mechanism?
A. line 21
B. line 23
C. line 27
D. line 29
Answer: A
6. try {
12. int x = 0;
13. int y = 5 / x;
14. } catch (Exception e) {
15. System.out.println("Exception");
16. } catch (ArithmeticException ae) {
17. System.out.println("Arithmetic Exception");
18. }
19. System.out.println("finished");
What is the result?
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
Answer: D
为什么不是C而是D
7. for( int i = min; i <max; i++) {
12. System.out.println(i);
13. }
If min and max are arbitrary integers, what gives the same result?
A. init i = min;
while( i < max ) {
}
B. int i = min;
do
System.out.println(i++);
} while( i< max );
C. for (int i=min; i<max; System.out.println(++I));
D. for (int i=; i++<max; System.out.println(i));
Answer: B
8. that b and c refer to instances of wrapper classes, which two statements are true? (Choose two)
A. b.equals(b) returns true.
B. b.equals(c) returns the same result as b == c.
C. b.eqials(c) can return false even if c.equals(b) returns true.
D. b.equals(c) throws an exception if b and c are different wrapper types.
E. b.equals(c) returns false if the type of wrapper objects being compared are
different.
Answer: B, C
9. Which two are benefits of fully encapsulating a class? (Choose two)
A. Performance of class methods is improved.
B. Implementation details of the class are hidden.
C. Access modifiers can be omitted on class data members.
D. Code that uses the encapsulation class can access data members directly.
E. Internal operation of the class can be modified without impacting clients of that class.
Answer: B, E
10. package foo;
2. public class Outer {
3. public static class Inner {
4. }
5. }
Which statement is true?
A. Compilation fails.
B. An instance of the Inner class can be constructed with "new Outer.Inner()".
C. An instance of the Inner class cannot be constructed outside of package foo.
D. An instance of the Inner class can be constructed only from within the Outer class.
E. From within the package foo, and instance of the Inner class can be constructed with "new Inner()".
Answer: B
11.What happens when thread X executes a wait() method on object A, without owning object A's lock?
A. Compilation fails.
B. An exception is thrown.
C. The wait() method has no effect.
D. Thread X receives the lock immediately.
E. Object A moves the thread to the wait pool.
Answer: B
12. public static void main(String[] args) {
12. Object obj = new Object() {
13. public int hashCode() {
14. return 42;
15. }
15. };
17. System.out.println(obj.hashCode());
18. }
What is the result?
A. 42
B. An exception is thrown at runtime.
C. Compilation fails because of an error on line 12.
D. Compilation fails because of an error on line 16.
E. Compilation fails because of an error on line 17.
Answer: A
13.Which two statements are true regarding the return values of property written hashCode and equals methods from two instances of the same class? (Choose two)
A. If the hashCode values are different, the objects might be equal.
B. If the hashCode values are the same, the object must be equal.
C. If the hashCode values are the same, the objects might be equal.
D. If the hashCode values are different, the objects must be unequal.
Answer: C, D
14. public class test (
2. public static void main (String args[]) {
3. int i = 0xFFFFFFF1;
4. int j = ~i;
5.
6. }
7. )
What is the decimal value of j at line 5?
A. 0
B. 1
C. 14
D. -15
E. An error at line 3 causes compilation to fail.
F. An error at line 4 causes compilation to fail.
Answer: C
15.Integer i = new Integer (42);
Long 1 = new Long (42);
Double d = new Double (42.0);
Which two expressions evaluate to True? (Choose Two)
A. (i ==1)
B. (i == d)
C. (d == 1)
D. (i.equals (d))
E. (d.equals (i))
F. (i.equals (42))
Answer: D, E