OCP Java17 SE Developers 复习题14

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

======================== ==========================

======================== ==========================

C.  Since the question asks about putting data into a structured object, the best class would be one that deserializes the data. Therefore, ObjectInputStream is the best choice, which is option C. ObjectWriterBufferedStream, and ObjectReader are not I/O stream classes. ObjectOutputStream is an I/O class but is used to serialize data, not deserialize it. FileReader can be used to read text file data and construct an object, but the question asks what would be the best class to use for binary data.

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

======================== ==========================

======================== ==========================

A, F.  Paths that begin with the root directory are absolute paths, so option A is correct, and option C is incorrect. Option B is incorrect because the path could be a file or directory within the file system. There is no rule that files have to end with a file extension. Option D is incorrect, as it is possible to create a File reference to files and directories that do not exist. Option E is also incorrect. The delete() method returns false if the file or directory cannot be deleted. Character stream classes often include built-in convenience methods for working with String data, so option F is correct. There is no such optimization for multi-threading, making option G incorrect.

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

======================== ==========================

======================== ==========================

B, D.  If the console is unavailable, System.console() will return null, making option D correct and options E and F incorrect. The writer methods throw a checked IOException, making option C incorrect. The code works correctly, prompting for input and printing it. Therefore, option A is incorrect and option B is correct.

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

======================== ==========================

======================== ==========================

F.  The code does not compile, as Files.deleteIfExists() declares the checked IOException that must be handled or declared. Remember, most Files methods declare IOException, especially the ones that modify a file or directory. For this reason, option F is correct. If the method were corrected to declare the appropriate exceptions, option C would be correct. Option B would also be correct if the method were provided a symbolic link that pointed to an empty directory. Options A and E would not print anything, as Files.isDirectory() returns false for both. Finally, option D would throw a DirectoryNotEmptyException at runtime.

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

======================== ==========================

======================== ==========================

C.  The filter() operation applied to a Stream<Path> takes only one parameter, not two, so the code does not compile, and option C is correct. If the code were rewritten to use the Files.find() method with the BiPredicate as input (along with a maxDepth value), the output would be option B, Has Sub, since the directory is given to be empty. For fun, we reversed the expected output of the ternary operation.

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

======================== ==========================

======================== ==========================

C.  The code compiles and runs without issue, so options F and G are incorrect. The key here is that while Eagle is serializable, its parent class, Bird, is not. Therefore, none of the members of Bird will be serialized. Even if you didn't know that, you should know what happens on deserialization. During deserialization, Java calls the constructor of the first non-serializable parent. In this case, the Bird constructor is called, with name being set to Matt, making option C correct. Note that none of the constructors or instance initializers in Eagle are executed as part of deserialization.

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

======================== ==========================

======================== ==========================

B, C.  The code snippet will attempt to create a directory if the target of the symbolic link exists and is a directory. If the directory already exists, though, it will throw an exception. For this reason, option A is incorrect, and option B is correct. It will be created in /mammal/kangaroo/joey and also reachable at /kang/joey because of the symbolic link, making option C correct.

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

======================== ==========================

======================== ==========================

B.  The readAllLines() method returns a List, not a Stream. Therefore, the call to flatMap() is invalid, and option B is correct. If the Files.lines() method were used instead, it would print the contents of the file one capitalized word at a time with the commas removed.

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

======================== ==========================

======================== ==========================

C, E, G.  First, the method does compile, so options A and B are incorrect. Methods to read/write byte[] values exist in the abstract parent of all I/O stream classes. This implementation is not correct, though, as the return value of read(buffer) is not used properly. It will only correctly copy files whose character count is a multiple of 10, making option C correct and option D incorrect. Option E is also correct as the data may not have made it to disk yet. Option F would be correct if the flush() method were called after every write. Finally, option G is correct as the reader stream is never closed.

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

======================== ==========================

======================== ==========================

B, D, G.  Options A and E are incorrect because Path and FileSystem, respectively, are abstract types that should be instantiated using a factory method. Option C is incorrect because the static method in the Path interface is of(), not get(). Option F is incorrect because the static method in the Paths class is get(), not getPath(). Options B and D are correct ways to obtain a Path instance. Option G is also correct, as there is an overloaded static method in Path that takes a URI instead of a String.

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

