aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion Question 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 "Confusion Question" Watch "Confusion Question" New topic
Author

Confusion Question

Venkat01
Greenhorn

Joined: Jan 09, 2000
Posts: 24
given these two source files
F1.java
Package forms;
Public class F1{ int type;}
F2.java
Package forms;
Class F2{
Int gettype(F1 ques)
{return ques.type;}
}
1. both compile successfully
2. Neither compile successfully
3. Only F1 compiles
4. Only F2 compiles
Totally confused with the question.
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I don't mean to be picky, but nothing will compile. Java is case sensitive. Who knows if, in the exam, you may get a question that depends on you knowing just that?
Can you repost with the case corrected?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20729
    ∞

Where did the question come from?
Tony's right. If that's the question verbatim, then neither will compile cuz there is no keyword "Package" although there is a keyword "package".
If we ignored case, I would say the answer is 3 because of the statement "return ques.type;"


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ignoring case (which applies to "Class", "Public" and "Int" as well as "Package"), then the correct answer is 1 - both compile successfully. (Yes I did it myself to be sure.) There's nothing wrong with the line "return ques.type;" - ques is declared as an instance of F1, and type is a member variable of class F1 which has package access. Since both classes are in the same package, this is fine. It's true that we often see only private variables (or public static final), but that isn't always the case.

"I'm not back." - Bill Harding, Twister
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20729
    ∞

Doh!
Yer right. I just didn't see it.
Once again the compiler shows the proof.
Siddhu
Ranch Hand

Joined: Oct 02, 1999
Posts: 31
Able to compile F1.java
Unable to compile F2.java
Got the compilation error as:
F2.java:3: Class forms.F1 not found.
int gettype(F1 ques) {
F2.java:4: Class forms.F1 not found in int gettype(forms.F1).
return ques.type;
2 errors.
Any help?
F1.java
package forms;
class F1{
int type;
}
<BOLD>F2.java<BOLD>
package forms;
class F2 {
int gettype(F1 ques) {
return ques.type;
}
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Siddhu- the problem is probably the form of the javac command you use. There are several ways to set it up, but try this:
<code>javac -d . F1.java F2.java</code>
The "-d ." tells the compiler to put the class files in appropriate subdirectories for the package they're in, which in turn allows the compiler to more easily locate the classes it needs later when a different file tries to access them. You need to compile F1 before (or simultaneously with) F2, or you need to have the .java files in package-specific subdirectories as well (again, so the compiler can find them).
Venkat01
Greenhorn

Joined: Jan 09, 2000
Posts: 24
Thanks every body for the reply and sorry for the typo. I promise to be more careful in future. Gentlemen, please understand that this is not deliberate.
Again thanks JIM, you have been wonderful. One small point, have you stopped answering posts in javacert.com. if so then there is no need to go there.
Byeeeee
Siddhu
Ranch Hand

Joined: Oct 02, 1999
Posts: 31
Got a compilation error as when it tried to compile the below code.
Class1.java:2: Superclass Class1.Base of class Class1.Class1 not found.
public class Class1 extends Base {
I tried to compile using javac -d . Base.java Class1.java
<BOLD>
Base.java
package Base;
class Base {
protected void amethod() {
System.out.println("amethod");
}
}
Class1.java
package Class1;
public class Class1 extends Base {
public static void main(String[] args) {
Base b = new Base();
b.amethod();
}
}
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
You need to specify the package that the Base class belongs to:
<PRE>public class Class1 extends Base.Base</PRE>
The classes are not in the same package.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
...or you need to add the line
import Base.*; // (or Base.Base)
to the file Class1.java
Venkat01 - Thanks. I'm not yet planning to stop posting in javacert entirely, but I am planning to shift most of my posting here. Someone's got to look after Tony after all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion Question