• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP static import question

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a question I missed on a SCJP practice test:

Given two files:



What is the result?

The reason I missed it was I thought these two files were in the same xcom package. If they were, this compiles and runs as User can find the static imports. If they are not, it obviously can not. How many of you would've read the question the same way? I thought with only one package listed at the top, it implied that both files were in the same package. Apparently this is not what the test writers had in mind. What are the conventions you've come across? Thanks.

Brian.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any file that is in a package must have the mandatory package statement at the top. That means the 2nd file can't be in the same package since there is no package statement (its not in any package at all).
 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, it said 2 files.
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In questions such as these you should assume that 2 classes are defined in separate files, and there are no shared code/statements between them. So in this question you can assume that lines 1-6 constitute the contents of the first file and 8-14 strictly the second file. In this instance the import statements marking the start of the second file make the separation a little easier.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geez, I hope that wasn't one of my questions... was it?

In any case, on the real exam (and mock exam creators SHOULD follow this rule although they probably don't):

- when a code listing starts at line 1, assume you're seeing the entire file
- if there are multiple files, the question will either say that, or each file will start at line 1.

So the mock question you showed, if you copied it exactly correctly, doesn't follow standards - the second program should restart at line 1.

hth,

Bert
 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geez, I hope that wasn't one of my questions... was it?

In any case, on the real exam (and mock exam creators SHOULD follow this rule although they probably don't):

- when a code listing starts at line 1, assume you're seeing the entire file
- if there are multiple files, the question will either say that, or each file will start at line 1.

So the mock question you showed, if you copied it exactly correctly, doesn't follow standards - the second program should restart at line 1.



Exactly as described would've been no numbers. The text editor automatically put those in when I used code tags. Exactly would've been the same with the following line,

Given two files:

package xcom;
public class Stuff{

static int MY_CONSTANT = 5;
static int doStuff(int x) { return (x++)*5; }
}

import static xcom.Stuff.*;
import static java.lang.System.out;
class User {
public static void main(String[] args) {
new User().go();
}
void go() { out.println(doStuff(MY_CONSTANT));}

I'm not using code tags now to avoid confusion.

Also the following questions were on the quiz in the CD from your book:

Given:


What's the output?

The given correct answer is:

Created a Father Well, that's all right then.

Wrong.

It would show as two lines give that println was used both times.

And a more substantial example:

Given:

Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all the apply)

return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate== this.code.length()*this.bal*this.rate;

is given as a correct answer.

It's not and the reason given is even worse: Te equals() algorithm must be at least as precise in defining what "meaningfully equivalent" means as the hashCode() method is.

?

One of the basic rules of equals() and hashCode() is that if two objects return true on equals() they must have the same hash code.

Here's a trivially easy example of this problem.


foo.code = "yes";
foo.bal = 2;
foo.rate = 2;

bar.code = "not";
bar.bal = 1;
bar.rate = 4;

Both equals products ==12 so they are "equal" but their hash values are 6 and 3, so, they won't be in the same hash bucket, and they will never be found on a search.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brian, what is the answer for the first question?

When I compile the second file, I get "cannot find symbol" for MY_CONSTANT. I think it is because of default access for this constant?
 
Brian K Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sami Devine wrote:Hi Brian, what is the answer for the first question?

When I compile the second file, I get "cannot find symbol" for MY_CONSTANT. I think it is because of default access for this constant?



Yup. The basic point we went over in the thread, was should I have realized as written that these two files were in different packages. I assumed they were both in the xcom package.

They were not, and was not sure if I was misreading the files, or was the question poorly written. I misread it. It said two files, and without a package statement in both files, I should not have assumed they were both under the same package.

What's your take on the problems I found with the other two practice exam questions?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the both classes are in different package, we cannot access either static method or static variable from one package to another. Its Complie time error.

Please let me know am i correct.
 
Greenhorn
Posts: 13
Mac Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Krish Khan..

both constant and function dostuff are static thats OK ,,,and reason why will not compile here is because of there assess level.. default is package level access... has nothing to do with static
reply
    Bookmark Topic Watch Topic
  • New Topic