OCP Java17 SE Developers 复习题05

news2024/11/26 13:56:46

======================答案=========================

======================答案=========================

A, E.  Instance and static variables can be marked final, making option A correct. Effectively final means a local variable is not marked final but whose value does not change after it is set, making option B incorrect. Option C is incorrect, as final refers only to the reference to an object, not its contents. Option D is incorrect, as var and final can be used together. Finally, option E is correct: once a primitive is marked final, it cannot be modified.

======================答案=========================

======================答案=========================

B, C.  The keyword void is a return type. Only the access modifier or optional specifiers are allowed before the return type. Option C is correct, creating a method with private access. Option B is also correct, creating a method with package access and the optional specifier final. Since package access does not use a modifier, we get to jump right to final. Option A is incorrect because package access omits the access modifier rather than specifying default. Option D is incorrect because Java is case sensitive. It would have been correct if public were the choice. Option E is incorrect because the method already has a void return type. Option F is incorrect because labels are not allowed for methods.

======================答案=========================

======================答案=========================

A, D.  Options A and D are correct because the optional specifiers are allowed in any order. Options B and C are incorrect because they each have two return types. Options E and F are incorrect because the return type is before the optional specifier and access modifier, respectively.

======================答案=========================

======================答案=========================

A, B, C, E.  The value 6 can be implicitly promoted to any of the primitive types, making options A, C, and E correct. It can also be autoboxed to Integer, making option B correct. It cannot be both promoted and autoboxed, making options D and F incorrect.

======================答案=========================

======================答案=========================

A, C, D.  Options A and C are correct because a void method is optionally allowed to have a return statement as long as it doesn't try to return a value. Option B does not compile because null requires a reference object as the return type. Since int is primitive, it is not a reference object. Option D is correct because it returns an int value. Option E does not compile because it tries to return a double when the return type is int. Since a double cannot be assigned to an int, it cannot be returned as one either. Option F does not compile because no value is actually returned.

======================答案=========================

======================答案=========================

A, B, F.  Options A and B are correct because the single varargs parameter is the last parameter declared. Option F is correct because it doesn't use any varargs parameters. Option C is incorrect because the varargs parameter is not last. Option D is incorrect because two varargs parameters are not allowed in the same method. Option E is incorrect because the  for a varargs must be after the type, not before it.

======================答案=========================

======================答案=========================

D, F.  Option D passes the initial parameter plus two more to turn into a varargs array of size 2. Option F passes the initial parameter plus an array of size 2. Option A does not compile because it does not pass the initial parameter. Option E does not compile because it does not declare an array properly. It should be new boolean[] {true, true}. Option B creates a varargs array of size 0, and option C creates a varargs array of size 1.

======================答案=========================

======================答案=========================

D.  Option D is correct. A common practice is to set all fields to be private and all methods to be public. Option A is incorrect because protected access allows everything that package access allows and additionally allows subclasses access. Option B is incorrect because the class is public. This means that other classes can see the class. However, they cannot call any of the methods or read any of the fields. It is essentially a useless class. Option C is incorrect because package access applies to the whole package. Option E is incorrect because Java has no such wildcard access capability.

======================答案=========================

======================答案=========================

B, C, D, F.  The two classes are in different packages, which means private access and package access will not compile. This causes compiler errors on lines 5, 6, and 7, making options B, C, and D correct answers. Additionally, protected access will not compile since School does not inherit from Classroom. This causes the compiler error on line 9, making option F a correct answer as well.

======================答案=========================

======================答案=========================

B.  Rope runs line 3, setting LENGTH to 5, and then immediately after that runs the static initializer, which sets it to 10. Line 5 in the Chimp class calls the static method normally and prints swing and a space. Line 6 also calls the static method. Java allows calling a static method through an instance variable, although it is not recommended. Line 7 uses the static import on line 2 to reference LENGTH. For these reasons, option B is correct.

======================答案=========================

======================答案=========================

B, E.  Line 10 does not compile because static methods are not allowed to call instance methods. Even though we are calling play() as if it were an instance method and an instance exists, Java knows play() is really a static method and treats it as such. Since this is the only line that does not compile, option B is correct. If line 10 is removed, the code prints swing-swing, making option E correct. It does not throw a NullPointerException on line 17 because play() is a static method. Java looks at the type of the reference for rope2 and translates the call to Rope.play().

======================答案=========================

======================答案=========================

B.  The test for effectively final is if the final modifier can be added to the local variable and the code still compiles. The monkey variable declared on line 11 is not effectively final because it is modified on line 13. The giraffe and name variables declared on lines 13 and 14, respectively, are effectively final and not modified after they are set. The name variable declared on line 17 is not effectively final since it is modified on line 22. Finally, the food variable on line 18 is not effectively final since it is modified on line 20. Since there are two effectively final variables, option B is correct.

======================答案=========================

======================答案=========================

D.  There are two details to notice in this code. First, note that RopeSwing has an instance initializer and not a static initializer. Since RopeSwing is never constructed, the instance initializer does not run. The other detail is that length is static. Changes from any object update this common static variable. The code prints 8, making option D correct.

======================答案=========================

======================答案=========================

E.  If a variable is static final, it must be set exactly once, and it must be in the declaration line or in a static initialization block. Line 4 doesn't compile because bench is not set in either of these locations. Line 15 doesn't compile because final variables are not allowed to be set after that point. Line 11 doesn't compile because name is set twice: once in the declaration and again in the static block. Line 12 doesn't compile because rightRope is set twice as well. Both are in static initialization blocks. Since four lines do not compile, option E is correct.

======================答案=========================

======================答案=========================

B.  The two valid ways to do this are import static java.util.Collections.*; and import static java.util.Collections.sort;. Option A is incorrect because you can do a static import only on static members. Classes such as Collections require a regular import. Option C is nonsense as method parameters have no business in an import. Options D, E, and F try to trick you into reversing the syntax of import static.

======================答案=========================

======================答案=========================

E.  The argument on line 17 is a short. It can be promoted to an int, so print() on line 5 is invoked. The argument on line 18 is a boolean. It can be autoboxed to a Boolean, so print() on line 11 is invoked. The argument on line 19 is a double. It can be autoboxed to a Double, so print() on line 11 is invoked. Therefore, the output is int-Object-Object-, and the correct answer is option E.

======================答案=========================

======================答案=========================

B.  Since Java is pass-by-value and the variable on line 8 never gets reassigned, it stays as 9. In the method squarex starts as 9. The y value becomes 81, and then x gets set to –1. Line 9 does set result to 81. However, we are printing out value, and that is still 9, making option B correct.

======================答案=========================

======================答案=========================

B, D, E.  Since Java is pass-by-value, assigning a new object to a does not change the caller. Calling append() does affect the caller because both the method parameter and the caller have a reference to the same object. Finally, returning a value does pass the reference to the caller for assignment to s3. For these reasons, options B, D, and E are correct.

======================答案=========================

======================答案=========================

B, C, E.  The variable value1 is a final instance variable. It can be set only once: in the variable declaration, an instance initializer, or a constructor. Option A does not compile because the final variable was already set in the declaration. The variable value2 is a static variable. Both instance and static initializers are able to access static variables, making options B and E correct. The variable value3 is an instance variable. Options D and F do not compile because a static initializer does not have access to instance variables.

======================答案=========================

======================答案=========================

A, E.  The 100 parameter is an int and so calls the matching int method, making option A correct. When this method is removed, Java looks for the next most specific constructor. Java prefers autoboxing to varargs, so it chooses the Integer constructor. The 100L parameter is a long. Since it can't be converted into a smaller type, it is autoboxed into a Long, and then the method for Object is called, making option E correct.

======================答案=========================

======================答案=========================

