File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from Marcus Green's mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question from Marcus Green Watch "A question from Marcus Green New topic
Author

A question from Marcus Green's mock exam

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi there (hope Marcus will read this):
This is from following mock exam:
Java Programmer Certification Mock Exam No 1
Copyright �Marcus Green 2002
Last Updated 17 July 2002
The original question:

Question 58)
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled Base.java
//Base.java
package Base;
class Base{
protected void amethod(){
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
public class Class1 extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}//End of main
}//End of Class1

I have three questions about this question. They are noted at appropriate places below:

Question 58)
You have these files in the same directory. What will happen when you attempt to compile and run Class1.java if you have not already compiled
// How can you have two packages in same dir. The package name has to match the dir name. Rigth?
Base.java
//Base.java
package Base;
public class Base{ // This class will have to be
// public
// so that it can be accessed
// from Class1
// package. Right?
protected void amethod(){
System.out.println("amethod");
}//End of amethod
}//End of class base
package Class1;
//Class1.java
import Base.*; // you have to import Base class
public class Class1 extends Base{
public static void main(String argv[]){
Base b = new Base(); // following line will
// give an error
// because you can
// access protected
// method via an
// object of sub-clas
//b.amethod();
Class1 c = new Class1(); // this will work.
c.amethod();
}//End of main
}//End of Class1

[ September 03, 2002: Message edited by: Barkat Mardhani ]
[ September 03, 2002: Message edited by: Barkat Mardhani ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Barkat, it's Base.java and Class1.java, the files that are in the same directory. A package is represented by a directory.
The question said
You have these files in the same directory.
and you said
How can you have two packages in same dir.

It does not say which directory, so I am going to assume it is .../Class1.
So when Class1.java is compiled the compiler will try to find the file Base.java.
When you compile Class1.java you get:
So it fails to compile.
What was the answer to the original question?
4)Compile error: Superclass Class1.Base of class Class1.Class1 not found
Using the package statement has an effect similar to placing a source file into a different
directory.
Because the files are in different packages they cannot see each other. The stuff about File1 not having been compiled was just to mislead,
java has the equivalent of an "automake", whereby if it was not for the package statements the other file would have been automatically compiled.

That's near enough to what the compiler says.
-Barry
Sorry about editing freakouts.
[ September 03, 2002: 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.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Barry:
But these two files belong to two distinct packagees i.e Base and Class1. And these two packages are in same directory as per punch line of the question...
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Nice question Barkat, package questions do come up on the exam and once you get your head around the concepts they are fairly easy points. You can compile a file with a package direction in a directory that doesn't match the package name but you cannot execute it or see it from another class. Thus taking the package line out of class1 will not help because Base (or Base.Base to be precise) will not be visible, plus amethod is protected anyway. Basically it is very broken code.
Hmm I like this question, I think I'll write some more like it.
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question from Marcus Green's mock exam