OCJP(1Z0-851) 模拟题分析(六)

本文分析了Java Standard Edition 6 Programmer Certified Professional Exam(1Z0-851)的模拟试题,重点关注了try-catch语句块和异常处理。内容提到在编译时,会根据父类方法检查异常处理,而在运行时调用子类方法。若子类方法抛出父类未声明的异常,会导致没有适当的异常处理器,因此这是不被允许的。
摘要由CSDN通过智能技术生成

Exam : 1Z0-851

Java Standard Edition 6 Programmer Certified Professional Exam

以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑。


QUESTION 167
Given:
1. import java.util.*;
2. public class WrappedString {
3. private String s;
4. public WrappedString(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet<Object> hs = new HashSet<Object>();
7. WrappedString ws1 = new WrappedString("aardvark");
8. WrappedString ws2 = new WrappedString("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. hs.add(ws1); hs.add(ws2); hs.add(s1); hs.add(s2);
12. System.out.println(hs.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: D
WrappedString 的hashCode()是继承自Object类,而String的hashCode()时重写过的仅与字符串的内容有关。ws1 和ws2存储地址不同,所以hashCode()的返回值不同,而s1和s2字符串的内容相同,哈希值相同。


QUESTION 168
Given a class whose instances, when found in a collection of objects, are sorted by using the compareTo()
method, which two statements are true? (Choose two.)
A. The class implements java.lang.Comparable.
B. The class implements java.util.Comparator.
C. The interface used to implement sorting allows this class to define only one sort sequence.
D. The interface used to implement sorting allows this class to define many different sort sequences.
Answer: AC
Comparable接口仅有一个compareTo()函数~~谨记


QUESTION 169
Given:
1. import java.util.*;
2. public class Example {
3. public static void main(String[] args) {
4. // insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8. }
9. }
Which code, inserted at line 4, guarantees that this program will output [1, 2]?
A. Set set = new TreeSet();
B. Set set = new HashSet();
C. Set set = new SortedSet();
D. List set = new SortedList();
E. Set set = new LinkedHashSet();
Answer: A
必须是排好序的集合,所以TreeSet合适。
而HashSet应该也可以的,Integer类型的hashCode()函数返回的是相应的int类型的值。如果就只有一个答案的话TreeSet是最好的选项。
SortedSet是一个interface,java里面没有SortedList类。


QUESTION 171
DRAG DROP
Click the Task button.

Answer:
多态性~~a和b的getName()都是A类的方法,对这个方法来说看不到B中的name变量。


QUESTION 177
Given:
1. class TestException extends Exception { }
2. class A {
3. public String sayHello(String name) throws TestException {
4. if(name == null) throw new TestException();
5. return "Hello " + name;
6. }
7. }
8. public class TestA {
9. public static void main(String[] args) {
10. new A().sayHello("Aiko");
11. }
12. }
Which statement is true?
A. Compilation succeeds.
B. Class A does not compile.
C. The method declared on line 9 cannot be modified to throw TestException.
D. TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.
Answer: D
第十行 new A().sayHello("
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值