aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam question" Watch "Mock exam question" New topic
Author

Mock exam question

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Please help me to understand the below question.

Read the code below carefully

//Contents of Constants.java




//Contents of SomeClass.java




The Constants.java file is edited and the greetingString is changed to "Hello World!". The file after editing is shown below.




On Compilation of Constants.java and running of the application gives. (Note : The client class SomeClass is not compiled)

1. A RuntimeException due to incompatable change in class Constants
2. An Error due to incompatable change in class Constants
3. The program terminates normally and "Hello" is printed on the system console.
4. The program terminates normally and "Hello World!" is printed on the system console.

The answer they have given is Hello.But executing only the third part giving runtimeException NoSuchMethodError :Main.In order to get some result,I modified the Constants class and that coding is



But it gives the output HelloWorld instead of giving Hello. But the answer given is (option 3).Please help me to get the result Hello even after modifying.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Who is "they"? From what mock exam is this question?

Are you using an IDE? Have you tried this with a simple editor and compiling the classes at the command line?
[ July 24, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
The Output will be NoSuchMethodError : main
It cannot be "hello";
We cannot run a program without main().


If you worry you cannot work... If you work you need not worry
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
Isn't this behavior (I am talking about the first part of the question, where it prints "Hello" instead of "HelloWorld") dependent on compiler optimization? I guess since the string reference was final, so on compiling SomeClass.java, the compiler literally 'inserted' the value inside the SomeClass class file, thereby eliminating any dependency on Constants.java. (The proof of this is that SomeClass runs even when Constants.class is deleted.)

Is such behavior, which, IMHO, an example of compiler optimization, a valid candidate for the exam question?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I understand the problem as follows:
Using the original two files,
1) compile Constants.java
2) compile SomeClass.java
3) run the application SomeClass.java - get "Hello" output
4) change Constants.java
5) compile Constants.java
6) run the application SomeClass.java - get "Hello" output once more


That's what you get using command line tools.

An IDE may recompile SomeClass.java if it notices Constants.java has changed. Then you would get "HelloWorld" output.

Running the application means running the class with a main method (SomeClass), not the Constants class.
[ July 24, 2006: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Neelesh Bodas:
Isn't this behavior (I am talking about the first part of the question, where it prints "Hello" instead of "HelloWorld") dependent on compiler optimization? I guess since the string reference was final, so on compiling SomeClass.java, the compiler literally 'inserted' the value inside the SomeClass class file, thereby eliminating any dependency on Constants.java. (The proof of this is that SomeClass runs even when Constants.class is deleted.)

Is such behavior, which, IMHO, an example of compiler optimization, a valid candidate for the exam question?


Yes, you are correct - the Constants.java and the class file Constants can be removed. The class Constants is not even loaded by SomeConstants when it runs. As to being part of the SCJP objectives - no, I cannot recall it being mentioned (but my memory is not so perfect nowadays)

So once again, to the original topic poster: "who are they", and what mock exam does this come from?
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
The link is

http://www.angelfire.com/or/abhilash/Main.html and the Quiz no 9

Thanks for your help.now i got it.

(removed point at end of link to make it work)
[ July 25, 2006: Message edited by: Barry Gaunt ]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 805
Link doesn't work. If you repost a new one could you send me a message. Thanks.

Rob
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Rob Pike:
Link doesn't work. If you repost a new one could you send me a message. Thanks.

Rob


Link fixed. Check back often.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock exam question
 
Similar Threads
Deploying servlets as a war file
enum question
does an anonymous inner class have constructor?
Does SimpleDateFormat have any dependency on JDK versions
static final fields