Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic