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.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【上海大学《面向对象程序设计A》课程小项目报告】抽象向量类模板及其派生类

1 项目内容及要求 本项目通过设计一个抽象向量类模板&#xff0c;以及一个通用的向量类模板和一个字符串类作为其派生类&#xff0c;以满足各种应用场景中的数据存储和处理需求。 项目内容&#xff1a; 抽象向量类模板。派生向量类。派生字符串类。测试及异常处理。联合测试…

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

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

SpringBoot+SSM项目实战 苍穹外卖(3)

继续上一节的内容&#xff0c;本节完成菜品管理功能&#xff0c;包括公共字段自动填充、新增菜品、菜品分页查询、删除菜品、修改菜品。 目录 公共字段自动填充新增菜品文件上传实现新增菜品实现 useGeneratedKeys 菜品分页查询删除菜品修改菜品根据id查询菜品实现修改菜品实现…

【unity3D】Transform组件(如何访问和获取Transform组件)

&#x1f497; 未来的游戏开发程序媛&#xff0c;现在的努力学习菜鸡 &#x1f4a6;本专栏是我关于游戏开发的学习笔记 &#x1f236;本篇是unity的Transform组件 Transform组件 基础知识介绍三个成员变量常用属性扩展 Transform的相关查找方法静态方法 基础知识 介绍 在Unit…

在线网页视频提取工具哪个好用?建议收藏!

随着短视频的崛起&#xff0c;很多人都喜欢将视频下载到手机中慢慢观看&#xff0c;这样可以避免在线播放的卡顿问题&#xff0c;但是会遇到一个问题就是在线网页视频提取工具哪个好用&#xff0c;有的可以提取但是画质太差模糊&#xff0c;有的自带水印飞来飞去。今天小编给大…

【AXI死锁】

单主机单从机死锁 AXI4没有WID,所以比较严格,即写数据通道的数据必须严格的按照写地址通道的数据顺序传送,比如AW通道发送ADDR0,ADDR1,ADDR2三笔写操作,每个写操作burst length=2,那么W通道的顺序在AXI4协议的规定下必须为:WDATA0_0,WDATA0_1,WDATA1_0,WDATA1_1,WDATA2_0…

【3】PyQt文本和图片

1. 文本控件 文本控件是QLabel from PyQt5.QtWidgets import QWidget, QApplication, QLabel import sys# 1.创建应用程序 app QApplication(sys.argv)# 2.创建窗口 w QWidget()# 修改窗口标题 w.setWindowTitle(文本展示)# ---------------------------------------------…

从0开始学Spring、Springboot总结笔记(持续更新中~)

文章目录 一.基于SpringBoot进行Web开发入门1.IDEA编译器中创建springboot工程扩展&#xff1a;如何解决pom.xml文件中“找不到Maven插件”的问题&#xff1f; 2.Springboot项目如何编写请求类和请求方法并启动访问编写请求类和请求方法启动Springboot访问 一些学习资源参考 一…

超完整的mysql安装配置方法(包含idea和navicat连接mysql,并实现建表)

mysql安装配置方法 1、下载mysql2、解压到指定的安装目录3、配置初始化文件my.ini4、配置用户变量和系统变量5、初始化mysql6、安装mysql服务并启动修改密码7、使用idea连接mysql8、使用Navicat可视化工具连接mysql&#xff0c;并实现新建数据库&#xff0c;新建表 1、下载mysq…

pta模拟题(C语言7-26 整除光棍、7-27 稳赢、7-28 查验身份证、7-29 出生年、7-30 点赞)

7-26 整除光棍 这里所谓的“光棍”&#xff0c;并不是指单身汪啦~ 说的是全部由1组成的数字&#xff0c;比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如&#xff0c;111111就可以被13整除。 现在&#xff0c;你的程序要读入一个整数x&#xff0…

CentOS增加虚拟内存 (Linux增加内存)

