OCP Java17 SE Developers 复习题08

news2024/11/28 8:37:51

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

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

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

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

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

相关文章

TrustZone​之在安全状态之间切换

如果处理器处于NS.EL1,而软件想要转移到S.EL1,应该如何实现呢? 要改变安全状态,无论是向上还是向下,执行都必须经过EL3,如下图所示: 前面的图表显示了在不同安全状态之间移动涉及的步骤的示例序列。逐步进行解释: 进入较高的异常级别需要一个异常。通常,此异常…

网络程序设计

互相连接&#xff0c;发送信息 tcp和udp协议 tcp会有准备&#xff0c;udp不会准备。 8080端口&#xff1a;tomcat端口&#xff0c;java和web相连接 80端口&#xff1a;http 21端口&#xff1a;ftp 套接字 socket&#xff1a;提供给程序可以对外进行连接的接口 ip地址 特…

利用github copilot完成代码,利用正则化完成字符串中信息查找

利用正则化完成字符串中的字符拆解。 下面的代码是实现在“计算机组成原理-计科2101-123456-小明同学.docx”中提取出班级&#xff08;grade&#xff09;&#xff0c;学号&#xff08;id&#xff09;&#xff0c;姓名&#xff08;name&#xff09;。以下的代码都是github copi…

java中Random随机数使用和生成随机数的多个示例

在 Java 中&#xff0c;我们可以使用 java.util.Random 类生成伪随机数。伪随机数的特性是&#xff0c;虽然它们看起来是随机的&#xff0c;但实际上它们是由一个固定的算法生成的。只要我们提供相同的种子&#xff0c;这个算法就会生成相同的数字序列。 首先&#xff0c;我们…

Java链接数据库

本文介绍的是Java链接数据库中的JDBC操作&#xff0c;JDBC虽然现在用的不多&#xff0c;但面试的时候会问道。需要有相应的了解。下面以链接MySQL为例子。 JDBC 什么jdbc Java DataBase Connectivity是一种用于执行SQL语句的Java API&#xff0c;它由一组用Java语言编写的类和…

pod容器内无法访问集群外部主机ipv6地址

一、背景 同事反馈他这边有一环境出现pod容器内无法请求集群外部主机ipv6地址&#xff0c;但是在pod所在集群所主机上是可以请求到外部主机ipv6地址。 二、问题处理过程 首先主机和主机之间ipv6地址能通讯&#xff0c;说明主机之间网络是没啥问题&#xff0c;哪问题就出在容器…

Python语言基础学习大纲(由某大模型生成)

自从上次经丙察察游了一次滇藏线&#xff0c;已有3个没写一篇了。今天利用由某大模型生成的上面这张思维导图&#xff0c;配合这个大模型生成的6000多字拼凑出一篇博文聊以交差。 Python语言概述 一、语言特点 1.语法简单明了 Python的语法简洁易懂&#xff0c;使得编写代码…

邮件群发工具的功能:实用性与高效率功能推荐

市场营销对于每个企业来讲都至关重要&#xff0c;他能为企业带来商机的增长&#xff0c;获得持续的收益。邮件营销作为一种传统但是较少为众多行业使用的营销手段&#xff0c;同样也存在着无限的潜力。 它可以实现&#xff1a; 精准点对点个性化营销。数据报表追踪营销效果。…

如果不小心修改了按钮的名字并且忘记了原名字

出现上述情况&#xff0c;可以右边点击转到代码&#xff0c;注释掉问题行&#xff0c;此页的设计界面就恢复了。

Taro 学习教程 - - - - - 开发环境的安装 helloworld

一、Taro脚手架安装 npm install tarojs/cli -g // or yarn add tarojs/cli -g // or cnpm install tarojs/cli -g1.1 如何判断taro安装成功 taro -v正常安装成功之后显示如图&#xff1a; 1.2 环境变量配置(自行判断是否需要手动配置) 如果遇到如下问题&#xff0c;则是需要…

顶级资源!五个免费图标素材网站

