这套题我参考了这篇文章,以及一些百度上找的内容,再加上自己的解释,总结下来的详解。

第1题:
1.Given a pre-generics implementation of a method:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
14. int i = ((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
What three changes allow the class to be used with generics and avoid an unchecked warning? (Choose
three.)
A. Remove line 14.
B. Replace line 14 with “int i = iter.next();”.
C. Replace line 13 with “for (int i : intList) {”.
D. Replace line 13 with “for (Iterator iter : intList) {”.
E. Replace the method declaration with “sum(List intList)”.
F. Replace the method declaration with “sum(List intList)”.
Answer: A,C,F

解析:
这个题目的意思是,哪三个选项可以一起使用并避免警告;
1.将代码打入会显示不安全警告:List is a raw type. References to generic type List should be parameterized .意思为:List是原始类型。List的范用类型的引用应当被参数化。就是让人搞清楚是那种类型(如,Integer,String)。
List< T >为泛型,对于List中元素不确定时,应用泛型保证其安全性。
泛型要求能包容的是对象型,而基本类型在java中不属于对象。想放int的话应该放Integer,int是基本数据类型,Integer是其包装类。所以应该选F。

泛型详解

2.Integer 会自动拆装箱,所以不需要第14行,所以选A。

Integer自动拆装箱相关内容

  1. for ( Iterator iter = list.iterator(); iter.hasNext(); ) 这个for循环中,
    Iterator iter=buses.iterator()表示获取一个迭代器中当前位置的对象,
    iter.hasNext()判断迭代器中后面是否还有东西,
    留空表示无限制,但是后面代码中并没有让指针移动,例如使用iter.next( ),
    改成for each,选C

for(Iterator iter=cars.iterator();iter.hasNext(); )

第二题:
2.A programmer has an algorithm that requires a java.util.List that provides an efficient
implementation of add(0, object), but does NOT need to support quick random access. What supports these requirements.?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
Answer: D
解析:首先,学好英语。。。

A. java.util.Queue

它不在java.util.List下,通常情况下但不一定先进先出,队列还提供额外的插入、提取和检查操作
B. java.util.ArrayList:可调整大小的数组的实现List接口
C. java.util.LinearList:没有在 jdk1.8 帮助文档里找到。。。百度后说是线性表,顺序存储,可实现随机存取

LinearLis

D. java.util.LinkedList:双链表,开头添加数据效率高,通过指针的移动来指向下一个内存地址的分配。

LinkedList

第三题
3.Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue())
19. max = added;
20. }
21.
}
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A. public class MinMax<?>
{
B. public class MinMax<? extends Number>
{
C. public class MinMax
{
D. public class MinMax
{
E. public class MinMax<? extends Object>
{
F. public class MinMax
{
Answer: D,F
*解析:*必须要有N,而且后面要实现doubleValue()这个方法,object没有这个方法,所以选D,F。



第四题
4.Given:
12. import java.util.*;
13. public class Explorer2 {
14. public static void main(String[] args) {
15. TreeSet s = new TreeSet();
16. TreeSet subs = new TreeSet();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s + " " + subs);
22. }
23.
}
What is the result?A. Compilation fails.
B. An exception is thrown at runtime.
C. [608, 610, 612, 629] [608, 610]
D. [608, 610, 612, 629] [608, 610, 629]
E. [606, 608, 610, 612, 629] [608, 610]
F. [606, 608, 610, 612, 629] [608, 610, 629]
Answer: E
*解析:*TreeSet是不可重复的且是自然排序的,所以s.add(629)只有一个629,再按顺序就是[606, 608, 610, 612, 629]。
题中的 (TreeSet)s.subSet(608, true, 611, true);相当于取s在[608,611]范围内的值,并且是自然排序的,所以结果为[608, 610]

第五题:
5.Given:

  1. public class Score implements Comparable {
  2. private int wins, losses;
  3. public Score(int w, int l) { wins = w; losses = l; }
  4. public int getWins() { return wins; }
  5. public int getLosses() { return losses; }
  6. public String toString() {
  7. return “<” + wins + “,” + losses + “>”;
  8. }
  9. // insert code here

}
Which method will complete this class?A. public int compareTo(Object o){/more code here/
}
B. public int compareTo(Score other){/more code here/
}
C. public int compare(Score s1,Score s2){/more code here/
}
D. public int compare(Object o1,Object o2){/more code here/
}
Answer: B
解析:
comparable< T >的方法是 compareTo (T o);
而 comparator< T >的方法是compare(T o1, T o2) ;
(查询 jdk 可知)

第六题
6.Given
11. public class Person {
12. private name;
13. public Person(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19.
}
Which statement is true?A. The time to find the value from HashMap with a Person key depends on the
size of the map.
B. Deleting a Person key from a HashMap will delete all map entries for all keys of type Person.
C. Inserting a second Person object into a HashSet will cause the first Person object to be
removed as a duplicate.
D. The time to determine whether a Person object is contained in a HashSet is constant and does NOT
depend on the size of the map.
Answer: A
解析:(参考了文章最开头文章)
A选项:由key来查找value的次数与map的大小有关。map越大,即bucket个数越多,entry链的长度相应的来说就越小;
B选项:删除HashMap中一个Person对象对应的键将会删除这个散列映射表中Person类的全部条目。错误,HashMap中Person对象的键值不是由Person对象决定的,而是程序员给定的键,例如staff.add(“123-345”, bob),就是把键为123-456的bob对象添加到名为staff的HashMap中,因而HashMap允许添加相同的对象。所以说,删除一个键对应的Person对象并不会删除所有的条目,他们的key都不同嘛。

C选项:向HashSet中插入另外一个Person对象将会引起第二个对象覆盖第一个对象。错误,虽然Person对象的hashCode方法返回的值都是420,这仅仅表明两个Person对象在一个entry链表中,接下来要调用equals方法,由于Person类没有equals方法,所以调用Object的equals方法返回对象的存储地址,很明显两个Person对象的存储地址是不同的。综上,HashSet中可以添加不同的Person对象,只要equals方法返回值为false就好。

D选项:判断一个HashSet中是否存在一个Person对象的次数是常数次,和map的大小无关。错误,由于Person对象的hashCode返回的值都是420,所以HashSet中的Person对象都在一个bucket中,组成了一条entry链表,查询速度与entry链表的大小息息相关。

关于Map挺多不理解的,参考他人的解析也不是特别懂,目前先写这样,等详细了解了,再回头更改。

第七题
7.Given:
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList a = new ArrayList();
9. a.add(1); a.add(5); a.add(3);
11. Collections.sort(a);
12. a.add(2);
13. Collections.reverse(a);
14. System.out.println(a);
15. }
16. }
What is the result?A. [1, 2, 3, 5]
B. [2, 1, 3, 5]
C. [2, 5, 3, 1]
D. [5, 3, 2, 1]
E. [1, 3, 5, 2]
F. Compilation fails.
G. An exception is thrown at runtime.
Answer: C
解析:
Collections.sort(a);排序,默认升序; Collections.reverse(a);翻转,所以答案为c

第八题:
8.Given
11. public interface Status {
12. /* insert code here */ int MY_VALUE = 10;
13. } Which three are valid on line
12?
(Choose three.
)A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
Answer: A,B,D
解析:
在接口中,变量的默认修饰符为public static final;方法的默认修饰符为public abstract;
native修饰符就是java调用非java代码的接口;protected访问修饰符就是访问仅限于包含类或从包含类派生的类型。

第九题:
9.Given:
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static void main(String[] a) { new Mountain(); }
17. }
What is the result?
A. Compilation fails.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom granite atom granite
Answer: F
解析:
调用子类的构造函数时,也会调用父类的构造函数。如果子类的构造函数没有显示地调用父类的构造函数,则默认调用父类的无参构造函数。

子类实例化时

第十题:
10.Click the Exhibit button. Which three statements are true? (Choose three.)

A. Compilation fails.
B. The code compiles and the output is 2.
C. If lines 16, 17 and 18 were removed, compilation would fail.
D. If lines 24, 25 and 26 were removed, compilation would fail.
E. If lines 16, 17 and 18 were removed, the code would compile and the output would be 2.
F. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1.
Answer: B,E,F
解析:
这里有两个内部类,一个是名为A的内部类A,一个是名为A的局部内部类A。以我的理解就是,把他们看作全局变量和局部变量。答案就选出来了。

第十一题:
11.Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18.
}
Which code, inserted at line 16, correctly retrieves a local instance of a Point object?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (new Line()).getPoint();
Answer: D
解析:
这里要求写入的代码是构建一个Point的构造函数。因为Point是一个内部类,所以要用Line来调用(用getPoint();方法)。Line.Point p =xxx;其中Line也要进行构造。所以答案为D。
若是要写成Point P=new Point();则需将public class Point改成public static class Point;这是因为Point这个内部类是动态的,而主程序public static class main是静态的,静态方法不能直接调用动态方法,修饰为静态才可以调用。
另外,由于现在的JDK版本较高,所以C选项也不会报错。

第十二题
12.Given:
11. class Alpha {
12. public void foo() { System.out.print("Afoo "); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print("Bfoo "); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();
20. b.foo();
21. }
22.
}
What is the result?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.
Answer: D
解析:
a,b都为Beta类型。

第十三题
13.Click the Exhibit button.
Which statement is true about the classes and interfaces in the exhibit?

A. Compilation will succeed for all classes and interfaces.
B. Compilation of class C will fail because of an error in line 2.
C. Compilation of class C will fail because of an error in line 6.
D. Compilation of class AImpl will fail because of an error in line 2.
Answer: C
解析:
继承时,重写父类,返回类型要小于等于父类;

所谓子类方法重写父类方法遵循“两同两小一大”的规则
两同:
1)方法名
2)形参列表
两小:
1)返回值类型比父类更小或相等(比较大小时首先要在同一个类型层次中)
2)异常比父类方法更小或相等
一大:
子类权限比父类大或相等

