Shadov
Shadov

Reputation: 5592

Java practice OCAJP confusing answers

I'm doing some free practice exams on https://www.myexamcloud.com and I'm getting some weird answers here.

  1. When the objects are equal by equals(Object) method then its hash codes also must be equal

I answered false, because there is possibility of that not being true, but it was wrong answer. Now I'm not sure what they are asking me here: if this is a good practice, if this is a must according to equals-hashCode contract or if there is simply no possibility with every code combination.

  1. Which of the following sentences are TRUE about Java source files?

    a. package statement must be the first line

    b. comments can be placed before package statements

Those are the two correct answers (I checked b only), to me it just looks like a contradiction. If package statement must be the first line then how can comments before it be allowed?

  1. Which of the following statements are TRUE?

    a. Implicit casting is not permitted in all kinds of transformations

What do they mean by this, what transformations? To me transformation is a word that can mean basically anything (casting, cloning, mapping etc.) and it's not a special word in Java.

  1. Which of the following statements are TRUE?

    a. A valid override has identical argument types and order, identical return type, and is not less accessible than the original method

    b. A valid override has identical argument types and order. Return type can be different

I checked b, but a is correct. How is b not correct if there is a possibility of overriding with covariant return type?

I understand that this site is not official, that's why I ask this question. Can I expect such questions on official exam? I noticed that those exams are trying to fool and bait me a lot, but here it simply seems wrong to me, like questions have not enough information/context to answer it.

Upvotes: 1

Views: 122

Answers (2)

Alex Savitsky
Alex Savitsky

Reputation: 2371

1- Most likely the question has referred to the best practices expected contract of implementing equals/hashCode. It's not prohibited by the compiler to make them return different results, though - but I've yet to see a valid use case for such behavior, and, as such, would question it any time I see it. Presumably the exam tries to enforce this best practice expected contract as a rule

2- What they most likely meant here is that the package statement must be the first processed line (as comments are ignored by the compiler) - i.e., you can't place import statements before the package statement, for example. So no contradiction here, just an unclear wording. And yes, you can expect the same lack of clarity in the exam, so be on a lookout.

3- Hard to make out what they meant without the context. Maybe include the other answer choices as well? Transformations can indeed refer to various actions in Java

4- The question here is not whether the presented choice is the only valid one. Answer a presents a narrower set than the possible valid overrides (as yes, the return type can be different, as long as it's co-variant); however, this wording also explicitly cuts off any invalid override implementations, while b does not (note the lack of the not less accessible clause). So a will always be correct, even though it doesn't cover all valid cases; b, on the other hand, covers valid as well as invalid cases, and thus can possibly be incorrect at times. Again, do expect such questions on the actual exam, where you will have to read very carefully

Upvotes: 3

kucing_terbang
kucing_terbang

Reputation: 5131

  1. When the objects are equal by equals(Object) method then its hash codes also must be equal

The answer is true. This is a general contract of an object when one trying to implement a hashcode() method. Taken from api doc: If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

Try imagine on how HashMap handle collision when the equal method return true but the hashcode return different value for two objects.

  1. Which of the following sentences are TRUE about Java source files?
    • a. package statement must be the first line
    • b. comments can be placed before package statements

Both are correct because the comments section are not a part of compilation unit. Taken from JLS

CompilationUnit:

OrdinaryCompilationUnit
ModularCompilationUnit

OrdinaryCompilationUnit:

[PackageDeclaration] {ImportDeclaration} {TypeDeclaration}

ModularCompilationUnit:

{ImportDeclaration} ModuleDeclaration

  1. Which of the following statements are TRUE? a. Implicit casting is not permitted in all kinds of transformations

This is kinda a bit vague. But, I would assume this is true. As, if it is permitted, then this expression should also run (it should not run though)

long a = 123L;
int b = a;
  1. Which of the following statements are TRUE? a. A valid override has identical argument types and order, identical return type, and is not less accessible than the original method b. A valid override has identical argument types and order. Return type can be different

Though b make a valid choice. Option a is more specific than the other option. Thus, the b could also imply that, the overridden method, could be more accessible than the original method.

Through my experience taking the exam. The question should be very clear (means that, there should be no trick question involved). So, I guess you could try to take the mock exam from them. But, try to also use other mock exam. And also, read some other book.

Best of luck.

Upvotes: 3

Related Questions