Java实践OCAJP混淆答案

标签 java

我正在 https://www.myexamcloud.com 上做一些免费练习考试我在这里得到了一些奇怪的答案。

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

我回答了 false,因为这可能不是真的,但这是错误的答案。现在我不确定他们在这里问我什么:这是一个好的做法,根据 equals-hashCode 契约(Contract)这是必须的,还是每种代码组合都不可能。

  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

这是两个正确答案(我只检查了 b),对我来说这看起来像是矛盾。如果包声明必须在第一行,那么它之前的评论怎么能被允许呢?

  1. Which of the following statements are TRUE?

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

这是什么意思,什么转换?对我来说,转换这个词基本上可以表示任何东西(类型转换、克隆、映射等),但它不是 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

我检查了b,但是a是正确的。如果有可能用协变返回类型覆盖,b 怎么会不正确?

我知道这个网站不是官方网站,这就是我问这个问题的原因。我可以在正式考试中遇到这样的问题吗?我注意到那些考试经常试图愚弄和引诱我,但这里对我来说似乎是错误的,就像问题没有足够的信息/上下文来回答它。

最佳答案

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

答案是正确的。当一个人试图实现一个hashcode() 方法时,这是一个对象的一般契约。取自 api 文档: 如果根据 equals(Object) 方法两个对象相等,则对这两个对象中的每一个调用 hashCode 方法必须产生相同的整数结果。

尝试想象当两个对象的 equal 方法返回 true 但 hashcode 返回不同值时 HashMap 如何处理冲突。

  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

两者都是正确的,因为注释部分不是编译单元的一部分。取自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

这有点含糊。但是,我认为这是真的。因为,如果允许,那么这个表达式也应该运行(它不应该运行)

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

尽管 b 做出了有效的选择。选项 a 比另一个选项更具体。因此,b 也可能意味着被覆盖的方法可能比原始方法更易于访问。

根据我参加考试的经验。问题应该很清楚(意思是说,不应该涉及技巧问题)。所以,我想你可以尝试参加他们的模拟考试。但是,也尝试使用其他模拟考试。还有,读一些其他的书。

祝你好运。

关于Java实践OCAJP混淆答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586651/

相关文章:

java - ObjectMapper - 读/写通用对象

java - 如何计算某个项目在列表中出现的次数

java - Jersey 休息服务未返回 XML 响应

java - 通过可序列化的套接字传输对象

java - 尝试连接到MySQL时出现NullPointerException

Java QVariant 等效项?

java - 通过 ThreadPoolExecutor,可调用代码不会使用多个 CPU

java - 如何将每个 Collection 的项目与另一个 Collection 的项目关联起来?

java - 为什么我得到 null 作为数组的值?

java - 编译java代码时找不到符号