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

9 篇文章 1 订阅

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam

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


QUESTION 261
Given:
3. class Employee {
4. String name; double baseSalary;
5. Employee(String name, double baseSalary) {
6. this.name = name;
7. this.baseSalary = baseSalary;
8. }
9. }
10. public class SalesPerson extends Employee {
11. double commission;
12. public SalesPerson(String name, double baseSalary, double commission) {
13. // insert code here
14. }
15. }
Which two code fragments, inserted independently at line 13, will compile? (Choose two.)
A. super(name, baseSalary);
B. this.commission = commission;
C. super();
this.commission = commission;
D. this.commission = commission;
super();
E. super(name, baseSalary);
this.commission = commission;
F. this.commission = commission;
super(name, baseSalary);
G. super(name, baseSalary, commission);
Answer: AE
调用父类的构造器必须在第一行用super(name, baseSalary),并且不能用super(),因为父类没有无参数的构造函数~~


QUESTION 262
Given:
11. class A {
12. public void process() { System.out.print("A,"); }
13. class B extends A {
14. public void process() throws IOException {
15. super.process();
16. System.out.print("B,");
17. throw new IOException();
18. }
19. public static void main(String[] args) {
20. try { new B().process(); }
21. catch (IOException e) { System.out.println("Exception"); }
22. }
What is the result?
A. Exception
B. A,B,Exception
C. Compilation fails because of an error in line 20.
D. Compilation fails because of an error in line 14.
E. A NullPointerException is thrown at runtime.
Answer: D
子类抛出了父类没有的已检查异常~~


QUESTION 263
Given a method that must ensure that its parameter is not null:
11. public void someMethod(Object value) {
12. // check for null value ...
20. System.out.println(value.getClass());
21. }
What, inserted at line 12, is the appropriate way to handle a null value?
A. assert value == null;
B. assert value != null, "value is null";
C. if (value == null) {
throw new AssertionException("value is null");
}
D. if (value == null) {
throw new IllegalArgumentException("value is null"); }
Answer: D
应该抛出参数非法异常才对~~


QUESTION 264
Given:
11. public static void main(String[] args) {
12. try {
13. args = null;
14. args[0] = "test";
15. System.out.println(args[0]);
16. } catch (Exception ex) {
17. System.out.println("Exception");
18. } catch (NullPointerException npe) {
19. System.out.println("NullPointerException");
20. }
21. }
What is the result?
A. test
B. Exception
C. Compilation fails.
D. NullPointerException
Answer: C
NullPointerException 的catch块会有不可达到(cannot reach)的编译错误~~


QUESTION 265
Given:
11. public static Iterator reverse(List list) {
12. Collections.reverse(list);
13. return list.iterator();
14. }
15. public static void main(String[] args) {
16. List list = new ArrayList();
17. list.add("1"); list.add("2"); list.add("3");
18. for (Object obj: reverse(list))
19. System.out.print(obj + ", ");
20. }
What is the result?
A. 3, 2, 1,
B. 1, 2, 3,
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.
Answer: C
for-reach循环的第二个参数必须是obj[]类型的


QUESTION 267
Given:
11. class X { public void foo() { System.out.print("X "); } }
12.
13. public class SubB extends X {
14. public void foo() throws RuntimeException {
15. super.foo();
16. if (true) throw new RuntimeException();
17. System.out.print("B ");
18. }
19. public static void main(String[] args) {
20. new SubB().foo();
21. }
22. }
What is the result?
A. X, followed by an Exception.
B. No output, and an Exception is thrown.
C. Compilation fails due to an error on line 14.
D. Compilation fails due to an error on line 16.
E. Compilation fails due to an error on line 17.
F. X, followed by an Exception, followed by B.
Answer: A
子类虽然抛出了RuntimeException异常,但是父类可以不必也处理RuntimeException,因为任何类都有处理RuntimeException的能力~~


:
QUESTION 274
Given:
11. public class Person {
12. private String name, comment;
13. private int age;
14. public Person(String n, int a, String c) {
15. name = n; age = a; comment = c;
16. }
17. public boolean equals(Object o) {
18. if (! (o instanceof Person)) return false;
19, Person p = (Person)o;
20. return age == p.age && name.equals(p.name);
21. }
22. }
What is the appropriate definition of the hashCode method in class Person?
A. return super.hashCode();
B. return name.hashCode() + age * 7;
C. return name.hashCode() + comment.hashCode() / 2;
D. return name.hashCode() + comment.hashCode() / 2 - age * 3;
Answer: B
看equals,仅和age,name有关~~要遵从hashCode()返回值不同则两个元素一定不同,即equals的返回值一定是false~~



QUESTION 275
A programmer must create a generic class MinMax and the type parameter of MinMax must implement
Comparable. Which implementation of MinMax will compile?

A. 

class MinMax<E extends Comparable<E>> {
E min = null;
E max = null;
public MinMax() {}
public void put(E value) { /* store min or max */ }


B. 

class MinMax<E implements Comparable<E>> {
E min = null;
E max = null;
public MinMax() {}
public void put(E value) { /* store min or max */ }


C. 

class MinMax<E extends Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() {}
public <E> void put(E value) { /* store min or max */ }


D.

 class MinMax<E implements Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() {}
public <E> void put(E value) { /* store min or max */ }
Answer: A
简单的泛型~~


QUESTION 276
Given:
3. import java.util.*;
4. public class G1 {
5. public void takeList(List<? extends String> list) {
6. // insert code here
7. }
8. }
Which three code fragments, inserted independently at line 6, will compile? (Choose three.)
A. list.add("foo");
B. Object o = list;
C. String s = list.get(0);
D. list = new ArrayList<String>();
E. list = new ArrayList<Object>();
Answer: BCD
list的类型是List<? extends String>,根据PECS法则,就是说list必须是producer生产者~~list是实现了List接口的String类或其子类~~


QUESTION 277
Given:
1. public class Drink implements Comparable {
2. public String name;
3. public int compareTo(Object o) {
4. return 0;
5. }
6. }
and:
20. Drink one = new Drink();
21. Drink two = new Drink();
22. one.name= "Coffee";
23. two.name= "Tea";
24. TreeSet set = new TreeSet();
25. set.add(one);
26. set.add(two);
A programmer iterates over the TreeSet and prints the name of each Drink object. What is the result?
A. Tea
B. Coffee
C. Coffee
Tea
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: B
Drink对象相互比较的话都会是相等的,因为 public int compareTo(Object o) { return 0; },而TreeSet类的add()方法通过compareTo()方法比较各个对象的大小,然后把对象按大小添加到红黑树这种数据结构上。如果比较结果相等,就拒绝添加,add方法返回false~~


:
QUESTION 279
Given:
1. public class LineUp {
2. public static void main(String[] args) {
3. double d = 12.345;
4. // insert code here
5. }
6. }
Which code fragment, inserted at line 4, produces the output | 12.345|?
A. System.out.printf("|%7d| \n", d);
B. System.out.printf("|%7f| \n", d);
C. System.out.printf("|%3.7d| \n", d);
D. System.out.printf("|%3.7f| \n", d);
E. System.out.printf("|%7.3d| \n", d);
F. System.out.printf("|%7.3f| \n", d);
Answer: F
格式输出~~和c语言一样一样的~~


QUESTION 280
Given that the current directory is empty, and that the user has read and write privileges to the current
directory, and the following:
1. import java.io.*;
2. public class Maker {
3. public static void main(String[] args) {
4. File dir = new File("dir");
5. File f = new File(dir, "f");
6. }
7. }
Which statement is true?
A. Compilation fails.
B. Nothing is added to the file system.
C. Only a new file is created on the file system.
D. Only a new directory is created on the file system.
E. Both a new file and a new directory are created on the file system.
Answer: B



QUESTION 281
Given:
1. d is a valid, non-null Date object
2. df is a valid, non-null DateFormat object set to the current locale What outputs the current locale's
country name and the appropriate version of d's date?
A. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));
B. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.format(d));

C. Locale loc = Locale.getLocale();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
D. Locale loc = Locale.getDefault();
System.out.println(loc.getDisplayCountry()
+ " " + df.setDateFormat(d));
Answer: B


  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
OCA / OCP Practice Tests: Exam 1Z0-808 and Exam 1Z0-809 by Scott Selikoff English | 16 Mar. 2017 | ASIN: B06XQR7DVN | 600 Pages | AZW3 | 1.8 MB OCA/OCP Java SE 8 Programmer Practice Tests complements the Sybex OCA: Oracle Certified Associate Java SE 8 Programmer I Certification Study Guide and the OCP: Oracle Certified Professional Java SE 8 Programmer II Study Guide for exams 1Z0-808 and 1ZO-809 by providing last minute review of 100% of exam objectives. Get the advantage of over 1,000 expert crafted questions that not only provide the answer, but also give detailed explanations. You will have access to unique practice questions that cover all 21 objective domains in the OCA/OCP exams in the format you desire–test questions can also be accessed via the Sybex interactive learning environment. Two additional practice exams will ensure that you are ready for exam day. Whether you have studied with Sybex study guides for your OCA/OCP or have used another brand, this is your chance to test your skills. Access to all practice questions online with the Sybex interactive learning environment Over 1,000 unique practice questions and 2 practice exams include expert explanations Covers 100% of all 21 OCA/OCP objective domains for Exams 1Z0-809 and 1Z0-809 Studying the objectives are one thing, but diving deeper and uncovering areas where further attention is needed can increase your chance of exam day success. Full coverage of all domains shows you what to expect on exam day, and accompanying explanations help you pinpoint which objectives deserve another look.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值