前言 因为囊中羞涩不敢言&#xff0c;所以内存只有2G&#xff0c;项目在运行的时候&#xff0c;占用的内存已经报表&#xff0c;所以有的时候就会出现宕机的情况发生&#xff0c;后面发现可以通过使用增加虚拟内存空间&#xff0c;来增加内存容量。 下面进入正题&#xff0c;讲…

APOLLO自动驾驶技术沙龙:未来已来,共创智能交通新时代

在这次Apollo会议上&#xff0c;我深刻地感受到了人工智能自动驾驶技术领域的最新进展和未来趋势。作为一名从事软件开发工作的人员&#xff0c;我深感荣幸能够参加这次盛会。 前言 本次活动是百度Apollo社区工程师齐聚首钢Park&#xff0c;带来现场实操与技术分享。主要围绕Ap…

B027-MySQL增强

目录 多表查询为什么要用多表查询&#xff1f;笛卡尔积和内连接消除笛卡尔积外键数据库内连接练习左连接查询和右连接查询等值连接out join自连接子查询 数据操作(DML)数据的插入数据的删除数据的修改 数据库的备份与恢复Dos命令行窗口导出Dos命令行窗口导入Navicat导出Navicat…

【问题总结】Docker环境下,将Nacos版本2.0.4升级到2.2.3,操作留档 以及 踩坑记录

前记&#xff0c;鉴于nacos暴露的验证鉴权bug&#xff08;之前尝试解决但是没有完全解决&#xff01;&#xff0c;需要对公司之前架构留下来的老版本nacos进行升级 参考资料&#xff1a; https://nacos.io/zh-cn/blog/announcement-token-secret-key.html https://nacos.io/…

C++-内联函数

目录 一.什么是内联函数 1.内联函数的概念 2.内联函数的定义 二.C中引入内联函数的原因 三.什么样的函数适合被声明为内联呢&#xff1f; 四.面试题 一.什么是内联函数 1.内联函数的概念 以inline修饰的函数叫做内联函数&#xff0c;编译时C编译器会在调用内联函数的地方展开…

iptables入门

今天我的工作遇到了巡检网络配置的任务&#xff0c;这次巡检的主机都是运行十多年的机器&#xff0c;并不是新的firewalld&#xff0c;基本都是iptables&#xff0c;上学的时候以为这些都没人用&#xff0c;所以没有认真学习&#xff0c;现在需要用到了&#xff0c;所以写一篇文…

【Midjourney实战】| 新年礼盒元素设计

文章目录 1 初步提示词2 润色提示词3 提示词发散联想 这期实践任务&#xff0c;我们想去做一个新年礼盒的效果&#xff0c;最后我们想把不同元素拼在一起&#xff0c;方便后期进行新年的相关设计 1 初步提示词 提示词初步我们乍一想&#xff0c;肯定要包括主体元素礼盒 新年礼…

C# 雪花算法生成Id工具类

写在前面 传说自然界中并不存在两片完全一样的雪花的&#xff0c;每一片雪花都拥有自己漂亮独特的形状、独一无二&#xff1b;雪花算法也表示生成的ID如雪花般独一无二&#xff0c;该算法源自Twitter。 雪花算法主要用于解决分布式系统的唯一Id生成问题&#xff0c;在生产环境…

YOLOv8改进 | 2023 | FocalModulation替换SPPF(精度更高的空间金字塔池化)

一、本文介绍 本文给大家带来的改进是用FocalModulation技术来替换了原有的SPPF&#xff08;快速空间金字塔池化&#xff09;模块。FocalModulation是今年新提出的特征增强方法&#xff0c;它利用注意力机制来聚焦于图像中的关键区域&#xff0c;从而提高模型对这些区域的识别…

Python中检查字符串是否仅包含字母的多种方法:深入探究

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com 随着Python在数据处理和字符串操作方面的广泛应用&#xff0c;经常需要对字符串进行验证&#xff0c;确认其是否仅包含字母。本文将探讨Python中的多种方法来检查字符串是否只由字母组成&#xff0c;以及它们的应…