图片太花哨了&#xff0c;纯文本太单调了&#xff1f;别忘了设计师的魔法武器——图标&#xff01;图标材料是UI设计师不可缺少的一部分。优秀的图标设计不仅可以提高界面美感&#xff0c;还可以提高用户的互动体验&#xff0c;帮助用户更好地了解应用程序的功能和信息。在本文…

2024年CSC国际区域问题研究及外语高层次人才培养项目介绍

国家留学基金委&#xff08;CSC&#xff09;公布了2024年国际区域问题研究及外语高层次人才培养项目&#xff0c;申报时间均为3月中下旬。为帮助关注者了解项目申报情况&#xff0c;知识人网小编特整理本文。 近日&#xff0c;国家留学基金委&#xff08;CSC&#xff09;公布了…

P4715 【深基16.例1】淘汰赛-仅思路

首先从题干要求入手&#xff0c;我们可以了解到题目要求是二进一&#xff0c;不难想到这是二叉树的题 再来&#xff0c;从题干可以知道&#xff0c;我们所采用的结构体除了需要有树的两个左右节点指针外&#xff0c;还需要两个变量用来储存“能力值”和“编号” 在这道题中&am…

回归预测 | MATLAB实现CNN-BiLSTM(卷积双向长短期记忆神经网络

效果一览 基本介绍 提出一种同时考虑时间与空间因素的卷积&#xff0d;双向长短期记忆&#xff08; CNN-BiLSTM&#xff09;模型&#xff0c;将具有空间局部特征提取能力的卷积神经网络&#xff08;CNN&#xff09;和具有能同时考虑前后方向长时间信息的双向长短期记忆&#xf…

BL121EN:IEC 61850到OPC UA的即插即用无缝转换解决方案

添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09; 在当今快速发展的工业自动化领域&#xff0c;实现不同通信协议之间的无缝连接是提高系统集成度、数据共享和设备互操作性的关键。钡铼技术&#xff08;Bay-Tech&#xff09;BL121EN硬网关应运而生&#xf…

YOLOv4 学习笔记

文章目录 前言一、YOLOv4贡献和改进二、YOLOv4核心概念三、YOLOv4网络架构四、YOLOv4数据增强五、YOLOv4的损失函数总结 前言 在近年来的目标检测领域&#xff0c;YOLOv4的出现标志着一个重要的技术突破。YOLOv4不仅继承了YOLO系列快速、高效的特点&#xff0c;还引入了一系列…

HarmonyOS开发基础(一)

HarmonyOS开发基础&#xff08;一&#xff09; // &#xff1a;装饰器&#xff1a;用来装饰类结构、方法、变量 Entry // Entry&#xff1a;标记当前组件为入口组件 Component // Component&#xff1a;标记为自定义组件 // struct&#xff1a;自定义组件&#xff0c;可复用的…

羊大师带大家探寻,南北地区冬季饮食的差异

羊大师带大家探寻&#xff0c;南北地区冬季饮食的差异 南北地区的冬季饮食有着明显的不同。随着气温的骤降&#xff0c;人们的餐桌上也逐渐变得丰盛起来。精心准备的美食不仅温暖了身心&#xff0c;更能带来满满的幸福感。接下来&#xff0c;让小编羊大师带大家一起走进南北饮…

一文带你了解Java中synchronized原理

&#x1f308;&#x1f308;&#x1f308;今天给大家分享的是Java中 synchronized 的基本原理 清风的CSDN博客 &#x1f6e9;️&#x1f6e9;️&#x1f6e9;️希望我的文章能对你有所帮助&#xff0c;有不足的地方还请各位看官多多指教&#xff0c;大家一起学习交流&#xff…

Windows(Microsoft)win电脑装Xcode方法

你想在你的Windows电脑上体验和使用苹果的Xcode进行应用打包。遗憾的是&#xff0c;Xcode官方只支持macOS操作系统&#xff0c;但别担心&#xff0c;我们有替代方案可以让你在Windows环境下进行iOS应用的开发和打包。接下来我将指导你如何实现这一目标。 图片来源&#xff1a;W…