Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam question

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Output will be NoSuchMethodError : main
It cannot be "hello";
We cannot run a program without main().
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Link doesn't work. If you repost a new one could you send me a message. Thanks.

Rob
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic