scjp考证试题

mzzandlss 2012-04-20 03:56:42
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
...全文
220 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我叫阿狸猫 2012-04-22
  • 打赏
  • 举报
回复
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选项 因为函数和父类中的一摸一样 所以是覆写。
B选项的权限是private 而继承后覆写的函数必须大于等于被覆写函数的权限
C选项的函数不是覆写,而是重载 因为参数列表和父类里的不一样
D选项 两个函数函数名相同而返回值不相同,虚拟机在调用函数的时候不知道返回的是int类型的还是short类型的
E选项 因为是静态的 静态函数必须覆写静态函数。
不知道这样对不对...
nmyangym 2012-04-21
  • 打赏
  • 举报
回复
第6题加点代码测试,编译通不过。报下面错误:

已捕捉到异常 java.lang.ArithmeticException
} catch (ArithmeticException ae) {
^
1 错误
流星陨落 2012-04-21
  • 打赏
  • 举报
回复
不懂,等待高手...
  • 打赏
  • 举报
回复
SCJP 现在改叫 OCPJP (Oracle Certified Professional Java Programmer) 了
a372210774 2012-04-21
  • 打赏
  • 举报
回复
第2题 在继承一个类的方法时 方法的访问权限 只能变高不能变低 答案C其实第2个参数与继承的方法的参数不用 所以是方法的重载 即继承A类的子类有两个名字相同的方法 只是参数不同而已
sffx123 2012-04-20
  • 打赏
  • 举报
回复
建议楼主一次别搞这么多题,不方便大家解答.
mzzandlss 2012-04-20
  • 打赏
  • 举报
回复
以上试题求为什么?求大牛解答

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