aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes help! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "help!" Watch "help!" New topic
Author

help!

PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
package test1;
public class Test1 {
static int x = 42;
}
package test2;
public class Test2 extends test1.Test1 {
public static void main(String[] args) {
System.out.println(�x = � + x);
}
}

A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.
Answer: C

Why not B? Would you tell me the two packages' relation?
Thanks !
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Peter,
I guess import test1.Test1 statement is needed on line 2.

public class Test2 extends test1.Test1
Here Test1 java class is in different package test1 which is not visible in package test2.
Hope that helps !!!


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by Jay Pawar:
Peter,
I guess import test1.Test1 statement is needed on line 2.

Nope


The class name is fully qualified therefore an import statement would just be redundant.

The correct answer is E.

...
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println(“x = “ + x);
...
E. Compilation fails because of an error in line 4 of class Test2.


The compilation fails because x has "package" (also known as default) access and therefore is not accessible to the Test2 class which is in another package.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
You are correct Louie. I didn't see that test1.Test1
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
I know a little of your thinking and chang it :
package test1;
public class Test1 {
static int x = 42;
}
package test2;
import test1;//I added.
public class Test2 extends test1.Test1 {
public static void main(String[] args) {
System.out.println(�x = � + x);
}
}

I chang it above ,is it right now ?
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
I think the real answer is C and E.
Do you follow me ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
To get this to compile you must give protected or public access to the static variable x:


So the answer to the original question should be:
D. Compilation fails because of an error in line 3 of class Test1.


provided you count from the package statement in the file containing class Test1. Line 3 of class Test1 is where the error (or mistake) is.
[ September 08, 2004: 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.
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
class Test1 {
static int x = 42;
}
public class Test2 extends Test1 {
public static void main(String[] args) {
System.out.println("x = " + x);
}
}
It can complie the answer :
x = 42
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Now everything is in the same (default) package!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: help!
 
Similar Threads
Package level question.
package
Compilation fails
error in result
Mock Exam Qn.(What is the result)