OCP Java17 SE Developers 复习题08

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

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

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

A.  This code is correct. Line 8 creates a lambda expression that checks whether the age is less than 5, making option A correct. Since there is only one parameter and it does not specify a type, the parentheses around the parameter are optional. Lines 11 and 13 use the Predicate interface, which declares a test() method.

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

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

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

C.  The interface takes two int parameters. The code on line 7 attempts to use them as if h is a String making option C correct. It is tricky to use types in a lambda when they are implicitly specified. Remember to check the interface for the real type.

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

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

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

A, C.  A functional interface can contain any number of non-abstract methods, including defaultprivatestatic, and private static. For this reason, option A is correct, and option D is incorrect. Option B is incorrect, as classes are never considered functional interfaces. A functional interface contains exactly one abstract method, although methods that have matching signatures as public methods in java.lang.Object do not count toward the single method test. For these reasons, option C is correct. Finally, option E is incorrect. While a functional interface can be marked with the @FunctionalInterface annotation, it is not required.

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

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

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

A, F.  Option B is incorrect because it does not use the return keyword. Options C, D, and E are incorrect because the variable e is already in use from the lambda and cannot be redefined. Additionally, option C is missing the return keyword, and option E is missing the semicolon. Therefore, options A and F are correct.

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

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

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

A, C, E.  Java includes support for three primitive streams, along with numerous functional interfaces to go with them: intdouble, and long. For this reason, options C and E are correct. Additionally, there is a BooleanSupplier functional interface, making option A correct. Java does not include primitive streams or related functional interfaces for other numeric data types, making options B and D incorrect. Option F is incorrect because String is not a primitive but an object. Only primitives have custom suppliers.

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

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

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

A, C.  Predicate<String> takes a parameter list of one parameter using the specified type. Options E and F are incorrect because they specify the wrong type. Options B and D are incorrect because they use the wrong syntax for the arrow operator. This leaves us with options A and C as the answers.

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

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

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

E.  While there appears to have been a variable name shortage when this code was written, it does compile. Lambda variables and method names are allowed to be the same. The x lambda parameter is scoped to within each lambda, so it is allowed to be reused. The type is inferred by the method it calls. The first lambda maps x to a String and the second to a Boolean. Therefore, option E is correct.

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

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

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

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

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

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

E.  The question starts with a UnaryOperator<Integer>, which takes one parameter and returns a value of the same type. Therefore, option E is correct, as UnaryOperator extends Function. Notice that other options don't even compile because they have the wrong number of generic types for the functional interface provided. You should know that a BiFunction<T,U,R> takes three generic arguments, a BinaryOperator<T> takes one generic argument, and a Function<T,R> takes two generic arguments.

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

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

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

A, F.  Option A is correct and option B is incorrect because a Supplier returns a value while a Consumer takes one and acts on it. Option C is tricky. IntSupplier does return an int. However, the option asks about IntegerSupplier, which doesn't exist. Option D is incorrect because a Predicate returns a boolean. It does have a method named test(), making option F correct. Finally, option E is incorrect because Function has an apply() method.

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

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

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

A, B, C.  Since the scope of start and c is within the lambda, the variables can be declared or updated after it without issue, making options A, B, and C correct. Option D is incorrect because setting end prevents it from being effectively final.

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

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

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

D.  The code does not compile because the lambdas are assigned to var. The compiler does not have enough information to determine they are of type Predicate<String>. Therefore, option D is correct.

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

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

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

A.  The a.compose(b) method calls the Function parameter b before the reference Function variable a. In this case, that means that we multiply by 3 before adding 4. This gives a result of 7, making option A correct.

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

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

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

E.  Lambdas are only allowed to reference final or effectively final variables. You can tell the variable j is effectively final because adding a final keyword before it wouldn't introduce a compiler error. Each time the else statement is executed, the variable is redeclared and goes out of scope. Therefore, it is not reassigned. Similarly, length is effectively final. There are no compiler errors, and option E is correct.

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

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

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

B, D.  Option B is a valid functional interface, one that could be assigned to a Consumer<Camel> reference. Notice that the final modifier is permitted on variables in the parameter list. Option D is correct, as the exception is being returned as an object and not thrown. This would be compatible with a BiFunction that included RuntimeException as its return type.

Options A and G are incorrect because they mix format types for the parameters. Option C is invalid because the variable b is used twice. Option E is incorrect, as a return statement is permitted only inside braces ({}). Option F is incorrect because the variable declaration requires a semicolon (;) after it.

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

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

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

A, F.  Option A is a valid lambda expression. While main() is a static method, it can access age since it is using a reference to an instance of Hyena, which is effectively final in this method. Since var is not a reserved word, it may be used for variable names. Option F is also correct, with the lambda variable being a reference to a Hyena object. The variable is processed using deferred execution in the testLaugh() method.

Options B and E are incorrect; since the local variable age is not effectively final, this would lead to a compilation error. Option C would also cause a compilation error, since the expression uses the variable name p, which is already declared within the method. Finally, option D is incorrect, as this is not even a lambda expression.

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

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

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

C.  Lambdas are not allowed to redeclare local variables, making options A and B incorrect. Option D is incorrect because setting end prevents it from being effectively final. Lambdas are only allowed to reference final or effectively final variables. Option C compiles since chars is not used.

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

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

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

C.  Line 8 uses braces around the body. This means the return keyword and semicolon are required. Since the code doesn't compile, option C is the answer

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

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

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

B, F, G.  We can eliminate four choices right away. Options A and C are there to mislead you; these interfaces don't exist. Option D is incorrect because a BiFunction<T,U,R> takes three generic arguments, not two. Option E is incorrect because none of the examples returns a boolean.

The declaration on line 6 doesn't take any parameters, and it returns a String, so a Supplier<String> can fill in the blank, making option F correct. The declaration on line 7 requires you to recognize that Consumer and Function, along with their binary equivalents, have an andThen() method. This makes option B correct. Finally, line 8 takes a single parameter, and it returns the same type, which is a UnaryOperator. Since the types are the same, only one generic parameter is needed, making option G correct.

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

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

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

F.  While there is a lot in this question trying to confuse you, note that there are no options about the code not compiling. This allows you to focus on the lambdas and method references. Option A is incorrect because a Consumer requires one parameter. Options B and C are close. The syntax for a lambda is correct. However, s is already defined as a local variable, and therefore the lambda can't redefine it. Options D and E use incorrect syntax for a method reference. Option F is correct.

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

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

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

E.  Option A does not compile because the second statement within the block is missing a semicolon (;) at the end. Option B is an invalid lambda expression because t is defined twice: in the parameter list and within the lambda expression. Options C and D are both missing a return statement and semicolon. Options E and F are both valid lambda expressions, although only option E matches the behavior of the Sloth class. In particular, option F only prints Sleep:, not Sleep: 10.0.

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

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

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

A, E, F.  A valid functional interface is one that contains a single abstract method, excluding any public methods that are already defined in the java.lang.Object class. Transport and Boat are valid functional interfaces, as they each contain a single abstract method: go() and hashCode(String), respectively. This gives us options A and E. Since the other methods are part of Object, they do not count as abstract methods. Train is also a functional interface since it extends Transport and does not define any additional abstract methods. This adds option F as the final correct answer.

Car is not a functional interface because it is an abstract class. Locomotive is not a functional interface because it includes two abstract methods, one of which is inherited. Finally, Spaceship is not a valid interface, let alone a functional interface, because a default method must provide a body. A quick way to test whether an interface is a functional interface is to apply the @FunctionalInterface annotation and check if the code still compiles.

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

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

相关文章

h5如何跳转到微信公众号中

需求&#xff1a;点击按钮或者某个地方&#xff0c;弹出关注公众号的页面。&#xff08;h5跳转到微信公众号&#xff09; 需要什么&#xff1a;通过window.location.href来进行跳转。 因此&#xff0c;需要一个微信公众号链接。 一、打开想要跳转到的微信公众号的任何一篇文章&…

单片机_RTOS_架构

一. RTOS的概念 // 经典单片机程序 void main() {while (1){喂一口饭();回一个信息();} } ------------------------------------------------------ // RTOS程序 喂饭() {while (1){喂一口饭();} }回信息() {while (1){回一个信息();} }void main() {create_task(喂饭);cr…

一网打尽损失函数和正则化的关系,在损失函数中加入正则化有什么用,如何加入,这里为大家用通俗易懂的例子进行详细解释!(系列1)

文章目录 一、BP神经网络预测中&#xff0c;常见的损失函数是均方误差损失函数MSE二、L2正则化的公式如下&#xff1a;三、 结合MSE和L2正则化的总损失函数公式如下&#xff1a;总结 一、BP神经网络预测中&#xff0c;常见的损失函数是均方误差损失函数MSE 在BP神经网络预测中…

气膜厂家怎样确保产品质量和售后服务?

气膜厂家作为一家专业生产气膜产品的企业&#xff0c;确保产品质量和提供良好的售后服务是我们的责任和使命。为了确保产品质量和售后服务的可靠性&#xff0c;我们采取了以下措施。 起初&#xff0c;我们严格按照国家标准和相关行业规范进行生产。气膜产品的质量是产品能否长…

【JavaEE进阶】 Spring 的创建和使⽤

文章目录 &#x1f334;前言&#x1f38b;创建 Spring 项⽬&#x1f6a9;创建⼀个 Maven 项⽬&#x1f6a9;添加 Spring 框架⽀持&#x1f6a9;添加启动类 &#x1f333;存储 Bean 对象&#x1f6a9;创建Bean&#x1f6a9;将 Bean 注册到容器 &#x1f332;获取并使⽤ Bean 对象…

图解系列--Web服务器,Http首部

1.用单台虚拟主机实现多个域名 HTTP/1.1 规范允许一台 HTTP 服务器搭建多个 Web 站点。。比如&#xff0c;提供 Web 托管服务&#xff08;Web Hosting Service&#xff09;的供应商&#xff0c;可以用一台服务器为多位客户服务&#xff0c;也可以以每位客户持有的域名运行各自不…

VSCode之C++ CUDA入门:reduce的N+1重境界

背景 Reduce是几乎所有多线程技术的基础和关键&#xff0c;同样也是诸如深度学习等领域的核心&#xff0c;简单如卷积运算&#xff0c;复杂如梯度聚合、分布式训练等&#xff0c;了解CUDA实现reduce&#xff0c;以及优化reduce是理解CUDA软硬件连接点的很好切入点。 硬件环境&…

模式识别与机器学习(七):集成学习

集成学习 1.概念1.1 类型1.2 集成策略1.3 优势 2. 代码实例2.1boosting2.2 bagging2.3 集成 1.概念 集成学习是一种机器学习方法&#xff0c;旨在通过组合多个个体学习器的预测结果来提高整体的预测性能。它通过将多个弱学习器&#xff08;个体学习器&#xff09;组合成一个强学…

空间金字塔池化(SPP,Spatial Pyramid Pooling)系列

空间金字塔池化的作用是解决输入图片大小不一造成的缺陷&#xff0c;同时在目标识别中增加了精度。空间金字塔池化可以使得任意大小的特征图都能够转换成固定大小的特征向量&#xff0c;下面针对一些典型的空间金字塔进行盘点。 部分图片来自blog:空间金字塔池化改进 SPP / SP…

Jmeter接口自动化测试 —— Jmeter断言之Json断言

&#x1f4e2;专注于分享软件测试干货内容&#xff0c;欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01;&#x1f4e2;交流讨论&#xff1a;欢迎加入我们一起学习&#xff01;&#x1f4e2;资源分享&#xff1a;耗时200小时精选的「软件测试」资…

PHP:js中怎么使用PHP变量,php变量为数组时的处理

方法一&#xff1a;使用内嵌 PHP 脚本标记 1、简单的拼接 使用内嵌的 PHP 脚本标记 <?php ?> 将 PHP 变量 $phpVariable 的值嵌入到 JavaScript 代码中。 <?php $phpVariable "Hello, World!"; ?><script> // 将 PHP 变量的值传递给 JavaS…

软著项目推荐 深度学习图像风格迁移 - opencv python

文章目录 0 前言1 VGG网络2 风格迁移3 内容损失4 风格损失5 主代码实现6 迁移模型实现7 效果展示8 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 深度学习图像风格迁移 - opencv python 该项目较为新颖&#xff0c;适合作为竞赛课题…

游戏:火星孤征 - deliver us mars - 美图秀秀~~

今天水一篇&#xff0c;借着免费周下载了deliver us mars&#xff0c;玩下来截了好多图&#xff0c;就放这里了。 游戏没有难度&#xff0c;剧情也不难理解&#xff0c;美图到处都是&#xff0c;建模细节也是满满&#xff0c;值得一玩。 游戏中的 A.S.E是守卫飞行机器人&…

在Vivado 仿真器中搭建UVM验证环境(不需要联合modelsim)

Vivado 集成设计环境支持将通用验证方法学 (UVM) 应用于 Vivado 仿真器。Vivado 提供了预编译的 UVM V1.2 库。 &#xff08;1&#xff09;在 Vivado 2019.2 中创建新 RTL 工程。 &#xff08;2&#xff09;单击“添加目录 (Add Directories)”以将“src”和“verif”目录添加…

揭秘Etsy店群模式:Etsy多账户关联如何解决?如何创建多个账户?

Etsy是美国一个在线销售原创手工工艺品的网站&#xff0c;以手工艺成品买卖为主要特色 &#xff0c;曾被纽约时报拿来和eBay&#xff0c;Amazon和“祖母的地下室收藏”比较。近年来作为热门的电商平台吸引了大量跨境玩家是从事电子商务的人熟悉的平台。但它有一个很大的限制&am…

Python推导式详细讲解

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com 在Python中&#xff0c;推导式是一种简洁而强大的语法特性&#xff0c;可以用来创建列表、集合、字典等数据结构。本文将深入探讨Python中的三种推导式&#xff1a;列表推导式、集合推导式和字典推导式&#xff…

Open3D 最小二乘拟合二维直线(直接求解法)

目录 一、算法原理二、代码实现三、结果展示本文由CSDN点云侠原创,原文链接。爬虫网站自重。 一、算法原理 平面直线的表达式为: y = k x + b

Python-炸弹人【附完整源码】

炸弹人 炸弹人是童年的一款经典电子游戏&#xff0c;玩家控制一个类似"炸弹人"的角色&#xff0c;这个角色可以放置炸弹&#xff0c;并在指定的时间内引爆它们消灭敌人以达到目标&#xff0c;此游戏共设有两节关卡&#xff0c;代码如下&#xff1a; 运行效果&#x…

二叉搜索树——模拟

对于一个无穷的满二叉排序树&#xff08;如图&#xff09;&#xff0c;节点的编号是1,2,3&#xff0c;…。对于一棵树根为X的子树&#xff0c;沿着左节点一直往下到最后一层&#xff0c;可以获得该子树编号最小的节点&#xff1b;沿着右节点一直往下到最后一层&#xff0c;可以…

『亚马逊云科技产品测评』活动征文| 基于etcd实现服务发现

提示&#xff1a;授权声明&#xff1a;本篇文章授权活动官方亚马逊云科技文章转发、改写权&#xff0c;包括不限于在 Developer Centre, 知乎&#xff0c;自媒体平台&#xff0c;第三方开发者媒体等亚马逊云科技官方渠道 背景 etcd 是一个分布式 Key-Value 存储系统&#xff0…