This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes MasterExam - Wrong Answers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "MasterExam - Wrong Answers?" Watch "MasterExam - Wrong Answers?" New topic
Author

MasterExam - Wrong Answers?

Mike Rolf
Greenhorn

Joined: Mar 04, 2008
Posts: 13
I've just started to do a few of the questions in the MasterExam simulation and some of them seem to have wrong answers. Example question below;

********************************************************************************
Question:

If three versions of MyClass.java exist on a file system:
Version 1 is in /foo/bar
Version 2 is in /foo/bar/baz
Version 3 is in /foo/bar/baz/bing

And the systems classpath includes the following:
/foo/bar/baz

Which command line(s) will use Version 2 of Myclass.java? (Choose all that apply.)
A) javac -cp /foo/bar:/foo/bar:. MyClass.java
B) javac -cp /foo/bar/baz:/foo/bar MyClass.java
C) javac -cp /foo/bar/baz/bing:/foo/bar:. MyClass.java
D) javac -cp /foo/bar/baz/bing:/foo/bar/baz MyClass.java
E) The result is not predictable.

Answer B.
******************************************************************************

I have two questions;

1) Surely the above answer is wrong because the classpath doesn't effect where javac looks for the .java file
2) Have people found other questions with wrong answers, can I expect a few more?

Cheers.
Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
Hi Mike,
I am not agree with you. Answer B is absolutely fine. classpaths are searched from left to right. and correct directory for version 2 i.e. "/foo/bar/baz" is coming first only in option B. Other options will invoke other versions of file as they will pick up different directory.
And one more point, -classpath affects javac command as well. Because javac also needs to lookup for classesas well used in your class.

Lets see if any anyone else has to add something here.


<br />
SCJP - 93%
Warm Regards,<br />Rollicking
Gaurav Arora
Ranch Hand

Joined: Aug 13, 2007
Posts: 35
Originally posted by Rahul Singhai:
Hi Mike,
I am not agree with you. Answer B is absolutely fine. classpaths are searched from left to right. and correct directory for version 2 i.e. "/foo/bar/baz" is coming first only in option B. Other options will invoke other versions of file as they will pick up different directory.
And one more point, -classpath affects javac command as well. Because javac also needs to lookup for classesas well used in your class.

Lets see if any anyone else has to add something here.


I think you misunderstood the question(either that or I have). The question asks which version of MyClass.java will be compiled, not included.

I agree with Mike, the answer is infact wrong (or misleading at the very least). The compilation is also dependent on where javac is invoked from which isn't mentioned here.
Vasy Costescu
Greenhorn

Joined: Nov 26, 2007
Posts: 3
I think Rahul is right, the answer is fine. You can use the classpath option with java and javac. If you use this option it doesn't matter that there is another classpath defined in the system classpath. Classpaths declared as command-line options override the classpath declared as an environment variable, but they persist only for the length of the invocation. Also you can see that the paths from the classpath option are absolute (they start with /) which means that it's not important from where the javac command was executed.

As Rahul said the classpaths are searched from left to right and when a version is found the search stops. So answer B is right.
Mike Rolf
Greenhorn

Joined: Mar 04, 2008
Posts: 13
Although -classpath can be used with both java and javac, it serves the same purpose. The classpath tells the JVM where to look for the .class files, not where to find the .java files.

I still think the above answer/question is wrong. Other views appreciated.

Cheers,

Mike
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Fromjavac documentation:
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files.
...
If the -sourcepath option is not specified, the user class path is also searched for source files.
Mike Rolf
Greenhorn

Joined: Mar 04, 2008
Posts: 13
I wasn't aware of that the classpath did look for .java files, if sourcepath wasn't specified. Thanks for clearning that up.

Mike
Mike Rolf
Greenhorn

Joined: Mar 04, 2008
Posts: 13
I still think that the above question/answer is wrong however, because javac requires you specify the PATH of the source file (the file to be compiled).

For example;

Directory structure: project/foo/bar/MyClass.java
Current directory: project

To compile MyClass.java the command would be: javac foo/bar/MyClass.java

Regardless of what the classpath was set to.

Agree?

Cheers,

Mike
Dan Silva
Ranch Hand

Joined: Sep 05, 2007
Posts: 86
The default directory is overridden whenever you provide a classpath.
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
You are right Mike, javac needs a file name with a path.
But after it reads the original file, it can search for additional source files wherever you specify. For example, here it load foo\bar\baz\Version.java and not foo\bar\Version.java:
javaranch>javac -cp foo\bar\baz;foo\bar -verbose -d classes foo\bar\MyClass.java
[parsing started foo\bar\MyClass.java]
[parsing completed 15ms]
[search path for source files: foo\bar\baz,foo\bar]
[search path for class files: <skip>,foo\bar\baz,foo\bar]

[checking MyClass]
[loading foo\bar\baz\Version.java]
[parsing started foo\bar\baz\Version.java]
[parsing completed 0ms]
[wrote classes\MyClass.class]
[checking Version]
[wrote classes\Version.class]
[total 187ms]

[ March 07, 2008: Message edited by: Irina Goble ]
Andriy Fedotov
Ranch Hand

Joined: Mar 07, 2008
Posts: 49
Originally posted by Mike Rolf:
I still think that the above question/answer is wrong however, because javac requires you specify the PATH of the source file (the file to be compiled).

For example;

Directory structure: project/foo/bar/MyClass.java
Current directory: project

To compile MyClass.java the command would be: javac foo/bar/MyClass.java

Regardless of what the classpath was set to.

Agree?

Cheers,

Mike


No. If you don't have "." in a classpath you won't compile this

The answer in the first question is perfectly right!
ansuman mohapatra
Greenhorn

Joined: Mar 04, 2008
Posts: 27
Originally posted by Mike Rolf:
I still think that the above question/answer is wrong however, because javac requires you specify the PATH of the source file (the file to be compiled).

For example;

Directory structure: project/foo/bar/MyClass.java
Current directory: project

To compile MyClass.java the command would be: javac foo/bar/MyClass.java

Regardless of what the classpath was set to.

Agree?

Cheers,

Mike


This is exactly what i think is correct. I didnt understand why you have foo/bar after the semicolon. I mean any guesses why thats required ? You can simply write javac and provide the path of the file to compile


Cleared SCJP.....
 
Consider Paul's rocket mass heater.
 
subject: MasterExam - Wrong Answers?
 
Similar Threads
K&B Master Exam Question 68
K&B SCJP 5 Chapter 10, #10 Exam Error?
classpath for the .java
Possible question eligible for errata
Class path question