例题1: Choose the three valid identifiers from those listed below.
4 q/ r( v# K: m2 ~* b A. IDoLikeTheLongNameClass . x/ `! x H' e3 U a& \9 u
B. $byte 1 h7 d" P. T e4 A
C. const
! a* K2 ^& E/ V$ p D. _ok 9 w, ], `+ ^1 h# Q$ q. }7 B& t
E. 3_case 6 u# k/ p" {* I- X- z; J" f1 _
解答:A, B, D $ g# S/ J& p4 R- y
点评:Java中的标示符必须是字母、美元符($)或下划线(_)开头。关键字与保留字不能作为标示符。选项C中的const是Java的保留字,所以不能作标示符。选项E中的3_case以数字开头,违反了Java的规则。 - z4 O6 P& N0 e0 b9 B
例题2:
" c9 \! c7 `. D8 k+ K% c* c4 ]1 I. A How can you force garbage collection of an object?
% N4 U" D2 p& S A. Garbage collection cannot be forced
V; R3 Y7 w7 W B. Call System.gc().
* p; ]1 j6 j- p. m2 K) w/ i C. Call System.gc(), passing in a reference to the object to be garbage collected. 8 M& T* }- f; P4 l
D. Call Runtime.gc(). 2 {9 g+ @) W0 q( [7 j5 E
E. Set all references to the object to new values(null, for example). f8 j1 y( V! {. i- w, l
解答:A $ W) [& k9 j3 g2 q
点评:在Java中垃圾收集是不能被强迫立即执行的。调用System.gc()或Runtime.gc()静态方法不能保证垃圾收集器的立即执行,因为,也许存在着更高优先级的线程。所以选项B、D不正确。选项C的错误在于,System.gc()方法是不接受参数的。选项E中的方法可以使对象在下次垃圾收集器运行时被收集。
( G8 I# R, e; I8 i 例题3:
' }4 d( l2 `/ E Consider the following class: " x# Q# Y7 k" s5 S0 H+ W& S) N
1. class Test(int i) {
7 `" ^* E- t/ ^ 2. void test(int i) { ! n1 B. k. ]. I0 {3 Q
3. System.out.println(“I am an int.”);
6 V1 ~- A' q; c% N9 w& V7 ? 4. } $ `( G3 Y! T9 J+ Q
5. void test(String s) {
2 r; h8 i+ t; ~3 B 6. System.out.println(“I am a string.”);
* a3 m2 J) d# ]/ P0 G( [- O* L 7. } 7 ^5 K/ C, V7 f2 y0 E* ] c
0 c* U! y e# ? 8.
' q$ m$ ~3 `9 L @( {$ C * \* ?+ n2 C$ E, S5 u% F
9. public static void main(String args[]) {
7 L; B& g$ d6 C" e 10. Test t=new Test(); ( s! N+ Q, n# W) Y2 g
11. char ch=“y”;
# X" h4 o) W+ p6 j 12. t.test(ch); ( T3 H! w8 W) N1 `
13. } 3 |9 d; E! ^! R8 T9 h% s
14. }
( U: m4 @# g5 y Which of the statements below is true?(Choose one.) ) F" u/ l) T3 J
A. Line 5 will not compile, because void methods cannot be overridden. 9 i4 T; E6 u9 z8 p
B. Line 12 will not compile, because there is no version of test() that rakes a char argument. ) b% T1 K0 [4 a% c
C. The code will compile but will throw an exception at line 12.
: O% N0 }' y( V% T( T/ p/ Z f D. The code will compile and produce the following output: I am an int.
' C6 L- }& {; l" G/ d3 @$ h$ w# a1 P7 v8 W6 W E. The code will compile and produce the following output: I am a String. & n, l) f2 T h# t' w* G ^" U
解答:D F% Z8 r% K7 p9 E+ M: A
点评:在第12行,16位长的char型变量ch在编译时会自动转化为一个32位长的int型,并在运行时传给void test(int i)方法。 |