• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incomplete code sample for question 22 in page 227 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Lucas Godoy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that question 22 for Chapter 4: Methods and Encapsulation could be incomplete.

I don't know if it's OK to post the question and its code, but for that code code to compile, the classes must be in the same package, since the static field "result" has a default access modifier and cannot be accessed from a class in a different package. The question should state that both classes are in the same package, otherwise, the correct answer is: G. The code does not compile.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucas Godoy,

First of all, a warm welcome to CodeRanch!

Lucas Godoy wrote:I don't know if it's OK to post the question and its code

If you QuoteYourSources (which you clearly did), it's generally not a problem to share the question and the (appropriate parts of the) code snippet.

Lucas Godoy wrote:but for that code code to compile, the classes must be in the same package, since the static field "result" has a default access modifier and cannot be accessed from a class in a different package. The question should state that both classes are in the same package.

I don't agree with you! There is nothing wrong with this question. And I can think of several reasons. The most obvious one (and one you should definitely know of for the actual exam): both classes are already in the same package, the default package. The package statement is not required in a class. If you omit the package statement (like in both code snippets, starting from line1), the class resides in the default package. Both classes omit this package statement, so they are both in the default package and therefore the code will compile and no explicit statement is required.

Hope it helps!
Kind regards,
Roel
 
Lucas Godoy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, my bad!
I will quote a sentence from the book to memorize for the exam:
"On the exam, you’ll see the default package used a lot to save space in code listings."
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas Godoy wrote:sorry, my bad!

No need to apologize! It's much better to post your doubts if you are confused by something than learn something wrong or make incorrect assumptions. And as the moose tells you, CodeRanch is a friendly place for programming greenhorns, so we are glad to help you

Another reason why the question is not incomplete: based on the code and the possible answers, this question is clearly testing your knowledge about execution flow of static and instance initializer blocks, not about access modifiers. So based on the "context of this question" you can assume that both classes are defined in the same package. The actual exam tells you how many answers are correct. If answers are borderline correct, choosing the best answer becomes an important skill. And then the "context of a question" could be (very) important. In this thread you'll find a nice discussion about this "context of a question", it's definitely worth reading.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic