This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
----------------------------------------------------------------------- A: The code does not compile due to line //1. B: The code does not compile due to line //2. C: The code does not compile due to line //3. D: The code compiles and runs with output: first second third </BLOCKQUOTE> I think the correct answer is A. that is because the class "Nested" is a top-level nested class (it's static), generally we can use below three ways to call the methord. new Test038().Nested.method("by instance of Test038"); new Test038.Nested().method("by instance of Nested"); Test038.Nested.method("direct by class name"); but the answer and explanation is
Answers: : B Explanation: : //2 tries to access Nested as if it is a variable of Test038, which is not the case.
I can't agree with that and try to compile and run it. the compile result is Test038.java:5: qualified new of static class new Test038().new Nested().method("first"); //1 then i modified the new Test038().new Nested().method("first"); //1 => new Test038.Nested().method("first") //1; it compiles and runs. the output is first second third wath's wrong in the Test038's answer? I found that different java vesion will bring different result. (above I use jdk1.4.) I tried jdk1.2.2, to the origin code the compile result is Test038.java:7: No variable Nested defined in class Test038. new Test038().Nested.method("second"); //2 if I commended this line then the compile was ok but the run result is Exception in thread "main" java.lang.VerifyError: (class: Test038, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack I also tried jdk1.3.0_02, to the origin code the compile result is awful, it gives An exception has occurred in the compiler (1.3.0_02). Please file a bug at the Java Developer Connection (http://java.su n.com/cgi-bin/bugreport.cgi). Include your program and the following diagnostic in your report. Thank you.java.lang.NullPointerException at com.sun.tools.javac.v8.comp.TransInner.access(TransInner.java:725) at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1563) at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1557) at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1490) at com.sun.tools.javac.v8.tree.Tree$Apply.visit(Tree.java:785) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:179) at com.sun.tools.javac.v8.tree.Tree$Exec.visit(Tree.java:699) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:47) at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:111) at com.sun.tools.javac.v8.tree.Tree$Block.visit(Tree.java:492) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:100) at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1423) at com.sun.tools.javac.v8.tree.Tree$MethodDef.visit(Tree.java:441) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1360) at com.sun.tools.javac.v8.tree.Tree$ClassDef.visit(Tree.java:402) at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35) at com.sun.tools.javac.v8.comp.TransInner.translate(TransInner.java:1322) at com.sun.tools.javac.v8.comp.TransInner.translateTopLevelClass(TransInner.java:1588) at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:397) at com.sun.tools.javac.v8.Main.compile(Main.java:247) at com.sun.tools.javac.Main.main(Main.java:16) my conclusion is: 1. befor version jdk1.4 there was bugs in treating inner class. 2. the correct answer of Test038 should be A not B.
[This message has been edited to add code tags] [This message has been edited by Marilyn deQueiroz (edited October 05, 2001).]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
Hi, galen: I am using jdk1.3.0, as to mock-exam-02-38, The code give the following compile error when I compile it:
If I comment //2 out, the code compiles and runs without any error. I don't know where is the problem. Please test it again and confirm with me if really got something wrong here. Best regards,
------------------ Guoqiao Sun Sun Certified Programmer for Java 2 Platform try my mock exam¹²³ at my homepage.
Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
Muhammad Farooq
Ranch Hand
Joined: May 08, 2001
Posts: 356
posted
0
Regardless of what the problem is, I just comment out the Line 2 and it compiles and runs, so I agree with the answer B. Sun, I am not satisfied with your explanation as I am getting the same error message as Galen is getting. Can anybody explain why B is the answer. --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
Hi, galen and Farooq, I have modified the question to avoid confusion. Thank you very much for your nice feedback. Best regards,
------------------ Guoqiao Sun Sun Certified Programmer for Java 2 Platform try my mock exam¹²³ at my homepage.
galen wang
Greenhorn
Joined: Sep 21, 2001
Posts: 13
posted
0
Hi Guoqiao, srry to bother you but owing to java's inperfect, growing and updating, modify our mind to synchronize with it seems unavoidable. for another example the mock-exam-01-15:
Answers: : A C E F Explanation: : Please refer to Java API for the details.
but from the API of JavaTM 2 Platform Std. Ed. v1.4.0, there is a FileOutputStream(File file, boolean append) existing. so the answers should include B. it's annoy but ... galen
Muhammad Farooq
Ranch Hand
Joined: May 08, 2001
Posts: 356
posted
0
Galen, "B" is NOT the correct answer. Please look in the API to confirm this. I am looking into the API of JDK 1.3. I have just checked the API of JDK 1.4, your point is valid Galen but I dont know how to take care of this issue, anyone any idea ??? --Farooq [This message has been edited by Muhammad Farooq (edited October 07, 2001).] [This message has been edited by Muhammad Farooq (edited October 07, 2001).] [This message has been edited by Muhammad Farooq (edited October 07, 2001).]
galen wang
Greenhorn
Joined: Sep 21, 2001
Posts: 13
posted
0
Hi Muhammad, please check http://java.sun.com/j2se/1.4/docs/api/index.html -> Class FileOutputStream in the Constructor Summary, there are five constructors: -------------------------------------------------------- FileOutputStream(File file) FileOutputStream(File file, boolean append) FileOutputStream(FileDescriptor fdObj) FileOutputStream(String name) FileOutputStream(String name, boolean append) -------------------------------------------------------- click FileOutputStream, you will see -------------------------------------------------------- FileOutputStream public FileOutputStream(File file, boolean append) throws FileNotFoundExceptionCreates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection. First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument. If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
Parameters: file - the file to be opened for writing. append - if true, then bytes will be written to the end of the file rather than the beginning Throws: FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason SecurityException - if a security manager exists and its checkWrite method denies write access to the file. Since: 1.4 See Also: File.getPath(), SecurityException, SecurityManager.checkWrite(java.lang.String) -------------------------------------------------------- galen
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
Hi, galen, for mock-exam-01-15 maybe I have to declare that the mock exam is for jdk1.3 since not every candidate check the latest JDK specification that often. Regards,
------------------ Guoqiao Sun Sun Certified Programmer for Java 2 Platform try my mock exam¹²³ at my homepage.
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
Seems like it works fine if you split it into two lines (see 5a plus 5b are equal to 2). I also see no reason for it not to work. It appears to me to be a bug, and not something that on the exam. If I saw it on the exam, I would answer 'D' since Nested is static and method is static.
Guoqiao Sun
Ranch Hand
Joined: Jul 18, 2001
Posts: 317
posted
0
Hi, Marilyn, I tested the code your suggested and got the same result as you, it really looks somewhat strange. Regards,
Originally posted by Marilyn deQueiroz: [CODE] Seems like it works fine if you split it into two lines (see 5a plus 5b are equal to 2). I also see no reason for it [b]not to work. It appears to me to be a bug, and not something that on the exam. If I saw it on the exam, I would answer 'D' since Nested is static and method is static.[/B]
------------------ Guoqiao Sun Sun Certified Programmer for Java 2 Platform try my mock exam¹²³ at my homepage.