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

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

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

时间: 2024-11-06 03:56:07

OCJP(1Z0-851) 模拟题分析(九)的相关文章

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 235Given:5. class Payload {6. private int weight;7. public Payload (int w) { weight = w; }8. public void setWeight(int w) {

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(this); }14. }15.16. class Boochy {1

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 201 Given:1. public class TestString3 {2. public static void main(String[] args) {3. // insert code here5. System.out.print

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 61Given:1. public class TestString1 {2. public static void main(String[] args) { 3. String str = "420";4. str += 42;5.

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 167Given:1. import java.util.*;2. public class WrappedString {3. private String s;4. public WrappedString(String s) { this.

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

Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有疏漏,还请大家对我的分析提出质疑. QUESTION 31 Given:1. interface A { public void aMethod(); }2. interface B { public void bMethod(); }3. interface C extends A,B { pub

一道模拟题

问题:把英文单词表示的数字转换为阿拉伯数字,要求数字不超过整形范围,数字形如abc,def,hrg. 第一行表示有几组数据,第二行输入英文. 输出:相应的阿拉伯数字. 例如:input: 3 eleven one hundred and two output: 11 102 分析:要注意百万和千要断位,还有要从高位往低位查找,注意分情况讨论. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 st

Gym 100625C 密文匹配-模拟题-(map)

题意:已知n个明文和一个密文,推出可能的匹配关系,然后输出字符串ss的密文. 分析: 一个模拟题,当时想偏了,还想着要同一字母可能在任意位置,然后要记录每个字母的位置,找密文的相应位置必须是同一字母,balabala的,不知道什么鬼. 其实就是简单的对应关系,不用管位置啥的,只管同一字母对应的密文是一样的就行了.26个字母,枚举一遍就是了.对应关系匹配啥的用map是最好不过的了.小tirck是,如果已知25个字母,那么剩下的一个也就知道了. 代码: #include<iostream> #in

hdu_1034(模拟题)

很久没有打模拟题了,再次总结一下模拟题的做法: 仔细分析题意,弄清楚过程 理清楚模拟步骤,严格按照步骤编写代码 添加中间输出测试每步结果 虽然这是一个很简单的水题,但是没有松哥帮忙还是卡了很久,因为没有做好第一步,题目中是老师先吹哨,然后一圈所有同学同事将自己现有糖的一般分给下一个同学,结束后老师将现手里为奇数的糖的同学再多给一颗糖最后判断每个人的糖是否一样,如果一样结束游戏 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include