官网上给的样题很少,带(*)的为正确答案。

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&lt;String, Integer&gt; myMap = new TreeMap&lt;String, Integer&gt;();
8.     myMap.put("ak", 50);  myMap.put("co", 60);
9.     myMap.put("ca", 70);  myMap.put("ar", 80);
10.     NavigableMap&lt;String, Integer&gt; 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的更多相关文章

  1. vue框架muse-ui官网文档主题错误毕竟【01】

    在使用了element-ui后,总觉得不尽兴,再学一个响应式的muse-ui发现是个小众框架,但是我很喜欢. 指出官网文档里的主题使用描述错误. 首先,在vue-cli里安装raw-loader:np ...

  2. Reveal常用技巧(翻译来自Reveal官网blog)

    翻译来自官网:http://revealapp.com/blog/reveal-common-tips-cn.html 以下基于Reveal 1.6. 用于快速上手的内置应用 刚刚下载Reveal,啥 ...

  3. 发布基于Orchard Core的友浩达科技官网

    2018.9.25 日深圳市友浩达科技有限公司发布基于Orchard Core开发的官网 http://www.weyhd.com/. 本篇文章为你介绍如何基于Orchard Core开发一个公司网站 ...

  4. Flink官网文档翻译

    http://ifeve.com/flink-quick-start/ http://vinoyang.com/2016/05/02/flink-concepts/ http://wuchong.me ...

  5. Bootstrap--模仿官网写一个页面

    本文参考Bootstrap官方文档写了简单页面来熟悉Bootstrap的栅格系统.常用CSS样.Javascript插件和部分组件. 以下html代码可以直接复制本地运行: BootstrapPage ...

  6. Android学习 多读官网,故意健康---手势

    官网地址 ttp://developer.android.com/training/gestures/detector.html: 一.能够直接覆盖Activity的onTouch方法 public ...

  7. 最直观的poi的使用帮助(告诉你怎么使用poi的官网),操作word,excel,ppt

    最直观的poi的使用帮助(告诉你怎么使用poi的官网),poi操作word,excel,ppt 写在最前面 其实poi的官网上面有poi的各种类和接口的使用说明,还有非常详细的样例,所以照着这些样例来 ...

  8. 几个比較好的IT站和开发库官网

    几个比較好的IT站和开发库官网 1.IT技术.项目类站点 (1)首推CodeProject,一个国外的IT站点,官网地址为:http://www.codeproject.com,这个站点为程序开发人员 ...

  9. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网

    官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...

随机推荐

  1. MM常用的双关语(男士必读)

    我们还是当朋友好了 (其实你还是有多馀的利用价值)我想我真的不适合你(我根本就不喜欢你.)天气好冷喔,手都快结冰了 (快牵我的手吧,大木头!)我觉得我需要更多一点的空间 (我不太想看到你啦!)其实你人 ...

  2. Java反射学习总结终(使用反射和注解模拟JUnit单元测试框架)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 本文是Java反射学习总结系列的最后一篇了,这里贴出之前文章的链接,有兴趣的可以打开看看. ...

  3. [array] leetCode-15. 3Sum-Medium

    leetCode-15. 3Sum-Medium descrition Given an array S of n integers, are there elements a, b, c in S ...

  4. Android系统开发(5)——Eclipse for C/C++

    一.下载JDK 官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 二 ...

  5. HDU 1407 测试你是否和LTC水平一样高 枚举、二分、hash

    http://acm.hdu.edu.cn/showproblem.php?pid=1407 计算方程x^2+y^2+z^2= num的一个正整数解.num为不大于10000的正整数 思路: 方法一. ...

  6. java I/O库的设计模式

    在java语言 I/O库的设计中,使用了两个结构模式,即装饰模式和适配器模式.      在任何一种计算机语言中,输入/输出都是一个很重要的部分.与一般的计算机语言相比,java将输入/输出的功能和使 ...

  7. golang 操作 Redis & Mysql & RabbitMQ

    golang 操作 Redis & Mysql & RabbitMQ Reids 安装导入 go get github.com/garyburd/redigo/redis import ...

  8. 【b802】火柴棒等式

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴 ...

  9. 微信开发学习日记(六):weiphp框架

    最近重点在看weiphp这个开源的第三方微信公众平台框架. 在网上找微信资料,找到了这个.很早之前,就初步学习了Thinkphp和Onethink2个开源框架,当看到weiphp是用这2个框架开发的时 ...

  10. 【u110】灾后重建

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前, ...