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官网
官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...
随机推荐
- [TypeScript] Distinguishing between types of Strings in TypeScript
In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how T ...
- linux下如何获取某一进程占用的物理内存和虚拟内存
首先,ps -A查看你所查看进程的进程号 ps -A 加入进程号为pid 那么使用如下脚本,可以打印该进程使用的虚拟内存和物理内存: root@Storage:/mnt/mtd# cat rss.sh ...
- php 删除数组指定元素,下标还不乱
$arr是目标数组 $offset是要删除的元素的key 1是指删除的长度 array_splice($arr, $offset, 1); 之前用的unset,但是比如删除的是第三个,那么下标的2就会 ...
- 多事务运行并发问题spring学习笔记——数据库事务并发与锁详解
多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...
- oracle 重置序列从指定数字开始的方法详解
原文 oracle 重置序列从指定数字开始的方法详解 重置oracle序列从指定数字开始 declare n ); v_startnum ):;--从多少开始 v_step ):;--步进 tsql ...
- Linux上安装JDK 分类: B1_JAVA B3_LINUX 2014-08-29 15:12 449人阅读 评论(0) 收藏
1.下载rpm文件并安装 rpm -ivh jdk-7u51-linux-x64.rpm 2.修改/etc/profile文件,增加以下配置 export JAVA_HOME=/usr/java/jd ...
- IOS的后台执行
写在前面给大家推荐一个不错的站点 www.joblai.com 本文章由央广传媒开发部 冯宝瑞整理.哈哈 http://www.cocoachina.com/bbs/read.php? tid=14 ...
- [Angular Form] ngModel and ngModelChange
When using Radio button for Tamplate driven form, we want to change to the value change and preform ...
- 超级简单的Android Studio jni 实现(无需命令行)
1.配置Anroid Studio(这步是关键) 使用[command+,] 打开Preferences,选择External Tools,点击加号框如下图: Paste_Image.png 点击+号 ...
- 安装绿色版mysql小记(5.7.11)
平时使用oracle,感觉太耗我电脑内存了,实在不想用oracle做平时练习了,那就只装一个客户端,工作用..平时自己试试mysql吧..mysql的安装真麻烦,真不是傻瓜式安装就能用,稍微配置不对就 ...