======================== ==========================

======================== ==========================

A, E.  The code will compile if the correct classes are used, so option G is incorrect. Remember, a try-with-resources statement can use resources declared before the start of the statement. The reference type of wrapper is InputStream, so we need a class that inherits InputStream. We can eliminate BufferedWriterObjectOutputStream, and BufferedReader since their names do not end in InputStream. Next, we see the class must take another stream as input, so we need to choose the remaining streams that are high-level streams. BufferedInputStream is a high-level stream, so option A is correct. Even though the instance is already a BufferedInputStream, there's no rule that it can't be wrapped multiple times by a high-level stream. Option D is incorrect, as FileInputStream operates on a file, not another stream. Finally, option E is correct—an ObjectInputStream is a high-level stream that operates on other streams.

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

======================== ==========================

======================== ==========================

C, E.  The method to create a directory in the Files class is createDirectory(), not mkdir(). For this reason, line 6 does not compile, and option C is correct. In addition, the setTimes() method is available only on BasicFileAttributeView, not the read-only BasicFileAttributes, so line 8 will also not compile, making option E correct.

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

======================== ==========================

======================== ==========================

A, G.  For a class to be serialized, it must implement the Serializable interface and contain instance members that are serializable or marked transient. For these reasons, options A and G are correct and option F is incorrect. Option B is incorrect because even records are required to implement Serializable to be serialized. Option C is incorrect because it describes deserialization. The Serializable interface is a marker interface that does not contain any abstract methods, making option D incorrect. While it is a good practice for a serializable class to include a static serialVersionUID variable, it is not required. Therefore, option E is incorrect as well.

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

======================== ==========================

======================== ==========================

B, D, E.  Path is immutable, so line 23 is ignored. If it were assigned to p1, option A would be correct. Since it is not assigned, the original value is still present, which is option B. Moving on to the second section, the subpath() method on line 27 is applied to the absolute path, which returns the relative path animals/bear. Next, the getName() method is applied to the relative path, and since this is indexed from 0, it returns the relative path bear. Therefore, option D is correct. Finally, remember calling resolve() with an absolute path as a parameter returns the absolute path, so option E is correct.

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

======================== ==========================

======================== ==========================

B, E, F.  Option A does not compile, as there is no File constructor that takes three parameters. Option B is correct and is the proper way to create a File instance with a single String parameter. Option C is incorrect, as there is no constructor that takes a String followed by a File. There is a constructor that takes a File followed by a String, making option E correct. Option D is incorrect because the first parameter is missing a slash (/) to indicate it is an absolute path. Since it's a relative path, it is correct only when the user's current directory is the root directory. Finally, option F is correct as it creates a File from a Path.

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

======================== ==========================

======================== ==========================

A, D.  The method compiles, so option E is incorrect. The method creates a new-zoo.txt file and copies the first line from zoo-data.txt into it, making option A correct. The try-with-resources statement closes all of the declared resources, including the FileWriter o. For this reason, the Writer is closed when the last o.write() is called, resulting in an IOException at runtime and making option D correct. Option F is incorrect because this implementation uses the character stream classes, which inherit from Reader or Writer.

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

======================== ==========================

======================== ==========================

B, C, E.  Options B and C are properties of NIO.2 and are good reasons to use it over the java.io.File class. Option A is incorrect as both APIs can delete only empty directories, not a directory tree. Using a view to read multiple attributes leads to fewer round trips between the process and the file system and better performance, making option E correct. Views can be used to access file system–specific attributes that are not available in Files methods; therefore, option D is correct. Files is part of NIO.2, whereas File is part of java.io, which means option F is incorrect.

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

======================== ==========================

======================== ==========================

C.  Since a Reader may or may not support mark(), we can rule out options E, F, G, and H. Assuming mark() is supported, P is added to the StringBuilder first. Next, the position in the stream is marked before E. The E is added to the StringBuilder, with AC being skipped, and then the O is added to the StringBuilder, with CK being skipped. The stream is then reset() to the position before the E. The call to skip(0) doesn't do anything since there are no characters to skip, so E is added onto the StringBuilder in the next read() call. The value PEOE is printed, and option C is correct.

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

======================== ==========================

======================== ==========================

C.  The code compiles and runs without issue, so option G is incorrect. If you simplify the redundant path symbols, p1 and p2 represent the same path, /lizard/walking.txt. Therefore, isSameFile() returns true. The second output is false, because equals() checks only if the path values are the same, without reducing the path symbols. Finally, mismatch() sees that the contents are the same and returns -1. For these reasons, option C is correct.

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

======================== ==========================

======================== ==========================

D.  The target path of the file after the move() operation is /animals, not /animals/monkey.txt, so options A and B are both incorrect. Both will throw an exception at runtime since /animals already exists and is a directory. Next, the NOFOLLOW_LINKS option means that if the source is a symbolic link, the link itself and not the target will be copied at runtime, so option C is also incorrect. The option ATOMIC_MOVE means that any process monitoring the file system will not see an incomplete file during the move, so option D is correct.

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

======================== ==========================

======================== ==========================

C.  The code compiles and runs without issue, so options D, E, and F are incorrect. The most important thing to notice is that the depth parameter specified as the second argument to find() is 0, meaning the only record that will be searched is the top-level directory. Since we know that the top directory is a directory and not a symbolic link, no other paths will be visited, and nothing will be printed. For these reasons, option C is the correct answer.

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

======================== ==========================

======================== ==========================

G.  The code compiles, so option F is incorrect. To be serializable, a class must implement the Serializable interface, which Zebra does. It must also contain instance members that either are marked transient or are serializable. The instance member stripes is of type Object, which is not serializable. If Object implemented Serializable, all objects would be serializable by default, defeating the purpose of having the Serializable interface. Therefore, the Zebra class is not serializable, with the program throwing an exception at runtime if serialized and making option G correct. If stripes were removed from the class, options A and D would be the correct answers, as name and age are both marked transient.

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

======================== ==========================

======================== ==========================

A, D.  The code compiles without issue, so options E and F are incorrect. The toRealPath() method will simplify the path to /animals and throw an exception if it does not exist, making option D correct. If the path does exist, calling getParent() on it returns the root directory. Walking the root directory with the filter expression will print all .java files in the root directory (along with all .java files in the directory tree), making option A correct. Option B is incorrect because it will skip files and directories that do not end in the .java extension. Option C is also incorrect as Files.walk() does not follow symbolic links by default. Only if the FOLLOW_LINKS option is provided and a cycle is encountered will the exception be thrown.

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

======================== ==========================

======================== ==========================

B.  The method compiles without issue, so option E is incorrect. Option F is also incorrect. Even though /flip exists, createDirectories() does not throw an exception if the path already exists. If createDirectory() were used instead, option F would be correct. Next, the copy() command takes a target that is the path to the new file location, not the directory to be copied into. Therefore, the target path should be /flip/sounds.txt, not /flip. For this reason, options A and C are incorrect. Since the question says the file already exists, the REPLACE_EXISTING option must be specified or an exception will be thrown at runtime, making option B the correct answer.

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

======================== ==========================

======================== ==========================

B, D.  Since you need to read characters, the Reader classes are appropriate. Therefore, you can eliminate options A, C, and F. Additionally, options E and G are incorrect, as they reference classes that do not exist. Options B and D are correct since they read from a file and buffer for performance.

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

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

相关文章

可视化看板有那么多应用场景,该如何快速搭建?可视化工具该如何选择?

在当今的信息化时代&#xff0c;数据已经成为了现代决策的核心。无论是企业战略规划、运营管理&#xff0c;还是个人生活决策&#xff0c;数据都扮演着至关重要的角色。随着数据分析技术和工具的不断进步&#xff0c;数据在决策中的作用将变得更加突出&#xff0c;对组织和个人…

代码随想录算法训练营第三十六天|435. 无重叠区间,763.划分字母区间,56. 合并区间

题目&#xff1a;435. 无重叠区间 给定一个区间的集合 intervals &#xff0c;其中 intervals[i] [starti, endi]。返回需要移除区间的最小数量&#xff0c;使剩余区间互不重叠。 题目链接/讲解链接&#xff1a; https://programmercarl.com/0435.%E6%97%A0%E9%87%8D%E5%8F%A0…

密文字段模糊检索方案

代码地址: https://github.com/zuiyu-main/EncryptDemo https://mp.weixin.qq.com/s/cXOg1tiMtJz2eibDZmXHUQ 在个别特殊领域中&#xff0c;数据的安全问题是非常的重要的&#xff0c;所以需要数据库存储的数据是需要加密存储的。所以也就引申出来本文这个问题&#xff0c;加密…

【每日刷题】Day7

【每日刷题】Day7 &#x1f955;个人主页&#xff1a;开敲&#x1f349; &#x1f525;所属专栏&#xff1a;每日刷题&#x1f34d; &#x1f33c;文章目录&#x1f33c; 1. 206. 反转链表 - 力扣&#xff08;LeetCode&#xff09; 2. 203. 移除链表元素 - 力扣&#xff08;…

重磅!Meta 发布 Llama 3,前所未有的强大功能和多模态能力|TodayAI

Meta今日宣布推出其最新一代尖端开源大型语言模型Llama 3。该模型预计很快将在多个领先的云服务平台上线&#xff0c;包括AWS、Databricks、Google Cloud、Hugging Face、Kaggle、IBM WatsonX、Microsoft Azure、NVIDIA NIM和Snowflake。 Llama 3模型得到了AMD、AWS、Dell、In…

【深度学习】yolov5目标检测学习与调试

2024.4.15 -2024.4.16 完结 0.准备&&补充知识点 yolo检测算法可以实现目标检测、分割和分类任务。 项目仓库地址&#xff1a;https://github.com/ultralytics/yolov5 跟练视频&#xff1a;目标检测 YOLOv5 开源代码项目调试与讲解实战 lux下载视频神器&#xff1a;h…

2W 3KVDC 隔离 稳压单输出 DC/DC 电源模块——TPK-SAR 系列介绍

TPK-SAR系列产品是专门针对PCB上分布式电源系统中需要与输入电源隔离且输出精度要求较高的电源应用场合而设计。该产品适用于&#xff1b;1&#xff09;输入电源的电压变化≤5%&#xff1b;2&#xff09;输入输出之前要求隔离电压≥3000VDC&#xff1b;3&#xff09;对输出电压…

idea新建一个springboot项目

本文分为几个部分&#xff0c; 首先是在idea中新建项目&#xff0c; 然后是配置 项目的目录&#xff08;新建controller、service、dao等&#xff09;&#xff0c; 然后是自定义的一些工具类&#xff08;比如启动后打印地址等&#xff09;。 1.、创建篇 新建项目&#xff0…

IO基础-传统I/O模型

关于IO数据流有两种形式&#xff0c;来源于网络和磁盘分别叫做网络IO、磁盘IO。 客户端通过TCP和UDP协议将数据流发往服务端&#xff0c;服务端接收数据这个过程称为网络IO。 服务端读取本地文件数据到服务中的过程称为磁盘IO。 基于 Linux 一切皆文件的理念&#xff0c;在内…

[大模型]Qwen-7B-Chat WebDemo

Qwen-7B-Chat WebDemo 环境准备 在autodl平台中租一个3090等24G显存的显卡机器&#xff0c;如下图所示镜像选择PyTorch–>2.0.0–>3.8(ubuntu20.04)–>11.8 接下来打开刚刚租用服务器的JupyterLab&#xff0c;并且打开其中的终端开始环境配置、模型下载和运行demo…

AI大模型日报#0419:全球最强开源大模型 Llama 3 发布:15T 数据预训练,参数将超 4000 亿