第十四题:
14.Which two code fragments correctly create and initialize a static array of int elements? (Choose two.)
A. static final int[] a = { 100,200 };
B. static final int[] a;
static { a=new int[2]; a[0]=100; a[1]=200;
}
C. static final int[] a = new int[2]{ 100,200 }
;
D. static final int[] a;
static void init() { a = new int[3]; a[0]=100; a[1]=200;
}
Answer: A,B
解析:
A选项和C选项都很好分辨对错;
B选项是静态初始化块,在编译的时候,编译器会自动收集类中的所有静态变量(类变量)和静态语句块(static{}块)中的语句合并产生的,编译器收集的顺序是根据语句在java代码中的顺序决定的。收集完成之后,会编译成java类的 static{} 方法,java虚拟机则会保证一个类的static{} 方法在多线程或者单线程环境中正确的执行,并且只执行一次。

类的加载和初始化

D选项的Init方法不会执行。

第十五题:
15.Given:
10. interface Foo { int bar(); }
11. public class Sprite {
12. public int fubar( Foo foo ) { return foo.bar(); }
13. public void testFoo() {
14. fubar(
15. // insert code here
16. );
17. }
18.
}
Which code, inserted at line 15, allows the class Sprite to compile?A. Foo { public int bar() { return 1;
}
B. new Foo { public int bar() { return 1;
}
C. new Foo() { public int bar() { return 1;
}
D. new class Foo { public int bar() { return 1; }
Answer: C
解析:
类似于第十题;

第十六题:
16.Given:

  1. class Alligator {
  2. public static void main(String[] args) {
  3. int []x[] = {{1,2}, {3,4,5}, {6,7,8,9}};
  4. int [][]y = x;
  5. System.out.println(y[2][1]);
  6. }

}
What is the result?A. 2
B. 3
C. 4
D. 6
E. 7
F. Compilation fails.
Answer: E
解析:
数组从0开始数;

第十七题:
17.Given:
22. StringBuilder sb1 = new StringBuilder(“123”);
23. String s1 = “123”;
24. // insert code here
25. System.out.println(sb1 + " " + s1)
;
Which code fragment, inserted at line 24, outputs “123abc 123abc”
?A. sb1.append(“abc”); s1.append(“abc”)
;
B. sb1.append(“abc”); s1.concat(“abc”)
;
C. sb1.concat(“abc”); s1.append(“abc”)
;
D. sb1.concat(“abc”); s1.concat(“abc”)
;
E. sb1.append(“abc”); s1 = s1.concat(“abc”)
;
F. sb1.concat(“abc”); s1 = s1.concat(“abc”)
;
G. sb1.append(“abc”); s1 = s1 + s1.concat(“abc”)
;
H. sb1.concat(“abc”); s1 = s1 + s1.concat(“abc”)
;
Answer: E

解析:
String类型可以用concat方法,但是String定义的变量不会改变,所以需要再次进行赋值,所以要写s1 = s1.concat(“abc”);而StringBuffer类型的可以直接用append方法;


第十八题:
18.Given that the current directory is empty, and that the user has read and write permissions, and the
following:
11. import java.io.*;
12. public class DOS {
13. public static void main(String[] args) {
14. File dir = new File(“dir”);
15. dir.mkdir();
16. File f1 = new File(dir, “f1.txt”);
17. try {
18. f1.createNewFile();
19. } catch (IOException e) { ; }
20. File newDir = new File(“newDir”);
21. dir.renameTo(newDir);
22. }
23.
}
Which statement is true?
A. Compilation fails.
B. The file system has a new empty directory named dir.
C. The file system has a new empty directory named newDir.
D. The file system has a directory named dir, containing a file f1.txt.
E. The file system has a directory named newDir, containing a file f1.txt.
Answer: E

解析:




第十九题:
19.Given:
11. class Converter {
12. public static void main(String[] args) {
13. Integer i = args[0];
14. int j = 12;
15. System.out.println(“It is " + (ji) + " that ji.”);
16. }
17. }
What is the result when the programmer attempts to compile the code and run it with the
command java Converter 12?
A. It is true that ji.
B. It is false that ji.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 13.
Answer: D

解析:
args是String类型的,所以第13行会报错;
更改后,由于args里没有赋值,会出现越界报错


第二十题
20.Given:
11. String test = “Test A. Test B. Test C.”;
12. // insert code here
13. String[] result = test.split(regex);
Which regular expression, inserted at line 12, correctly splits test into “Test A”, “Test B”, and “Test
C”?
A. String regex = “”;
B. String regex = " ";
C. String regex = “.";
D. String regex = “\s”;
E. String regex = "\.\s
”;
F. String regex = “\w[ .] +”;
Answer: E

解析:
“.”和“”都需要转义字符;
“”有一个和零个,所以要用s*;

不用s*:

结果:

不用转义字符:

结果:

第二十一题
21.Given:
5. import java.util.Date;
6. import java.text.DateFormat;
21. DateFormat df;
22. Date date = new Date();
23. // insert code here
24. String s = df.format(date);
Which code fragment, inserted at line 23, allows the code to compile?
A. df = new DateFormat();
B. df = Date.getFormat();
C. df = date.getFormat();
D. df = DateFormat.getFormat();
E. df = DateFormat.getInstance();
Answer: E

解析:
DateFormat的作用是格式化Date;帮助我们将Date转换成我们需要的String字符串。这里是对DateFormat初始化。没有D这个方法。

java中Dateformat类的详细使用

第二十二题
22.Given a class Repetition:

  1. package utils;
  2. public class Repetition {
  3. public static String twice(String s) { return s + s; }
  4. } and given another class Demo: 1. // insert code here
  5. public class Demo {
  6. public static void main(String[] args) {
  7. System.out.println(twice(“pizza”));
  8. }
  9. }
    Which code should be inserted at line 1 of Demo.java to compile and run Demo to print
    “pizzapizza”?
    A. import utils.;
    B. static import utils.
    ;
    C. import utils.Repetition.;
    D. static import utils.Repetition.
    ;
    E. import utils.Repetition.twice();
    F. import static utils.Repetition.twice;
    G. static import utils.Repetition.twice;
    Answer: F

解析:

静态导入:
import static utils.Repetition.twice;//导入Repetition类中的twice静态方法。
import static utils.Repetition.*;//导入Repetition类中的所有静态方法和静态域。

第二十三题:
23.A UNIX user named Bob wants to replace his chess program with a new one, but he is not sure where
the old one is installed. Bob is currently able to run a Java chess program starting from his home directory
/home/bob using the command: java -classpath /test:/home/bob/downloads/.jar
games.Chess Bob’s CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/
.jar What is a possible location for the
Chess.class file?
A. /test/Chess.class
B. /home/bob/Chess.class
C. /test/games/Chess.class
D. /usr/lib/games/Chess.class
E. /home/bob/games/Chess.class
F. inside jarfile /opt/java/lib/Games.jar (with a correct manifest)
G. inside jarfile /home/bob/downloads/Games.jar (with a correct manifest)
Answer: C
解析:
不懂暂留;games.Chess Bob’s CLASSPATH is set (at login time) to:
/usr/lib:/home/bob/classes:/opt/java/lib:/opt/java/lib/*.jar 这一段是干嘛的,没看懂。

第二十四题:
24.Given:
3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println(“whinny”); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println(“vinny”); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16.
}
When line 15 is reached, how many objects are eligible for the garbage collector?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6

Answer: E
解析:
garbage collector:垃圾回收GC

OCJP题库1Z0-851(21/30)相关推荐

  1. Cisco认证考试教材题库合集(30套)

    2019独角兽企业重金招聘Python工程师标准>>> 由于Cisco在网络设备领域的领导地位,考取Cisco的证书也成为谋求工作.升职加薪的手段. 按照认证计划的不同,又分为CCN ...

  2. Java基础50题(3) 21~30题

    2018.3.25 Java基础50题系列源码已上传到我的github仓库,有需要的可以自取,欢迎大家提出建议 https://github.com/Lawliet0717/Java-foudamen ...

  3. 1000道Python题库系列分享21(11道编程题:内置函数专项练习)

    本期题目,答案在文末: 温馨提示 进入公众号,通过菜单"最新资源"==>"历史文章"可以快速查看分专题的文章列表,通过"最新资源"== ...

  4. 1000道Python题库系列分享24(41个客观题,numpy专题)

    封面图片:<Python程序设计基础(第2版)>,董付国,清华大学出版社 ============== 好消息:智慧树网APP"知到"中搜索"董付国" ...

  5. 1000道Python题库系列分享23(61个填空题)

    封面图片:<玩转Python轻松过二级>,董付国,清华大学出版社 ================= 本期题目,参考答案在文末: 温馨提示 关注本公众号"Python小屋&quo ...

  6. 1000道Python题库系列分享

    本文是汇总地址,推送了新的题库之后会在这里更新. 1000道Python题库系列分享一(17道) 1000道Python题库系列分享二(48道) 1000道Python题库系列分享三(30道) 100 ...

  7. 2023年华为HCIE-Dacom认证题库(H12-891)

    1.如图所示是某位网络工程师在排查OSPF故障时的输出信息.据此判断,以下哪种原因可能导致邻接关系无法正常建立. Hello报文发送时间不一致 认证密码不一致 接口的IP地址掩码不一致 区域类型不一致 ...

  8. 语言题库体型判断问题_1000道Python题库系列分享20(43道填空与判断题)

    本期题目: 参考答案: 温馨提示 进入公众号,通过菜单"最新资源"==>"历史文章"可以快速查看分专题的文章列表,通过"最新资源"== ...

  9. 数据治理工程师CDGA备考心得、时间安排、题库资源

    1.写在前面 之前做一些数据质量控制.元数据.主数据相关工作,一直忙于工作,没有去往考证的方面想,去年年底心血来潮就决定考一考,证多不压身嘛(也有部分学生向我咨询),资源在文章结尾(狗头.gif) 2 ...

最新文章

  1. iOS技术篇1-CocoaPods
  2. seaborn heatmap绘制热力图cmap参数的含义
  3. python dll 变量
  4. 高精度模板(含加减乘除四则运算)
  5. JPA规范:一对多、一对一、多对多的双向关联与级联操作以及JPA联合主键
  6. python 编程之计算器
  7. 7 CO配置-控制-一般控制-维护版本
  8. Android6.0以上打电话动态权限
  9. SQL Server 2017 新功能分享
  10. Eclipse在当前行之上插入一行
  11. 符号回归工具之 geppy: Python中的基因表达编程框架
  12. 神经网络仿真实验matlab,神经网络及其matlab实现
  13. restlet client插件下载
  14. 【Axure手机原型】手机产品的规划和设计
  15. 基于matlab使用Windrose函数绘制风矢量分布图(风玫瑰图)
  16. 【转载】Oracle关于expdp、impdp以及rman介绍
  17. linux 打印文件目录结构,linux基本文件目录的结构及管理
  18. 收集12个经典的程序员段子
  19. 利用点赞功能提升Linkedin账号曝光率和活跃度
  20. 如何相对正确的安装windws桌面操作系统。

热门文章

  1. Unity3D:2D/3D 项目
  2. Oracle 正则表达式详解(regexp_substr、regexp_instr、regexp_replace、regexp_like)
  3. 网络基础——IP地址子网掩码MAC地址DNS(详解)
  4. (附源码)计算机毕业设计SSM音乐推荐系统
  5. C语言学生成绩管理系统(课程设计报告书)
  6. checkbox多选框,radio单选框
  7. python 安装包时添加国内源
  8. war3骚招之剑牛拆家流
  9. 《趣谈网络协议》学习笔记DAY10
  10. 极氪要上市,吉利“基因改造”成功