A, E.  The 100 parameter is an int and so calls the matching int method, making option A correct. When this method is removed, Java looks for the next most specific constructor. Java prefers autoboxing to varargs, so it chooses the Integer constructor. The 100L parameter is a long. Since it can't be converted into a smaller type, it is autoboxed into a Long, and then the method for Object is called, making option E correct.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1090178.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

JAVA基础(JAVA SE)学习笔记(一)JAVA学习路线、行业了解、开发环境JDK17安装配置

前言 1. 学习视频: 尚硅谷Java零基础全套视频教程(宋红康2023版,java入门自学必备)_哔哩哔哩_bilibili 2023最新Java学习路线 - 哔哩哔哩 2. 一步一个脚印,踏踏实实再学一遍! 这个世界上有的人输在不能开始,有的人…

IDEA spring-boot项目启动,无法加载或找到启动类问题解决

问题描述:找不到或无法加载主类 xxx.xxx.xxx.Classname 解决方案: 1.检查启动设置: 启动类所在包运行环境(一般选择默认即可)设置完成即可进行运行测试 2.如果第一步没有解决问题,试着第二步&#xff1a…

数据分析基础:数据可视化+数据分析报告

数据分析是指通过对大量数据进行收集、整理、处理和分析,以发现其中的模式、趋势和关联,并从中提取有价值的信息和知识。 数据可视化和数据分析报告是数据分析过程中非常重要的两个环节,它们帮助将数据转化为易于理解和传达的形式&#xff0…

VSCode怎么创建Java项目

首先安装好Java的开发环境:JDK在VSCode中安装适用于Java开发的插件。打开VSCode,点击左侧的扩展图标,搜索并安装Java Extension Pack插件。等待安装完成后,重启VSCode生效。创建一个新的Java项目,按下Ctrl Shift P&a…

[小林coding]4.2TCP重传,滑动窗口,流量控制,拥塞控制_1013

1.重传 1.1 超时重传 两个情况: a 数据包丢失 b ack应答丢失 RTT:网络包往返的时间(不是一个定值) RTO:超时重传的时间间隔(也是一个动态的) RTO设置的时间长:浪费时间资源&…

Open Winding-PMSM-开绕组永磁同步电机基本介绍

文章目录 前言简介Open Widing电机数学模型零序模型 双逆变器调制零序电流抑制基本思路 前言 最近看了些Open Winding永磁同步电机及其控制策略的文献资料,现做个总结。未来的研究方向也大概率围绕Open Winding开展,期待同行交流学习。 简介 开绕组(O…

【通义千问】大模型Qwen GitHub开源工程学习笔记(4)-- 模型的量化与离线部署

摘要: 量化方案基于AutoGPTQ,提供了Int4量化模型,其中包括Qwen-7B-Chat和Qwen-14B-Chat。更新承诺在模型评估效果几乎没有损失的情况下,降低存储要求并提高推理速度。量化是指将模型权重和激活的精度降低以节省存储空间并提高推理速度的过程。AutoGPTQ是一种专有量化工具。…

prosemirror error - Applying a mismatched transaction

bug描述 使用 prosemirror 时,dispatch transcation 报错: 代码如下(简化版): import { inject } from "vue"; const editorView inject("editorView");function handleClick() {const view …

2核4G游戏服务器推荐(阿里云/腾讯云/华为云)

2核4G游戏服务器推荐,首选腾讯云2核4G5M带宽轻量应用服务器218元一年、阿里云2核4G4M带宽轻量应用服务器297元一年,华为云2核2G3M云耀L服务器95元一年,阿腾云来详细说下2核4G游戏服务器推荐配置大全: 目录 2核4G游戏服务器推荐 …

AMD AFMF不但能用在游戏,也适用于视频

近期AMD发布了AMD Software Adrenalin Edition预览版驱动程序,增加了对平滑移动帧(AMD Fluid Motion Frames,AFMF)功能的支持,也就是AMD的“帧生成”技术,与DLSS 3类似,作为FidelityFX Super Re…

学习pytorch13 神经网络-搭建小实战Sequential的使用

神经网络-搭建小实战&Sequential的使用 官网模型结构根据模型结构和数据的输入shape,计算用在模型中的超参数coderunning log网络结构可视化 B站小土堆pytorch视频学习 官网 https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Se…

SSM - Springboot - MyBatis-Plus 全栈体系(二十六)

第六章 SpringBoot 快速启动框架:SpringBoot3 实战 一、SpringBoot3 介绍 1. SpringBoot3 简介 SpringBoot 版本:3.0.5 到目前为止已经学习了多种配置 Spring 程序的方式。但是无论使用 XML、注解、Java 配置类还是他们的混合用法,都会觉…

Qt 输入组控件(Input Widgets) 显示组控件(Display Widgets)详解

一、Qt 输入组控件(Input Widgets) Qt Input Widgets是一组用户界面元素,用于输入和显示文字和数字等的数据。这些小部件可以组成各种不同的表单和对话框,用户可以使用这些小部件与程序交互。 以下是Qt Input Widgets的一些常见小部件&…

vue面试题-应用层

MVC与MVVM MVCMVVM 双向数据绑定 vue2 双向绑定原理 v-model原理 vue3 双向绑定原理 示例 对比 vue2响应式原理和Vue3响应式原理 data为什么是函数?v-if 与 v-show MVC与MVVM MVC和MVVM是两种流行的设计模式,它们都是用于构建动态应用程序的框架。 MVC MVC&#…

Prometheus PromQL及传统部署 Alertmanager 发送告警

文章目录 一.PromQL 简介1.PromQL概念2.时间序列3.Prometheus数据模型4.指标名称及标签使用注意事项5.样本数据格式 二.PromQL 的数据类型1.PromQL 的表达式中支持 4 种数据类型2.时间序列选择器(Time series Selectors)①瞬时向量选择器由两部分组成②定…

关于智能控制领域中模糊控制算法的概述

智能控制领域中的模糊控制算法是一种基于模糊逻辑的控制策略,它通过对模糊集合的刻画来处理模糊信息,从而获得模糊输出并进行控制。模糊控制算法在实际控制工程中具有良好的应用前景,它不但具有较强的鲁棒性和适应性,而且可以为复…

Stable Diffusion 动画animatediff-cli-prompt-travel

基于 sd-webui-animatediff 生成动画或者动态图的基础功能,animatediff-cli-prompt-travel突破了部分限制,能让视频生成的时间更长,并且能加入controlnet和提示词信息控制每个片段,并不像之前 sd-webui-animatediff 的一套关键词控制全部画面。 动图太大传不上来,凑合看每…

数据仓库DW-理论知识储备

数据仓库DW 数据仓库具备 采集数据、分析数据、存储数据的功能,最后得出一些有用的数据,一些目标数据来使用。 采集来自不同源的数据,然后对这些数据进行分析和计算得出一些有用的指标,提供数据决策支持。 数据的来源有&#xff…

[华为杯研究生创新赛 2023] 初赛 REV WP

前言 一年没打比赛了, 差一题进决赛, REV当时lin的第三个challenge没看出来是凯撒, 想得复杂了, 结果错失一次线下机会 >_< T4ee 动态调试, nop掉反调试代码 发现处理过程为 置换sub_412F20处理(这里看其他师傅的wp知道应该是rc4, 我是直接en逆的buf字符串中每一位和…

GPIO子系统(三)

1&#xff0c;简述 GPIO 资源是相对来说较为简单&#xff0c;而且比较通用&#xff08;比如 LED 灯&#xff09;&#xff0c;而 Linux 的 GPIO 驱动属于 Linux Driver 中较为容易上手的部分&#xff0c;但是简单归简单&#xff0c;在 Linux 系统中&#xff0c;要使用 GPIO 资源…