ocjp(scjp) 的官网样题收录-20130723
官网上给的样题很少,带(*)的为正确答案。
OBJECTIVE: 1.5: Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method.
1) Given:
1. class SuperFoo {
2. SuperFoo doStuff(int x) {
3. return new SuperFoo();
4. }
5. }
6.
7. class Foo extends SuperFoo {
8. // insert code here
9. }
And four declarations:
I. Foo doStuff(int x) { return new Foo(); }
II. Foo doStuff(int x) { return new SuperFoo(); }
III. SuperFoo doStuff(int x) { return new Foo(); }
IV. SuperFoo doStuff(int y) { return new SuperFoo(); }
Which, inserted independently at line 8, will compile?
a) Only I.
b) Only IV.
c) Only I and III.
d) Only I, II, and III.
e) Only I, III, and IV. (*)
f) All four declarations will compile.
REFERENCE:
JLS 3.0
Option E is correct. Foo doStuff() cannot return a SuperFoo and co-variant returns are legal.
这道题我之前被一句话误导了“子类重写父类的方法的时候,方法返回值的类型一定相同,否则编译不过”(这话是不是过时了),以上的134都是对的,我在1.6.0_26版本上试了下,能正确打印出134句的值。
class SuperFoo {
SuperFoo doStuff(int x) {
System.out.println("SuperFoo");
return new SuperFoo();
}
} class Foo extends SuperFoo {
// insert code here
Foo doStuff(int x) { System.out.println("1. Foo"); return new Foo(); }
// Foo doStuff(int x) { return new SuperFoo(); } //Compile Error!
// SuperFoo doStuff(int x) { System.out.println("3. Foo"); return new Foo(); }
// SuperFoo doStuff(int y) { System.out.println("4. Foo"); return new SuperFoo(); }
} public class BTest{
public static void main(String[] args){ Foo f = new Foo();
f.doStuff(1);
}
}
-------
OBJECTIVE: 2.2: Develop code that implements all forms of loops and iterators, including the use of for, the enhanced for (for-each), do, while, labels, break, and continue; and explain the values taken by loop counter variables during and after loop execution.
2) Given:
5. public class Buddy {
6. public static void main(String[] args) {
7. def:
8. for(short s = 1; s < 7; s++) {
9. if(s == 5) break def;
10. if(s == 2) continue;
11. System.out.print(s + ".");
12. }
13. }
14. }
What is the result?
a) 1.
b) 1.2.
c) 1.3.4. (*)
d) 1.2.3.4.
e) 1.3.4.5.6.
f) 1.2.3.4.5.6.
g) Compilation fails.
REFERENCE:
JLS 3.0
Option C is correct. The continue skips the current iteration, the break ends the entire loop.
OBJECTIVE: 2.5: Recognize the effect of an exception arising at a specified point in a code fragment. Note that the exception may be a runtime exception, a checked exception, or an error.
3) Given:
1. class Birds {
2. public static void main(String [] args) {
3. try {
4. throw new Exception();
5. } catch (Exception e) {
6. try {
7. throw new Exception();
8. } catch (Exception e2) { System.out.print("inner "); }
9. System.out.print("middle ");
10. }
11. System.out.print("outer ");
12. }
13. }
What is the result?
a) inner
b) inner outer
c) middle outer
d) inner middle outer (*)
e) middle inner outer
f) Compilation fails.
g) An exception is thrown at runtime.
REFERENCE:
JLS 3.0
Option D is correct. It is legal to nest try/catches and normal flow rules apply.
public class Birds {
public static void main(String [] args) {
try {
throw new Exception();
} catch (Exception e) {
try {
throw new Exception();
} catch (Exception e2) { System.out.print("inner "); }
System.out.print("middle ");
}
System.out.print("outer ");
}
}
----------
OBJECTIVE: 3.3: Develop code that serializes and/or de-serializes objects using the following APIs from java.io: DataInputStream, DataOutputStream, FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream, and Serializable.
4) Given:
2. import java.io.*;
3. public class Network {
4. public static void main(String[] args) {
5. Traveler t = new Traveler();
6. t.x1 = 7; t.x2 = 7; t.x3 = 7;
7. // serialize t then deserialize t
8. System.out.println(t.x1 + " " + t.x2 + " " + t.x3);
9. }
10. }
11. class Traveler implements Serializable {
12. static int x1 = 0;
13. volatile int x2 = 0;
14. transient int x3 = 0;
15. }
If, on line 7, t is successfully serialized and then deserialized, what is the result?
a) 0 0 0
b) 0 7 0
c) 0 7 7
d) 7 0 0
e) 7 7 0 (*)
f) 7 7 7
REFERENCE:
API
Option E is correct. Fields declared as transient or static are ignored by the deserialization process.
OBJECTIVE: 3.5: Write code that uses standard J2SE APIs in the java.util and java.util.regex packages to format or parse strings or streams. For strings, write code that uses the Pattern and Matcher classes and the String.split method. Recognize and use regular expression patterns for matching (limited to: .(dot), *(star), +(plus), ?, \d, \s, \w, [], ()) The use of *, +, and ? will be limited to greedy quantifiers, and the parenthesis operator will only be used as a grouping mechanism, not for capturing content during matching. For streams, write code using the Formatter and Scanner classes and the PrintWriter.format/printf methods. Recognize and use formatting parameters (limited to: %b, %c, %d, %f, %s) in format strings.
5) Which regex pattern finds both 0x4A and 0X5 from within a source file?
a) 0[xX][a-fA-F0-9]
b) 0[xX](a-fA-F0-9)
c) 0[xX]([a-fA-F0-9])
d) 0[xX]([a-fA-F0-9])+ (*)
e) 0[xX]([a-fA-F0-9])?
REFERENCE:
API,
Option D is correct. The + quantifier finds 1 or more occurrences of hex characters after an 0x is found.
OBJECTIVE: 4.3: Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems.
6) Given:
5. public class Lockdown implements Runnable {
6. public static void main(String[] args) {
7. new Thread(new Lockdown())start();
8. new Thread(new Lockdown())start();
9. }
10. public void run() { locked(Thread.currentThread()getId()); }
11. synchronized void locked(long id) {
12. System.out.print(id + "a ");
13. System.out.print(id + "b ");
14. }
15. }
What is true about possible sets of output from this code?
a) Set 6a 7a 7b 8a and set 7a 7b 8a 8b are both possible.
b) Set 7a 7b 8a 8b and set 6a 7a 6b 7b are both possible. (*)
c) It could be set 7a 7b 8a 8b but set 6a 7a 6b 7b is NOT possible.
d) It could be set 7a 8a 7b 8b but set 6a 6b 7a 7b is NOT possible.
REFERENCE:
JLS 3.0
Option B is correct. Two different Lockdown objects are using the locked() method.
OBJECTIVE: 5.5: Develop code that implements "is-a" and/or "has-a" relationships.
7) A programmer wants to develop an application in which Fizzlers are a kind of Whoosh, and Fizzlers also fulfill the contract of Oompahs. In addition, Whooshes are composed with several Wingits.
Which code represents this design?
a) class Wingit { }
class Fizzler extends Oompah implements Whoosh { }
interface Whoosh {
Wingits [] w;
}
class Oompah { }
b) class Wingit { }
class Fizzler extends Whoosh implements Oompah { }
class Whoosh {
Wingits [] w;
}
interface Oompah { } (*)
c) class Fizzler { }
class Wingit extends Fizzler implements Oompah { }
class Whoosh {
Wingits [] w;
}
interface Oompah { }
d) interface Wingit { }
class Fizzler extends Whoosh implements Wingit { }
class Wingit {
Whoosh [] w;
}
class Whoosh { }
REFERENCE:
Sun's Java course OO226
Option B is correct. 'Kind of' translates to extends, 'contract' translates to implements, and 'composed' translates to a has-a implementation.
OBJECTIVE: 6.3: Write code that uses the generic versions of the Collections API, in particular, the Set, List, and Map interfaces and implementation classes. Recognize the limitations of the non-generic Collections API and how to refactor code to use the generic versions. Write code that uses the NavigableSet and NavigableMap interfaces.
8) Given:
4. import java.util.*;
5. public class Quest {
6. public static void main(String[] args) {
7. TreeMap<String, Integer> myMap = new TreeMap<String, Integer>();
8. myMap.put("ak", 50); myMap.put("co", 60);
9. myMap.put("ca", 70); myMap.put("ar", 80);
10. NavigableMap<String, Integer> myMap2 = myMap.headMap("d", true);
11. myMap.put("fl", 90);
12. myMap2.put("hi", 100);
13. System.out.println(myMap.size() + " " + myMap2.size());
14. }
15. }
What is the result?
a) 4 4
b) 5 4
c) 5 5
d) 6 5
e) 6 6
f) Compilation fails.
g) An exception is thrown at runtime. (*)
REFERENCE:
API
Answer: G is correct. Line 12 causes a "key out of range" exception.
import java.util.*;
public class Quest {
public static void main(String[] args) {
TreeMap<String, Integer> myMap = new TreeMap<String, Integer>();
myMap.put("ak", 50); myMap.put("co", 60);
myMap.put("ca", 70); myMap.put("ar", 80);
NavigableMap<String, Integer> myMap2 = myMap.headMap("d", true);
myMap.put("fl", 90);
myMap2.put("hi", 100);
System.out.println(myMap.size() + " " + myMap2.size());
}
}/** Output
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
at Quest.main(Quest.java:10)
*/
知识点:http://www.cjsdn.net/doc/jdk50/java/util/SortedMap.html
headMap
SortedMap<K,V> headMap(K toKey)
- 返回此有序映射的部分视图,其键值严格小于 toKey。返回的有序映射由此有序映射支持,因此返回有序映射中的更改将反映在此有序映射中,反之亦然。返回的映射支持此有序映射支持的所有可选映射操作。
如果用户试图插入超出指定范围的键,则此方法返回的映射将抛出 IllegalArgumentException。
注:此方法总是返回不包括(高)终结点的视图。如果需要包含此终结点的视图,而且键类型允许计算给定键值的后继值,则只需要请求以 successor(highEndpoint) 为界的 headMap。例如,假设 m 是一个用字符串作为键的映射。下面的语句将得到一个包含 m 中键值小于或等于 high 的所有键-值映射关系的视图:
Map head = m.headMap(high+"\0");
-
-
- 参数:
toKey
- subMap 的高终结点(不包括)。- 返回:
- 此有序映射指定初始区间的视图。
- 抛出:
ClassCastException
- 如果 toKey 与此映射的比较器不兼容(如果此映射没有比较器,并且 toKey 没有实现 Comparable)。如果 fromKey 或 toKey 不能与映射中的当前键进行比较,则实现可能抛出此异常,但不要求。IllegalArgumentException
- 如果此映射本身是一个 subMap、headMap 或 tailMap,并且 toKey 不在 subMap、headMap 或 tailMap 指定的范围之内。NullPointerException
- 如果 toKey 为 null,并且此有序映射不接受 null 键。
------------------
OBJECTIVE: 6.5: Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang.String on sorting.
9) Given:
3. import java.util.*;
4. public class ToDo {
5. public static void main(String[] args) {
6. String[] dogs = {"fido", "clover", "gus", "aiko"};
7. List dogList = Arrays.asList(dogs);
8. dogList.add("spot");
9. dogs[0] = "fluffy";
10. System.out.println(dogList);
11. for(String s: dogs) System.out.print(s + " ");
12. }
13. }
What is the result?
a) [fluffy, clover, gus, aiko]
fluffy, clover, gus, aiko,
b) [fluffy, clover, gus, aiko]
fluffy, clover, gus, aiko, spot,
c) [fluffy, clover, gus, aiko, spot]
fluffy, clover, gus, aiko,
d) [fluffy, clover, gus, aiko, spot]
fluffy, clover, gus, aiko, spot,
e) Compilation fails.
f) An exception is thrown at runtime. (*)
REFERENCE:
API
Option F is correct. The asList() method creates a fixed-size list that is backed by the array, so no additions are possible.
OBJECTIVE: 7.2: Given an example of a class and a command-line, determine the expected runtime behavior.
10) Given:
1. class x {
2. public static void main(String [] args) {
3. String p = System.getProperty("x");
4. if(p.equals(args[1]))
5. System.out.println("found");
6. }
7. }
Which command-line invocation will produce the output found?
a) java -Dx=y x y z
b) java -Px=y x y z
c) java -Dx=y x x y z (*)
d) java -Px=y x x y z
e) java x x y z -Dx=y
f) java x x y z -Px=y
REFERENCE:
API for java command
Option C is correct. -D sets a property and args[1] is the second argument (whose value is y)
ocjp(scjp) 的官网样题收录-20130723的更多相关文章
- vue框架muse-ui官网文档主题错误毕竟【01】
在使用了element-ui后,总觉得不尽兴,再学一个响应式的muse-ui发现是个小众框架,但是我很喜欢. 指出官网文档里的主题使用描述错误. 首先,在vue-cli里安装raw-loader:np ...
- Reveal常用技巧(翻译来自Reveal官网blog)
翻译来自官网:http://revealapp.com/blog/reveal-common-tips-cn.html 以下基于Reveal 1.6. 用于快速上手的内置应用 刚刚下载Reveal,啥 ...
- 发布基于Orchard Core的友浩达科技官网
2018.9.25 日深圳市友浩达科技有限公司发布基于Orchard Core开发的官网 http://www.weyhd.com/. 本篇文章为你介绍如何基于Orchard Core开发一个公司网站 ...
- Flink官网文档翻译
http://ifeve.com/flink-quick-start/ http://vinoyang.com/2016/05/02/flink-concepts/ http://wuchong.me ...
- Bootstrap--模仿官网写一个页面
本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...
- Android学习 多读官网,故意健康---手势
官网地址 ttp://developer.android.com/training/gestures/detector.html: 一.能够直接覆盖Activity的onTouch方法 public ...
- 最直观的poi的使用帮助(告诉你怎么使用poi的官网),操作word,excel,ppt
最直观的poi的使用帮助(告诉你怎么使用poi的官网),poi操作word,excel,ppt 写在最前面 其实poi的官网上面有poi的各种类和接口的使用说明,还有非常详细的样例,所以照着这些样例来 ...
- 几个比較好的IT站和开发库官网
几个比較好的IT站和开发库官网 1.IT技术.项目类站点 (1)首推CodeProject,一个国外的IT站点,官网地址为:http://www.codeproject.com,这个站点为程序开发人员 ...
- Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...
随机推荐
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- php 如何写一个自己项目的安装程序
版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...
- java 封装解析 Json数据。
import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; im ...
- Win或Linux中编译安装软件的命令解析: configure; make; make install
原文地址:http://www.cnblogs.com/Jerry-Chou/archive/2010/12/18/1909843.html 翻译一篇文章,我最早从这篇文章中了解到为什么Linux平台 ...
- 浅谈java中异常抛出后代码是否会继续执行
问题 今天遇到一个问题,在下面的代码中,当抛出运行时异常后,后面的代码还会执行吗,是否需要在异常后面加上return语句呢? public void add(int index, E element) ...
- Linux环境下Apache ActiveMQ 基本安装
原文链接:https://www.jianshu.com/p/1c017088aa95 在linux上安装mq,并映射到外网.1.Apache ActiveMQ安装基本条件请参考链接:2.下载Apac ...
- 用Java对CSV文件进行读写操作
需要jar包:javacsv-2.0.jar 读操作 // 读取csv文件的内容 public static ArrayList<String> readCsv(String filepa ...
- ArcGIS Engine 编辑- IWorkspaceEdit
转自原文 ArcGIS Engine 编辑- IWorkspaceEdit 这个例子中,我创建了1000条要素,并结合缓冲将数据写到文件中,并且添加了时间统计,当然数据是我捏造的,还请原谅,这个花费的 ...
- (三)Unity5.0新特性------动画的StateMachineBehaviours
出处:http://blog.csdn.net/u010019717 author:孙广东 时间:2015.3.31 (State machine behaviours)状态机的行为在Ani ...
- ios开发多选照片实现
#import "ViewController.h" #import <Photos/Photos.h> @interface ViewController () &l ...