Topic: 【讨论】 几道SCJP题目 |
Print this page |
1.【讨论】 几道SCJP题目 | Copy to clipboard |
Posted by: acelee Posted on: 2002-07-26 18:03 本人对下面的一些题感到疑惑,希望各位能人帮助解答,多谢了!42. Given: 3. string foo = “ABCDE”; 4. foo.substring(3); 5. foo.concat(“XYZ”); 6. Type the value of foo at line 6. Ans: ABCDE,为什么不是DEXYZ?,concat()方法怎么用?我查了api,但没看明白。 63. Given: 5. String foo = “base”; 6. foo.substring(0,3); 7. foo.concat(“ket”) 8. Type the value of foo at line 8. Ans:base 为什么不是baseket,同上题。 57. Given: 1. public class SyncTest ( 2. private int x; 3. private int y; 4. private synchronized void setX (int i) (x=1 5. private synchronized void setY (int i) (y=1 6. public void setXY(int 1)(set X; setY 7. public synchronized Boolean check() (return x !=y 8. ) Under which conditions will check () return true when called from a different class? A. Check() can never return true B. Check() can return true when setXY is called by multiple threads C. Check() can return true when multiple threads call setX and setY separately. D. Check() can only return true if SyncTest is changed to allow x and y to be set separately. answer:b 请问为什么不是c? 68. Click the exhibit button: 1. class A { 2. public int getNumber(int a) { 3. return a + 1; 4. } 5. } 6. 7. class B extends A { 8. public int getNumber (int a) { 9. return a + 2 10. } 11. 12. public static void main (String args[]) { 13. A a = new B(); 14. System.out.printIn(a.getNumber(0)); 15. } 16. } What is the result? A. Compilation succeeds and 1 is printed. B. Compilation succeeds and 2 is printed. C. An error at line 8 causes compilation to fail. D. An error at line 13 causes compilation to fail. E. An error at line 14 causes compilation to fail. 答案:b 为什么不是a啊?A a=new B()怎么解释? 78. Which two statements are true regarding the creation of a default constructor? (Choose Two) A. The default constructor initializes method variables. B. The compiler always creates a default constructor for every class. C. The default constructor invokes the no-parameter constructor of the superclass. D. The default constructor initializes the instance variables declared in the class. E. When a class has only constructors with parameters, the compiler does not create a default constructor. 答案:D E 情解释D项为什么对。 86. Click the exhibit button: 1. package foo; 2. 3. import java.util.Vector; 4. 5. private class MyVector extends Vector { 6. int i = 1; 7. public MyVector() { 8. i = 2; 9. } 10. } 11. 12. public class MyNewVector extends MyVector { 13. public MyNewVector () { 14. i = 4; 15. } 16. public static void main (String args []) { 17. MyVector v = new MyNewVector(); 18. } 19. } The file MyNewVector.java is shown in the exhibit. What is the result? A. Compilation will succeed B. Compilation will fail at line 5 C. Compilation will fail at line 6 D. Compilation will fail at line 14 E. Compilation will fail at line 17 答案:B 是不是应该用public 修饰? 97. Click the exhibit button 1. public class X { 2. public static void main (String[]args) { 3. int [] a = new int [1] 4. modify; 5. System.out.printIn(a[0]); 6. } 7. 8. public static void modify (int[] a) { 9. a[0] ++; 10. } 11. } What is the result? A. The program runs and prints “0”. B. The program runs and prints “1”. C. The program runs but aborts with an exception. D. An error “possible undefined variable” at line 4 causes compilation to fail. E. An error “possible undefined variable” at line 9 causes compilation to fail. 答案:b,为什么不是a啊?难道是数组的问题吗? 101. Which statement is true? A. The Error class is a untimeException. B. No exceptions are subclasses of Error. C. Any statement that may throw an Error must be enclosed in a try block. D. Any statement that may throw an Exception must be enclosed in a try block. E. Any statement that may thro a runtimeException must be enclosed in a try block. 答案:d,为什么不是b? 106. Click the exhibit button: 1. public class X implements Runnable( 2. private int x; 3. private int y; 4. 5. public static void main(String[]args) 6. X that = new X(); 7. (new Thread(that)).start(); 8. (new Thread(that)).start(); 9. ) 10. 11. public void run() ( 12. for (; ( 13. x++; 14. y++; 15. System.out.printIn(“x=” + x + “, y = ” + y); 16. ) 17. ) 18. ) What is the result? A. Errors at lines 7 and 8 cause compilation to fail. B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x=2, y=1”). C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears twice (for example, “x=1, y=1” followed by “x=1, y=1”). D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x=1, y=1”. In addition, each value appears only for once (for example, “x=1, y=1” followed by “x=2, y=2”). 答案:d,为什么不是c ? 130. Given: 1. public class ExceptionTest { 2. class TestException extends Exception {} 3. public void runTest () throws TestException {} 4. public void test () /* Point X*/ { 5. runTest (); 6. } 7. } At point X on line 4, which code can be added to make the code compile? A.throws Exception B.catch (Exception e) C.throws RuntimeException D.catch (TestException e) E.no code is necessary 答案:b 为什么不是a? 135. Given: 1. public class Foo implements Runnable ( 2. public void run (Thread t) { 3. system.out.printIn(“Running.”); 4. } 5. public static void main (String[] args) { 6. new thread (new Foo()).start(); 7. ) 8. ) What is the result? A. An exception is thrown, B. The program exits without printing anything. C. An error at line 1 causes compilation to fail. D. An error at line 6 causes the compilation to fail. E. “Running” is printed and the program exits.答案:c,为什么不是A,实际上第二行错了 141. Click on the exhibit button: 1. class A { 2. public String toString () { 3. return “4”; 4. } 5. } 6. class B extends A { 7. 8. public String toString () { 8. return super.toString() + “3”; 9. } 10. } 11. public class Test { 12. public static void main(String[]args) { 13. System.out.printIn(new B()); 14. } 15. } What is the result? A. Compilation succeeds and 4 is printed. B. Compilation succeeds and 43 is printed. C. An error on line 9 causes compilation to fail. D. An error on line 14 causes compilation to fail. E. Compilation succeeds but an exception is thrown at line 9 答案:B,为什么不是 D,System.out.prinln(new B())是什么意思? An anonymous inner class constructor can take arguments in some situation.这句话对吗?为什么? 143. Given: 1. public class MethodOver { 2. private int x, y; 3. private float z; 4. public void setVar(int a, int b, float c){ 5. x = a; 6. y = b; 7. z = c; 8. } 9. } Which two overload the setVar method? (Choose Two) A.void setVar (int a, int b, float c){ x = a; y = b; z = c; } B. public void setVar(int a, float c, int b) { setVar(a, b, c); } C. public void setVar(int a, float c, int b) { this(a, b, c); } D. public void setVar(int a, float b){ x = a; z = b; } E. public void setVar(int ax, int by, float cz) { x = ax; y = by; z = cz; } 答案:b d,为什么不是c d? this(a,b,c)怎么解释? 145. Given: 1. public class X { 2. public object m () { 3. object o = new float (3.14F); 4. object [] oa = new object [1]; 5. oa[0]= o; 6. o = null; 7. oa[0] = null; 8.return o; 9. } 10. } When is the float object created in line 3, eligible for garbage collection? A. Just after line 5 B. Just after line 6 C. Just after line 7 D. Just after line 8(that is, as the method returns) 答案:c,为什么不是d? 这类题怎么答? |
2.回复: 【讨论】 104 [Re: acelee] | Copy to clipboard |
Posted by: yamakasy Posted on: 2002-07-26 19:38 朋友,我本来准备回答可是看了4个题目就受不了了,太累了,没有格式。所以。。 |
3.回复: 【讨论】 104 [Re: acelee] | Copy to clipboard |
Posted by: jmd7 Posted on: 2002-07-27 14:11 朋友,劝你好好看看chairyuan的笔记吧,难是难了点,不过真的很精彩。你好像很多基本问题还没解决。比如第一题,string是immutable类,它的对象内容不能改变,那些函数只能返回一个新的string对象,StringBuffer类才能改变内部的内容。 |
4.我回答,帮我指正 [Re: acelee] | Copy to clipboard |
Posted by: fatboyslim Posted on: 2002-08-02 10:48 42. 那个应该这么用String a=foo.concat(“XYZ”); ,也就是说.concat只给一个返回值,并不修改源。 63.同上! 57.C,只有非别调用才能是两个值不同。 68.getNumber方法被覆盖 A a=new B();会调用子类B的方法。 78.我也不明白 86.class or public class ,no private class 97.先执行了modify;当然是a[0]=1啦^_^ 101.不明白的说! 106.由于两个线程的进度是不确定的,CPU先做哪个是不定的。 130.应该是A吧,哪儿有在那个地方catch的。 135.我编译过了,第一行出错,说没有写run方法。如果把Thread t参数删掉则会在第6行出错,谁能给讲讲? 141.super.toString() + “3”; 就是把父类(要不就叫超类,我搞不清楚)的toString执行一下,有了4在加个3。 143.overload是只能参数必须不同,方法可以不同。 145.5. oa[0]= o; oa[0],a都指向存3.14F的地址 6. o = null; a不指向 7. oa[0] = null;oa[0]也不指向了。 这种题可以画个图,只要一个空间没有对他指向的就要把他当垃圾回收。 献丑了!请指正。 |
5.回复: 【讨论】 几道SCJP题目 [Re: acelee] | Copy to clipboard |
Posted by: yamakasy Posted on: 2002-08-03 11:54 不错,加油!为了鼓励你!给你加分 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |