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.mzph.cn/news/199620.shtml

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

相关文章

【C++】POCO学习总结(九):网络

【C】郭老二博文之&#xff1a;C目录 1、Poco::Net::IPAddress IP地址 Poco::Net::IPAddress类存储IPv4或IPv6主机地址。 Poco::Net::IPAddress可以从字符串解析&#xff0c;也可以格式化为字符串。支持IPv4格式(d.d.d.d)和IPv6格式(x: x: x: x: x: x: x: x)。 常用函数&…

springboot整合阿里云oss上传图片,解决无法预览的问题

1.前置工作 需要申请一个域名,需要备案&#xff0c;对接这个踩了不少坑,写的很详细,guan fang tong guo bu 了,各位参考别的博客结合看吧,主要是域名配置,还有看service里面的实现 2.进入控制台 bucket列表 选择bucket 选择域名管理 复制你申请的域名,比如域名:abkhkajs…

1970-2022年中国省级国家级开发区数据集

1970-2022年Z国省级国家级开发区数据集 1、时间&#xff1a;1970-2022年 2、指标&#xff1a;单位名称、所属区域、所属省份、所属级别、开发区类型、关注热度、成立时间、核准面积、主导产业、地址、联系电话、经纬度 3、范围&#xff1a;2781个开发区 4、来源&#xff1a…

MagicPipe3D地下管网三维建模数据规格

经纬管网建模系统MagicPipe3D&#xff08;www.magic3d.net&#xff09;本地离线参数化构建三维地下管网&#xff08;含管道、接头、附属物等&#xff09;模型&#xff0c;输出标准3DTiles、Obj等格式&#xff0c;支持Cesium、Unreal、Unity等引擎可视化查询。MagicPipe3D三维建…

c++基本常见错误总结

我们无论是在学习中还是在工作当中&#xff0c;总是会遇到各种各样的c编译错误问题&#xff0c;经常会有一种情况就是上一次好像遇到过这种问题&#xff0c;但是就是想不起来了&#xff08;我就是这样&#xff09;所以下面这一篇文章就是总结自己遇到的编译以及运行错误。 注意…

【C语言】函数递归详解(二)

前言 在上一篇博客函数递归详解&#xff08;一&#xff09;中讲解了什么是递归&#xff0c;递归的思想及限制条件以及两个递归的例子&#xff0c;这一篇博客将讲解递归与迭代的关系。 递归与迭代 递归是一种很好的编程技巧&#xff0c;但是同很多技巧一样也是可能被误用的&…

Centos7 制作Openssh9.5 RPM包

Centos7 制作Openssh9.5 RPM包 最近都在升级Openssh版本到9.3.在博客里也放了openssh 9.5的rpm包. 详见:https://blog.csdn.net/qq_29974229/article/details/133878576 但还是有小伙伴不停追问这个rpm包是怎么做的,怕下载别人的rpm包里被加了盐. 于是做了个关于怎么用官方的o…

yolov5实现多图形识别和图像训练

1.使用了yolov7,检测更好,但是训练上有问题,运行不起来,转了一圈发现yolov5是应用更广泛使用简单 2.怎么使用 //下载代码 https://github.com/ultralytics/yolov5 //安装依赖 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple some-package //按…

CMake中的CACHE关键字

2023年12月5日&#xff0c;周二晚上 在 CMake 中&#xff0c;CACHE 关键字用于在变量定义时将其值缓存起来&#xff0c;以便在后续的 CMake 运行中重用。这对于在多次构建过程中保持变量的持久性和一致性非常有用。 当使用 CACHE 关键字定义一个变量时&#xff0c;CMake 将会为…

【Axure高保真原型】3D大屏可视化模板

今天和大家分享3D大屏可视化的原型模板&#xff0c;里面包括3D条形图、3D柱状图、3D饼图、3D环形图、3D金字塔图&#xff0c;鼠标移入图表&#xff0c;对应区域会高亮变色&#xff0c;并且显示对应的数据标签&#xff0c;具体效果可以点击下方视频观看或打开下方预览地址查看哦…

【PduR路由】PduR模块详细介绍

目录 1.PDUR模块功能介绍 2.关键概念理解 3.功能详细设计 3.1I-PDU handling 3.1.1 I-PDU Reception to upper module(s) 3.1.1.1 Communication Interface 3.1.1.2 Transport Protocol 3.1.2 I-PDU Transmission from upper module(s) 3.1.2.1 Multicast 3.1.2.2 Co…

LeetCode 2477. 到达首都的最少油耗:深度优先搜索(DFS)

【LetMeFly】2477.到达首都的最少油耗&#xff1a;深度优先搜索(DFS) 力扣题目链接&#xff1a;https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital/ 给你一棵 n 个节点的树&#xff08;一个无向、连通、无环图&#xff09;&#xff0c;每个节点表示一…

仓库管理系统【GUI/Swing+MySQL】(Java课设)

系统类型 Swing窗口类型Mysql数据库存储数据 使用范围 适合作为Java课设&#xff01;&#xff01;&#xff01; 部署环境 jdk1.8Mysql8.0Idea或eclipsejdbc 运行效果 本系统源码地址&#xff1a; 更多系统资源库地址&#xff1a;更多Java课设系统 更多系统运行效果展示…

JavaSE基础50题:5. 求两个正整数的最大公约数

方法 我们用到辗转相除法&#xff0c;也叫欧几里得算法。 如&#xff1a;求 32和26 的最大公约数。 32➗ 26 1……6 &#xff08;此时余数不为0&#xff0c;继续&#xff0c;此行的除数26作为下一行的被除数&#xff0c;余数6作为下一行的除数&#xff09; 26 ➗ 6 4……2&a…

为什么Nginx被称为反向代理

下图显示了 &#x1d41f;&#x1d428;&#x1d42b;&#x1d430;&#x1d41a;&#x1d42b;&#x1d41d; &#x1d429;&#x1d42b;&#x1d428;&#x1d431;&#x1d432; 和 &#x1d42b;&#x1d41e;&#x1d42f;&#x1d41e;&#x1d42b;&#x1d42c;&#…

甘草书店:#7 2023年11月19日 星期日 波澜不惊的日子里稳步前行

前进&#xff0c;可以伴着惊涛骇浪&#xff0c;也可以波澜不惊。 几番沟通&#xff0c;多方协商之后&#xff0c;甘草书店硬装方案基本确定&#xff0c;近期开始施工。 书目选择方面也在逐步推进。 就像之前设想的&#xff0c;划分成企业经管和个人成长两大类的前提下&#…

SpringBoot3-创建自定义启动器,使用自定义starter启动器

1、创建自定义启动工程pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.a…

软件设计之适配器模式

类模式 我们知道插座的电压为交流电220V&#xff0c;而日常电器使用的是直流电且电压会较小&#xff0c;比如手机充电会通过插头适配器达到额定的输入电流。下面我们实现这个案例&#xff1a;将220V电压转化为5V的电压。 package Adapter.Class;public class Adapter extends …

算法通关村——数论问题

数论是一个很重要的学科&#xff0c;覆盖领域极广&#xff0c;小到小学的智力问题&#xff0c;大到世界顶级科学家都一直在研究相关问题&#xff0c;因此其难度跨度非常大。在程序设计里 &#xff0c;也经常会出现数论的问题&#xff0c;但是&#xff0c;这些一般都是比较基本的…

【@ConfigurationProperties注解的用处】

介绍 ConfigurationProperties 是 Spring 框架中的一个注解&#xff0c;用于将配置文件中的属性映射到 Java 对象的字段上。它的主要用途是简化配置文件与 Java 对象之间的映射过程&#xff0c;使得配置更加方便、可读&#xff0c;并提供类型安全的属性访问。 用途和特性 属性…