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,一经查实,立即删除!

相关文章

Spring源码中的抽象工厂模式

Spring 框架中广泛运用了抽象工厂模式来实现其核心组件的创建与管理。以下是源码分析&#xff1a; 源码分析&#xff1a; 1. BeanFactory 与其实现 org.springframework.beans.factory.BeanFactory 是 Spring 中最基础的工厂接口&#xff0c;它代表了抽象工厂模式中的“抽象…

Spring Boot分段处理List集合多线程批量插入数据

项目场景&#xff1a; 大数据量的List集合&#xff0c;需要把List集合中的数据批量插入数据库中。 解决方案&#xff1a; 拆分list集合后&#xff0c;然后使用多线程批量插入数据库 1.实体类 package com.test.entity;import lombok.Data;Data public class TestEntity {priv…

Python入门:使用Python实现简单的猜数字游戏

Python是一种解释型、交互式、面向对象的编程语言&#xff0c;其设计哲学强调代码的可读性&#xff0c;并允许开发者用少量代码表达想法。在本文中&#xff0c;我们将通过编写一个简单的猜数字游戏来展示Python的基础知识和编程逻辑。 游戏描述 游戏开始时&#xff0c;程序会…

数字化转型过程中所面临的挑战以及应对这些挑战的策略

随着信息技术的快速发展&#xff0c;企业为了提升运营效率、优化资源配置&#xff0c;往往会在不同部门建立各自的信息系统。然而&#xff0c;这些系统的孤立性、功能重叠以及数据不一致性等问题逐渐凸显&#xff0c;导致企业业务流程受阻&#xff0c;难以形成高效的协同作战能…

Spring源码中的简单工厂模式

Spring 源码中广泛运用了各种设计模式,其中包括简单工厂模式。简单工厂模式在 Spring 中主要用于简化对象的创建过程,将对象的创建逻辑集中管理,从而使得客户端代码无需关心具体的对象创建细节,只需与工厂交互就能获取所需的对象实例。这种设计有助于提高代码的可读性、可维…

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

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

prometheus服务端部署

prometheus 安装 下载 # https://prometheus.io/download/wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gztar -zxf prometheus-2.34.0.linux-amd64.tar.gz -C /data0/prometheus-2.34.0sudo adduser prome…

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…

使用了Python语言和Flask框架。创建一个区块链网络,允许用户通过HTTP请求进行交互,如包括创建区块链、挖矿、验证区块链等功能。

目录 大概来说&#xff1a; 二、代码注释 1.添加交易方法&#xff08;add_transaction函数&#xff09; 2.添加新的节点&#xff08;add_node 函数&#xff09; 3、替换链的方法&#xff08;replace_chain函数&#xff09; 总结 大概来说&#xff1a; 定义了一个名为Blo…

1、初识Linux系统 shell 脚本

shell脚本组成介绍 1、shell脚本命名规则 必须以.sh文件结尾&#xff0c;类似.txt文件&#xff1b;例如创建一个test.sh文件 2、linux系统中shell脚本开头&#xff0c;必须以如下代码开头 test.sh内容如下&#xff1a; #!/bin/bash# 这是一个注释echo "Hello, World!&…

西安地区调频广播频率一览

FM89.6 陕西人民广播电台经济广播 FM91.6 陕西人民广播电台交通广播 FM93.1 西安人民广播电台音乐广播 FM95.5 中央人民广播电台音乐之声 FM96.4 中央人民广播电台中国之声 FM98.8 陕西人民广播电台音乐广播 FM101.1 陕西人民广播电台戏曲广播 FM101.8 陕西人民广播电台…

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

在生物制药企业中&#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…

2.微服务技术

微服务技术对比 DubboSpringCloudSpringCloudAlibaba注册中心zookeeper&#xff0c;RedisEureka,ConsulNacos,Eureka服务远程调用Dubbo协议Feign(http协议)Dubbo,Feign配置中心SpringCloudConfigSpringCloudConfig,Nacos服务网关SpringCloudGateway,ZuulSpringCloudGateway,Zu…

css 设置div阴影样式

文章目录 问题描述解决方案代码示例设置阴影样式 问题描述 大家好&#xff01;我是夏小花&#xff0c;今天是2024年4月23日|农历三月十五&#xff0c;今天这篇文章主要以div或者是view添加阴影样式为主题 &#xff0c;详细代码如下 解决方案 这段是vue页面中的 代码&#xff0…

基于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;函数和类的名称都存在于全…

vue-router学习4:嵌套路由

一些应用程序的 UI 由多层嵌套的组件组成。在这种情况下&#xff0c;URL 的片段通常对应于特定的嵌套组件结构。 定义子路由组件 首先&#xff0c;你需要为嵌套路由创建对应的子组件。这些组件将在父路由组件的 <router-view></router-view> 中被渲染。 <!-…