35道SCJP考试真题精解
35道SCJP考试真题精解 例题1 Choose the three valid identifiers from those listed below. A. IDoLikeTheLongNameClass B. byte C. const D. _ok E. 3_case 解答A, B, D 点评Java中的标示符必需是字母、美元符或下划线_开头。关键字与保留字不能作为标示符。选项C中的const是Java的保留字,所以不能作标示符。选项E中的3_case以数字开头,违反了Java的规则。 例题2 How can you force garbage collection of an object A. Garbage collection cannot be forced B. Call System.gc. C. Call System.gc, passing in a reference to the object to be garbage collected. D. Call Runtime.gc. E. Set all references to the object to new valuesnull, for example. 解答A 点评在Java中垃圾收集是不能被强迫马上执行的。调用System.gc或Runtime.gc静态方法不能保证垃圾收集器的马上执行,因为,或许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。 例题3 以下是引用片段 Considerthefollowingclass 1.classTestinti{ 2.voidtestinti{ 3.System.out.println“Iamanint.”; 4.} 5.voidtestStrings{ 6.System.out.println“Iamastring.”; 7.} 8. 9.publicstaticvoidmainStringargs[]{ 10.TesttnewTest; 11.charch“y”; 12.t.testch; 13.} 14.} Which of the statements below is trueChoose one. A. Line 5 will not compile, because void s cannot be overridden. B. Line 12 will not compile, because there is no version of test that rakes a char argument. C. The code will compile but will throw an exception at line 12. D. The code will compile and produce the following output I am an int. E. The code will compile and produce the following output I am a String. 解答D 点评在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给void testint i方法。 例题4 Which of the following lines of code will compile without error A. 以下是引用片段 inti0; ifi{ System.out.println“Hi”; } B. 以下是引用片段 booleanbtrue; booleanb2true; ifbb2{ System.out.println“Sotrue”; } C. 以下是引用片段 inti1; intj2; ifi1||j2 System.out.println“OK”; D. 以下是引用片段intln“OK”; 解答B, C 点评选项A错,因为if语句后须要一个boolean类型的表达式。逻辑操作有、、| 和 、||,但是“|”是非法的,所以选项D不正确。 例题5 以下是引用片段 WhichtwodemonstrateahasarelationshipChoosetwo A.publicinterfacePerson{} publicclassEmployeeextendsPerson{} B.publicinterfaceShape{} publicinterfaceRectandleextendsShape{} C.publicinterfaceColorable{} publicclassShapeimplementsColorable {} D.publicclassSpecies{} publicclassAnimal{privateSpeciesspecies;} E.interfaceComponent{} classContainerimplementsComponent{ privateComponent[]children; } 解答D, E 点评 在Java中代码重用有两种可能的方式,即组合“has a”关系和继承“is a”关系。“has a”关系是通过定义类的属性的方式实现的;而“is a”关系是通过类继承实现的。本例中选项A、B、C体现了“is a”关系;选项D、E体现了“has a”关系。 例题6 Which two statements are true for the class java.util.TreeSet Choose two A. The elements in the collection are ordered. B. The collection is guaranteed to