About Question enthuware.ocajp.i.v7.2.915 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
matheuscs
Posts: 5
Joined: Thu Nov 07, 2013 6:06 pm
Contact:

About Question enthuware.ocajp.i.v7.2.915 :

Post by matheuscs »

Here is the question:
Given the following contents of two java source files:

Code: Select all

package util.log4j; 
public class Logger  {   
    public void log(String msg){       
        System.out.println(msg);   
    } 
}
and

Code: Select all

package util; 
public class TestClass {
    public static void main(String[] args) throws Exception {
        Logger logger = new Logger();
        logger.log("hello");
    }
}
What changes, when made independently, will enable the code to compile and run?
The answer
Replace package util.log4j; with
package util;
makes me believe that it will only and simply rename the package, which would cause the Logger class unreachable.

Wouldn't it be good if the answer was changed to say also that the file should be moved from location?

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by admin »

Hi,
Why would the Logger class be unreachable? The Java file can reside at any location irrespective of the pacakge. The class files will automatically go in the right directory if compiled with -d option. Since the question is not asking about the details of compilation, you can assume that right compilation switches are being used.

HTH,
Paul.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by sarakh »

But if we
Replace package util.log4j; with
package util;
then we will have a package called util which has two public methods:
1. public class Logger
2. public class Logger
And I guess there can be only 1 public class in each package. right?

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by admin »

sarakh wrote:But if we
Replace package util.log4j; with
package util;
then we will have a package called util which has two public methods:
1. public class Logger
2. public class Logger
And I guess there can be only 1 public class in each package. right?
1. I not sure which two methods are you talking about. You've quoted the same class twice.
2. No, you can have as many public classes in a package as you want. You cannot have more than one public class in one file because a public class must always be in a file with the same name.

HTH,
Paul.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by sarakh »

Hi Paul,

I meant we will have "public class TestClass" and "public class Logger".
But yes you are right. I was confused with files and packages. Thank you for the explanation.

Sara

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by NickWoodward »

Answer 3:
In this case, you are importing package util
Are we? Where?

Nick

Online
admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by admin »

All the classes of the same package are already imported in a class. But you are right. importing is a poor choice of word here. Fixed.

thanks for your feedback!

NickWoodward
Posts: 29
Joined: Mon Mar 30, 2015 6:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.915 :

Post by NickWoodward »

ah, ok. yeah, i did find it a little confusing!

thanks for the quick reply!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 8 guests