Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Mock Exam 4, question 18 (Java OCA 8 Programmer I Study Guide)

 
Poyan Gerami
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chapter 4, question 18
in sybextestbanks

option G , main method can call private constructor , should be , main method can call private method ?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poyan Gerami wrote:option G , main method can call private constructor , should be , main method can call private method ?

Well spotted! There is indeed a difference between the statement of option G (which is about private constructor) and the explanation of this mock question (which is about private method). As both are allowed (illustrated in the following code snippets)It's up to the author to decide what will be changed to make option G and explanation match each other.

Hope it helps!
Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel is correct. The explanation and question don't match. Luckily both are true.
 
Ellen Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some difficulties understanding why option G is correct at all. There isn't something special about the main() method is it? If I understand correctly you can access a private constructor from anywhere in the same class, not only the main() method. Furthermore the main() method can as well be in another class (probably will be, because any non-trivial application will have multiple classes), in that case you can't access the private constructor from the main() method.
 
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
Ellen Bee wrote:If I understand correctly you can access a private constructor from anywhere in the same class, not only the main() method.

True! Any private constructor and/or method can be accessed/invoked from any code in the same class. (And that's why option G is correct )

Ellen Bee wrote:Furthermore the main() method can as well be in another class (probably will be, because any non-trivial application will have multiple classes), in that case you can't access the private constructor from the main() method.

If the main() method is defined in ClassX, you can't access a private constructor and/or method of ClassY, even if both classes are defined in the same source code file. As illustrated in this code snippet

Ellen Bee wrote:I have some difficulties understanding why option G is correct at all.

Because it's a true statement As mentioned earlier, you can access a private constructor from the main() method. You could (and probably would) argue that if the main() method is defined in another class you would not be able to access the private constructor. And that's true as well, but
with this reasoning you are completely ignoring the "context of a question". None of the options mentions something like "another class", "two classes",... so that means that every statement is about only one class. And if you only have one class with a main() method, you can access its private constructor (so option G is correct). In this thread you'll find a nice discussion about this "context of a question", it's definitely worth reading.
On the actual exam you know for each question how many correct answers you have to select. That's really helpful! Because you know how many answers to select, so if you have 1 obvious answer but the question states to select 2 you know you need to select another one (which you otherwise probably not would have selected). When I took the OCA7 exam, I even had to select an answer with a compiler error because all other answers were definitely incorrect!

Hope it helps!
Kind regards,
Roel
 
Ellen Bee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:None of the options mentions something like "another class", "two classes",...

While it's true none of the options mention some other class, some options (B,C,D) explicitly mention the same class (although not very clear). For example option C: "this.variableName can be called from any instance method in the class", I assume with "the class" they mean "the class with the field variableName". As option G doesn't say "the main() method in the class", I assumed it was meant in general.
You are right it is really helpful if the questions mention how many answers you have to select. Most of the review questions I got wrong, are those where I selected one answer too many or one too few.
 
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
Ellen Bee wrote:As option G doesn't say "the main() method in the class", I assumed it was meant in general.

You should never assume something which is not mentioned in the question or in the option. If the question wanted to test your knowledge about a private method (or constructor) being accessible in another class, the option would have been: "A private method (or constructor) can be invoked from the main() method in another class."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic