OCP Java17 SE Developers 复习题13

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

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

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

D, F.  There is no such class within the Java API called ParallelStream, so options A and E are incorrect. The method defined in the Stream class to create a parallel stream from an existing stream is parallel(); therefore, option F is correct, and option C is incorrect. The method defined in the Collection class to create a parallel stream from a collection is parallelStream(); therefore, option D is correct, and option B is incorrect.

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

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

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

A, D.  The tryLock() method returns immediately with a value of false if the lock cannot be acquired. Unlike lock(), it does not wait for a lock to become available. This code fails to check the return value on line 8, resulting in the protected code being entered regardless of whether the lock is obtained. In some executions (when tryLock() returns true on every call), the code will complete successfully and print 45 at runtime, making option A correct. On other executions (when tryLock() returns false at least once), the unlock() method on line 10 will throw an IllegalMonitorStateException at runtime, making option D correct. Option B would be possible if line 10 did not throw an exception.

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

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

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

B, C, F.  Runnable returns void and Callable returns a generic type, making options A and D incorrect and option F correct. All methods are capable of throwing unchecked exceptions, so option B is correct. Only Callable is capable of throwing checked exceptions, so option E is incorrect. Both Runnable and Callable are functional interfaces that can be implemented with a lambda expression, so option C is also correct.

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

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

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

B, C.  The code does not compile, so options A and F are incorrect. The first problem is that although a ScheduledExecutorService is created, it is assigned to an ExecutorService. The type of the variable on line w1 would have to be updated to ScheduledExecutorService for the code to compile, making option B correct. The second problem is that scheduleWithFixedDelay() supports only Runnable, not Callable, and any attempt to return a value is invalid in a Runnable lambda expression; therefore, line w2 will also not compile, and option C is correct. The rest of the lines compile without issue, so options D and E are incorrect.

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

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

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

C.  The code compiles and runs without throwing an exception or entering an infinite loop, so options D, E, and F are incorrect. The key here is that the increment operator ++ is not atomic. While the first part of the output will always be 100, the second part is nondeterministic. It may output any value from 1 to 100, because the threads can overwrite each other's work. Therefore, option C is the correct answer, and options A and B are incorrect.

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

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

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

C, E.  The code compiles, so option G is incorrect. The peek() method on a parallel stream will process the elements concurrently, so the order cannot be determined ahead of time, and option C is correct. The forEachOrdered() method will process the elements in the order in which they are stored in the stream, making option E correct. None of the methods sort the elements, so options A and D are incorrect.

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

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

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

D.  Livelock occurs when two or more threads are conceptually blocked forever, although they are each still active and trying to complete their task. A race condition is an undesirable result that occurs when two tasks that should have been completed sequentially are completed at the same time. For these reasons, option D is correct.

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

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

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

B.  Be wary of run() vs. start() on the exam! The method looks like it executes a task concurrently, but it runs synchronously. In each iteration of the forEach() loop, the process waits for the run() method to complete before moving on. For this reason, the code is thread-safe. Since the program consistently prints 500 at runtime, option B is correct. Note that if start() had been used instead of run() (or the stream was parallel), then the output would be indeterminate, and option C would have been correct.

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

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

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

C.  If a task is submitted to a thread executor, and the thread executor does not have any available threads, the call to the task will return immediately with the task being queued internally by the thread executor. For this reason, option C is the correct answer.

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

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

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

A.  The code compiles without issue, so option D is incorrect. The CopyOnWriteArrrayList class is designed to preserve the original list on iteration, so the first loop will be executed exactly three times and, in the process, will increase the size of tigers to six elements. The ConcurrentSkipListSet class allows modifications, and since it enforces the uniqueness of its elements, the value 5 is added only once, leading to a total of four elements in bears. Finally, despite using the elements of lions to populate the collections, tigers and bears are not backed by the original list, so the size of lions is 3 throughout this program. For these reasons, the program prints 3 6 4, and option A is correct.

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

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

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

F.  The code compiles and runs without issue, so options C, D, E, and G are incorrect. There are two important things to notice. First, synchronizing on the first variable doesn't impact the results of the code. Second, sorting on a parallel stream does not mean that findAny() will return the first record. The findAny() method will return the value from the first thread that retrieves a record. Therefore, the output is not guaranteed, and option F is correct. Option A looks correct, but even on serial streams, findAny() is free to select any element.

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

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

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

B.  The code snippet submits three tasks to an ExecutorService, shuts it down, and then waits for the results. The awaitTermination() method waits a specified amount of time for all tasks to complete and the service to finish shutting down. Since each five-second task is still executing, the awaitTermination() method will return with a value of false after two seconds but not throw an exception. For these reasons, option B is correct.

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

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

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

C.  The code does not compile, so options A and E are incorrect. The problem here is that c1 is an Integer and c2 is a String, so the code fails to combine on line q2, since calling length() on an Integer is not allowed, and option C is correct. The rest of the lines compile without issue. Note that calling parallel() on an already parallel stream is allowed, and it may return the same object.

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

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

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

C, E.  The code compiles without issue, so option D is incorrect. Since both tasks are submitted to the same thread executor pool, the order cannot be determined, so options A and B are incorrect, and option C is correct. The key here is that the order in which the resources o1 and o2 are synchronized could result in a deadlock. For example, if the first thread gets a lock on o1 and the second thread gets a lock on o2 before either thread can get their second lock, the code will hang at runtime, making option E correct. The code cannot produce a livelock, since both threads are waiting, so option F is incorrect. Finally, if a deadlock does occur, an exception will not be thrown, so option G is incorrect.

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

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

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

A.  The code compiles and runs without issue, so options C, D, E, and F are incorrect. The collect() operation groups the animals into those that do and do not start with the letter p. Note that there are four animals that do not start with the letter p and three animals that do. The logical complement operator (!) before the startsWith() method means that results are reversed, so the output is 3 4, and option A is correct, making option B incorrect.

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

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

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

A, B.  The code compiles just fine. If the calls to fuel++ are ordered sequentially, then the program will print 100 at runtime, making option B correct. On the other hand, the calls may overwrite each other. The volatile attribute only guarantees memory consistency, not thread-safety, making option A correct and option C incorrect. Option E is also incorrect, as no InterruptedException is thrown by this code. Remember, interrupt() only impacts a thread that is in a WAITING or TIMED_WAITING state. Calling interrupt() on a thread in a NEW or RUNNABLE state has no impact unless the code is running and explicitly checking the isInterrupted() method.

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

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

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

F.  The lock() method will wait indefinitely for a lock, so option A is incorrect. Options B and C are also incorrect, as the correct method name to attempt to acquire a lock is tryLock(). Option D is incorrect, as fairness is set to false by default and must be enabled by using an overloaded constructor. Finally, option E is incorrect because a thread that holds the lock may have called lock() or tryLock() multiple times. A thread needs to call unlock() once for each call to lock() and successful tryLock(). Option F is the correct answer since none of the other options are valid statements.

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

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

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

C, E, G.  A Callable lambda expression takes no values and returns a generic type; therefore, options C, E, and G are correct. Options A and F are incorrect because they both take an input parameter. Option B is incorrect because it does not return a value. Option D is not a valid lambda expression, because it is missing a semicolon at the end of the return statement, which is required when inside braces {}.

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

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

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

E, G.  The application compiles and does not throw an exception. Even though the stream is processed in sequential order, the tasks are submitted to a thread executor, which may complete the tasks in any order. Therefore, the output cannot be determined ahead of time, and option E is correct. Finally, the thread executor is never shut down; therefore, the code will run but never terminate, making option G also correct.

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

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

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

F.  The key to solving this question is to remember that the execute() method returns void, not a Future object. Therefore, line n1 does not compile, and option F is the correct answer. If the submit() method had been used instead of execute(), option C would have been the correct answer, as the output of the submit(Runnable) task is a Future<?> object that can only return null on its get() method.

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

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

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

A, D.  The findFirst() method guarantees the first element in the stream will be returned, whether it is serial or parallel, making options A and D correct. While option B may consistently print 1 at runtime, the behavior of findAny() on a serial stream is not guaranteed, so option B is incorrect. Option C is likewise incorrect, with the output being random at runtime.

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

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

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

B.  The code compiles and runs without issue. The key aspect to notice in the code is that a single-thread executor is used, meaning that no task will be executed concurrently. Therefore, the results are valid and predictable, with 100 100 being the output, and option B is the correct answer. If a thread executor with more threads was used, then the s2++ operations could overwrite each other, making the second value indeterminate at the end of the program. In this case, option C would be the correct answer.

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

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

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

F.  The code compiles without issue, so options B, C, and D are incorrect. The limit on the cyclic barrier is 10, but the stream can generate only up to 9 threads that reach the barrier; therefore, the limit can never be reached, and option F is the correct answer, making options A and E incorrect. Even if the limit(9) statement was changed to limit(10), the program could still hang since the JVM might not allocate 10 threads to the parallel stream.

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

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

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

A, F.  The class compiles without issue, so option A is correct. Since getInstance() is a static method and sellTickets() is an instance method, lines k1 and k4 synchronize on different objects, making option D incorrect. The class is not thread-safe because the addTickets() method is not synchronized, and option E is incorrect. One thread could call sellTickets() while another thread calls addTickets(), possibly resulting in bad data. Finally, option F is correct because the getInstance() method is synchronized. Since the constructor is private, this method is the only way to create an instance of TicketManager outside the class. The first thread to enter the method will set the instance variable, and all other threads will use the existing value. This is a singleton pattern.

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

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

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

C, D.  The code compiles and runs without issue, so options F and G are incorrect. The return type of performCount() is void, so submit() is interpreted as being applied to a Runnable expression. While submit(Runnable) does return a Future<?>, calling get() on it always returns null. For this reason, options A and B are incorrect, and option C is correct. The performCount() method can also throw a runtime exception, which will then be thrown by the get() call as an ExecutionException; therefore, option D is also a correct answer. Finally, it is also possible for our performCount() to hang indefinitely, such as with a deadlock or infinite loop. Luckily, the call to get() includes a timeout value. While each call to Future.get() can wait up to a day for a result, it will eventually finish, so option E is incorrect.

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

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

相关文章

高斯溅射融合之路(一)- webgl渲染3d gaussian splatting

大家好&#xff0c;我是山海鲸的技术负责人。之前已经写了一个GIS融合系列。其实CesiumJS的整合有相当的难度&#xff0c;同时也有很多方面的工作&#xff0c;很难在几篇文章内写完&#xff0c;整个山海鲸团队也是投入了接近两年的时间&#xff0c;才把周边整套工具链进行了完善…

Linux中inode号与日志分析

一.inode号 1.inode表结构 元信息&#xff1a;每个文件的属性信息&#xff0c;比如&#xff1a;文件的大小&#xff0c;时间&#xff0c;类型&#xff0c;权限等&#xff0c;称为文件的元数据(meta data 元信息 ) 元数据是存放在inode&#xff08;index node&#xff09;表中…

Spring Kafka—— KafkaListenerEndpointRegistry 隐式注册分析

由于我想在项目中实现基于 Spring kafka 动态连接 Kafka 服务&#xff0c;指定监听 Topic 并控制消费程序的启动和停止这样一个功能&#xff0c;所以就大概的了解了一下 Spring Kafka 的几个重要的类的概念&#xff0c;内容如下&#xff1a; ConsumerFactory 作用&#xff1a;…

Opencv_2_ 图像色彩空间转换

ColorInvert.h 内容如下&#xff1a; #pragma once #include <opencv.hpp> using namespace std; #include <opencv.hpp> using namespace cv; using namespace std; class ColorInvert{ public : void colorSpaceInvert(Mat&image); }; ColorInvert.cpp…

高效过滤器检漏方法选择指南及关键注意事项一览

在生物制药企业中&#xff0c;高效过滤器&#xff08;HEPA&#xff09;的检漏工作是确保洁净室能够达到并保持设计的洁净级别的关键步骤。这关系到产品的质量和安全&#xff0c;因此必须遵循相关法规标准和操作流程。 关于北京中邦兴业 北京中邦兴业科技有限公司是一家国家高新…

element中file-upload组件的提示‘按delete键可删除’,怎么去掉?

问题描述 element中file-upload组件会出现这种提示‘按delete键可删除’ 解决方案&#xff1a; 这是因为使用file-upload组件时自带的提示会盖住上传的文件名&#xff0c;修改一下自带的样式即可 ::v-deep .el-upload-list__item.is-success.focusing .el-icon-close-tip {d…

基于SpringBoot的宠物领养网站管理系统

基于SpringBootVue的宠物领养网站管理系统的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootMyBatis工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 主页 宠物领养 宠物救助站 宠物论坛 登录界面 管理员界面 摘要 基于Spr…

1.C++入门

目录 1.C关键字 2.命名空间 作用域方面的优化 a.命名空间定义 b.命名空间使用 3.C 输入&输出 1.C关键字 C有63个关键字&#xff0c;C语言有32个关键字&#xff0c;存在重叠如荧光笔标出 2.命名空间 作用域方面的优化 如果变量&#xff0c;函数和类的名称都存在于全…

AI自动生成PPT文档 aippt的API介绍文档

官方链接直达&#xff01; 产品介绍​ 能力介绍​ AiPPT 是一款智能生成演示幻灯片的在线工具。专业设计团队打造海量模板资源&#xff0c;输入标题即可轻松生成完整的PPT。同时 AiPPT 支持导入多格式文档一键生成 PPT&#xff0c;让 PPT 创作更加高效。聚焦于内容&#xff0…

安装zabbix server

目录 1、实验环境 2、yum 安装zabbix server 2.1 解决权限问题和放行流量 2.2 安装zabbix-server 1、实验环境 操作系统rhel8zabbix6.0TLS数据库mysql8.0.30IP地址192.168.81.131时间配置NTP时间服务器同步 2、yum 安装zabbix server 如果通过yum源安装&#xff0c;操作系…

SysetmUI开机是否显示Keyguard的流程

KeyguardViewMediator的onSystemReady方法 没有启用keyguard时KeyguardViewMediator的log&#xff1a; onSystemReady 方法 doKeyguardLocked LockPatternUtils.isLockScreenDisabled 来判断是否启用 public final static String DISABLE_LOCKSCREEN_KEY "lockscreen.…

信息化工作人员必备常识4——ping命令详解【不间断发包自定义发包的大小自定义发包次数】

信息化工作人员必备常识4——ping命令详解【不间断发包&自定义发包的大小&自定义发包次数】 前言回顾pingtelnetnslookup命令 ping 命令详解帮助手册不间断向目标 IP 发送数据包 -t定义发送数据包的大小 -l-t&-l 验证网络承载能力自定义发送数据包的次数统计响应时…

[BT]BUUCTF刷题第20天(4.22)

第20天 Web [GWCTF 2019]我有一个数据库 打开网站发现乱码信息&#xff08;查看其他题解发现显示的是&#xff1a;我有一个数据库&#xff0c;但里面什么也没有~ 不信你找&#xff09; 但也不是明显信息&#xff0c;通过dirsearch扫描得到robots.txt&#xff0c;然后在里面得…

51单片机数字温度报警器_DS18B20可调上下限(仿真+程序+原理图)

数字温度报警器 1 **主要功能&#xff1a;*****\*资料下载链接&#xff08;可点击&#xff09;&#xff1a;\**** 2 **仿真图&#xff1a;**3 **原理图&#xff1a;**4 **设计报告&#xff1a;**5 **程序设计&#xff1a;**主函数外部中断函数DS18B20驱动 6 讲解视频7 **资料清…

上海亚商投顾:沪指震荡调整 油气等周期股集体下挫

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 沪指昨日震荡调整&#xff0c;深成指、创业板指小幅走弱。军工板块逆势拉升&#xff0c;中船应急、捷安高科、…

simlab python二次开发2-一键生成轴瓦并设定节点号

simlab python二次开发2-一键生成轴瓦并设定节点号 1、节点坐标计算并建立1.1、建坐标原点节点&#xff0c;并得到Model-1.gda1.2、轴瓦节点计算并建立 2、由节点建面2.1、由4个节点建面得到3个面单元Body2.2、得到Bodies名称2.3、根据Bodies名称选面特征&#xff08;放入Group…

AR爆发的前夜,Rokid站在了门口

文&#xff5c;刘俊宏 摆脱6寸的手机屏幕&#xff0c;栖居在300寸大屏的智慧生活是什么样子&#xff1f; 4月20日&#xff0c;Rokid在新品AR Lite空间计算套装的发布会上&#xff0c;“硬刚”了苹果的Vision Pro。 Rokid AR Lite空间计算套装 Rokid AR Lite与苹果Vision Pro…

el-upload组件如何上传blob格式的url地址视频

el-upload组件如何上传blob格式的url地址视频 一、存在问题二、直接上代码 需求&#xff1a;想把视频地址url:“blob:http://localhost:8083/65bd3c0f-52ec-4844-b85e-06fdb5095b7b”&#xff0c;通过el-upload组件上传 el-upload是Element UI中用于文件上传的组件&#xff0c;…

中文医疗大模型及中文底座大模型参考

参考&#xff1a;https://github.com/HqWu-HITCS/Awesome-Chinese-LLM 中文底座大模型 中文医疗大模型

c#学习入门1

一、环境配置 颜色主题 字体设置 行号设置 二、第一个应用程序 1. 在解决方案下创建一个新项目 第一种注释&#xff1a;两杠注释 第二种注释&#xff1a;星号注释 第三种注释&#xff1a;三杠注释(只有在花括号后面输出才会自动补全&#xff09; 2.控制台输入打印基础语句 输…