导读&#xff1a; 欢迎阅读《AI大模型日报》&#xff0c;内容基于Python爬虫和LLM自动生成。目前采用“文心一言”生成了每条资讯的摘要。 标题: 刚刚&#xff0c;全球最强开源大模型 Llama 3 发布&#xff1a;使用 15T 数据预训练&#xff0c;最大模型参数将超 4000 亿 摘要…

【目标跟踪】ByteTrack详解与代码细节

文章目录 一、前言二、代码详解2.1、新起航迹2.2、预测2.3、匹配2.4、结果发布2.5、总结 三、流程图四、部署 一、前言 论文地址&#xff1a;https://arxiv.org/pdf/2110.06864.pdf git地址&#xff1a;https://github.com/ifzhang/ByteTrack ByteTrack 在是在 2021 年 10 月…

书生浦语训练营第2期-第4节笔记

一、为什么要微调&#xff1f; 1. 适应特定任务或领域&#xff1a;虽然预训练的模型通常具有广泛的知识和理解能力&#xff0c;但它们可能不完全适应特定任务的需求。通过在特定的数据集上微调模型&#xff0c;可以使模型更好地理解和处理与特定任务或领域相关的数据。 2. 提高…

【uniapp】微信小程序2024手机号快速验证及无感登录教程(内附代码)

组件&#xff1a;手机号快速验证组件 适用对象&#xff1a;企业/个体 费用&#xff1a;0.03元/次 目录 前言思路前端后端代码无感登录onload事件无感登录方法登录判断后端mini_login2 最后 前言 最近注册了公司&#xff0c;可以注册具有支付能力的小程序了&#xff0c;各种材料…

在Nuxt.js中添加PostCSS自动前缀器

在其他浏览器中&#xff0c;有些 CSS 属性需要带有前缀。如-webkit- | -o- | -ms- 等等 Autoprefixer 是一个 PostCSS 插件&#xff0c;可以将你的CSS代码渲染到浏览器中自动补充厂商前缀&#xff0c;因此你不用担心自己编写的CSS代码有浏览器兼容性问题。 如&#xff1a; .fl…

kaggle咖啡销售分析案例侧重可视化折线图条形图扇形图柱状图

目录 概述 环境依赖 数据描述 代码概述 导包 数据读取 统计缺失值 数据结构概述 描述统计 时间轴数据转换 月交易统计直方图 周交易统计图 小时数据转换 小时折线图 销售关系可视化统计 销售占比扇形图 价格箱线图 各类别多维度条形图统计 商店位置交易量折线…

重磅福利!参与现金红包抽奖活动,赶快行动吧!

文章目录 粉丝福利 粉丝福利 亲爱的朋友们&#xff0c;令人振奋的消息来啦&#xff01;本月&#xff0c;我们特地为大家准备了一份特别的粉丝福利&#xff01;只要您轻轻一点&#xff0c;关注我们的公众号&#xff0c;就有机会抽取现金红包&#xff0c;让您的生活多一份惊喜与喜…

【微信公众平台】扫码登陆

文章目录 前置准备测试号接口配置 带参数二维码登陆获取access token获取Ticket拼装二维码Url编写接口返回二维码接收扫描带参数二维码事件编写登陆轮训接口测试页面 网页授权二维码登陆生成ticket生成授权地址获取QR码静态文件支持编写获取QR码的接口 接收重定向参数轮训登陆接…

游泳耳机哪个牌子好?体验与口碑兼顾的4大游泳耳机汇总!

最近的天气越来越炎热了&#xff0c;许多人选择游泳作为一种既能锻炼身体又能享受清凉的活动。而随着科技的发展&#xff0c;越来越多的运动爱好者希望在游泳时也能享受到音乐的乐趣。因此&#xff0c;游泳耳机应运而生&#xff0c;成为市场上的热门产品。然而&#xff0c;面对…

使用PixVerse使用指定的角色生成视频

PixVerse 是一款可以将文字描述转换为高清视频的AI视频生成工具&#xff0c;它还支持直接生成原神角色的专属动画视频。以下是如何使用PixVerse使用指定的角色生成视频的步骤&#xff1a; 1. 点击PixVerse 网址 访问以下网址&#xff1a;https://app.pixverse.ai/create/vide…