OCP Java17 SE Developers 复习题14

news2024/12/30 1:41:01

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

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

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

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.coloradmin.cn/o/1607497.html

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

相关文章

karpathy build make more --- 2

1 Introduction 用多层神经网络实现更复杂一点名字预测器。 2 方案 采用两层全连接层&#xff0c;中间采用tanh作为激活函数&#xff0c;最后一层用softmax&#xff0c;loss用cross-entropy. 2.1 实施 step1: 生成输入的字符,输入三个字符&#xff0c;输出一个字符. 采用了…

java的spring循环依赖、Bean作用域等深入理解

前言 通过之前的几篇文章将Spring基于XML配置的IOC原理分析完成&#xff0c;但其中还有一些比较重要的细节没有分析总结&#xff0c;比如循环依赖的解决、作用域的实现原理、BeanPostProcessor的执行时机以及SpringBoot零配置实现原理&#xff08;ComponentScan、Import、Impo…

推荐一款websocket接口测试工具

网址&#xff1a;Websocket在线测试-Websocket接口测试-Websocket模拟请求工具 http://www.jsons.cn/websocket/ 很简单输入以ws开后的网址就可以了 这个网址是你后台设置的 如果连接成功会砸提示框内显示相关字样&#xff0c;反之则不行

python爬虫之爬取携程景点评价(5)

一、景点部分评价爬取 【携程攻略】携程旅游攻略,自助游,自驾游,出游,自由行攻略指南 (ctrip.com) import requests from bs4 import BeautifulSoupif __name__ __main__:url https://m.ctrip.com/webapp/you/commentWeb/commentList?seo0&businessId22176&busines…

U.2 NVMe全闪存储阵列在高性能计算环境中的表现

用户利用高性能计算 (HPC) 先进的计算技术来执行大规模的复杂计算任务。这有助于短时间内解决复杂问题&#xff0c;与传统计算方法相比遥遥领先。Infortrend 存储解决方案专门针对密集型 HPC 工作负载进行了优化。新推出的U.2 NVMe全闪存储阵列GS 5024UE在0.3毫秒的延迟下提供1…

镭速助力企业集成OIDC实现安全高效的大文件数据传输

在当今数字化时代&#xff0c;企业尤其是科研机构、研究所和实验室等&#xff0c;对于大量敏感数据的传输安全和效率有着日益增长的需求。面对这一挑战&#xff0c;企业需要一种既能保障数据传输安全&#xff0c;又能提高传输效率的解决方案。镭速&#xff0c;作为一款面向企业…

【C++学习】C++4种类型转换详解

这里写目录标题 &#x1f680;C语言中的类型转换&#x1f680;为什么C需要四种类型转换&#x1f680;C强制类型转换&#x1f680;static_cast&#x1f680;**reinterpret_cast**&#x1f680;const_cast与volatile&#x1f680;dynamic_cast &#x1f680;C语言中的类型转换 在…

buuctf——[ZJCTF 2019]NiZhuanSiWei

buuctf——[ZJCTF 2019]NiZhuanSiWei 1.绕过file_get_contents()函数 file_get_contents函数介绍 定义和用法 file_get_contents() 把整个文件读入一个字符串中。 该函数是用于把文件的内容读入到一个字符串中的首选方法。如果服务器操作系统支持&#xff0c;还会使用内存映射…

【opencv】示例-videocapture_starter.cpp 从视频文件、图像序列或连接到计算机的摄像头中捕获帧...

/** * file videocapture_starter.cpp * brief 一个使用OpenCV的VideoCapture与捕获设备&#xff0c;视频文件或图像序列的入门示例 * 就像CV_PI一样简单&#xff0c;对吧&#xff1f; * * 创建于: 2010年11月23日 * 作者: Ethan Rublee * * 修改于: 2013年4月17日 * …

【笔记】十分钟学会正确的github工作流,和开源作者们使用同一套流程

对视频十分钟学会正确的github工作流&#xff0c;和开源作者们使用同一套流程的记录&#xff0c;方便自己回顾和使用。 注1&#xff1a;一个分支 只有一个人在进行 注2&#xff1a; main和master是不同时期对主分支的命名&#xff0c;两者是同一个东西。如果项目已经有了&#…

将MySQL数据库导入到EA模型的教程

将MySQL数据库导入到EA 1.下载安装mysql-connector-odbc2.在管理工具中新增ODBC数据源3.在EA中新建项目4.链接MYSQL数据源4.1 安装64位的ODBC驱动可能出现”在连接ODBC 时发生错误&#xff0c;请相关检查设置“的提示&#xff0c;卸载后重新安装32位ODBC驱动后可以正常执行 5.导…

【1577】java网吧收费管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 java 网吧收费管理系统是一套完善的java web信息管理系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,Myeclipse8.5开发&#xff0c;数据库为Mysql5.0…

唠一唠,氮化镓和普通快充的不同

充电器,对于手机、电脑、平板等电子产品来说,就是“生命之源”。没有它,这些设备就像鱼儿离开了水。 过去数十年,充电器市场一直在不断变革。从最早的座插式,到后来的一体式、万能充、分离式直充、快充、无线充,再到现在的氮化镓充电器,技术的更新换代快得让人眼花缭乱。 那么…

SQL Server 2022 安装及使用

SQL Server 2022 前言一、安装SQL Server 2022下载SQL Server 2022安装SQL Server 2022配置SQL Server 2022 二、安装SQL Server Management Studio下载SQL Server Management Studio安装SSMS-Setup-CHS 三、使用SQL Server 2022四、解决连接到服务器报错问题 前言 SQL Serve…

Jmeter 性能-内存溢出问题定位分析

1、堆内存溢出 ①稳定性压测一段时间后&#xff0c;Jmeter报错&#xff0c;日志报&#xff1a; java.lang.OutOfMemoryError.Java heap space ②用jmap -histo pid命令dump堆内存使用情况&#xff0c;查看堆内存排名前20个对象。 看是否有自己应用程序的方法&#xff0c;从…

C++Primer3.2 标准类型string

文章目录 初始化string对象读写string对象string的empty和size操作不同string对象的比较string的加法处理string对象的字符遍历string中的每个字符 初始化string对象 //string的初始化 void test01() { string s1; // 默认初始化&am…

YOLOv8改进 | Conv篇 | CVPR2024最新DynamicConv替换下采样(包含C2f创新改进,解决低FLOPs陷阱)

一、本文介绍 本文给大家带来的改进机制是CVPR2024的最新改进机制DynamicConv其是CVPR2024的最新改进机制,这个论文中介绍了一个名为ParameterNet的新型设计原则,它旨在在大规模视觉预训练模型中增加参数数量,同时尽量不增加浮点运算(FLOPs),所以本文的DynamicConv被提出…

AIoT人工智能物联网之deepstream

1.deepstream介绍安装 deepstream是一个很强大的工具集,能够执行数据收集、数据预处理、视频追踪、编码等功能 (1)deepstream docker 版本查询 网页查询 https://catalog.ngc.nvidia.com/containers (2)下载 deepstream docker 对应 版本 https://catalog.ngc.nvidia.c…

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

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

正确解决:关于Lattic Diamond和Radiant License冲突问题(无法破解问题)

一、问题 今天工作&#xff0c;搞16nm Avant E系列FPGA&#xff0c;需要用到莱迪思的Radiant 2023.2软件&#xff08;按这个博主的安装流程Lattice Radiant 2023.1 软件安装教程&#xff09;。 安装好之后&#xff0c;设置环境变量&#xff0c;导入License.dat就是破解不了&…