OCP Java17 SE Developers 复习题09

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

===========================================================

===========================================================

A, E.  For the first scenario, the answer needs to implement List because the scenario allows duplicates, narrowing it down to options A and D. Option A is a better answer than option D because LinkedList is both a List and a Queue, and you just need a regular List.

For the second scenario, the answer needs to implement Map because you are dealing with key/value pairs per the unique id field. This narrows it down to options B and E. Since the question talks about ordering, you need the TreeMap. Therefore, the answer is option E.

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

===========================================================

===========================================================

C, G.  Line 12 creates a List<?>, which means it is treated as if all the elements are of type Object rather than String. Lines 15 and 16 do not compile since they call the String methods isEmpty() and length(), which are not defined on Object. Line 13 creates a List<String> because var uses the type that it deduces from the context. Lines 17 and 18 do compile. However, List.of() creates an immutable list, so both of those lines would throw an UnsupportedOperationException if run. Therefore, options C and G are correct.

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

===========================================================

===========================================================

B.  This is a double-ended queue. On lines 4 and 5, we add to the back, giving us [hello, hi]. On line 6, we add to the front and have [ola, hello, hi]. On line 7, we remove the first element, which is "ola". On line 8, we look at the new first element ("hello") but don't remove it. On lines 9 and 10, we remove each element in turn until no elements are left, printing hello and hi together which makes option B the answer.

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

===========================================================

===========================================================

B, F.  Option A does not compile because the generic types are not compatible. We could say HashSet<? extends Number> hs2 = new HashSet<Integer>();. Option B uses a lower bound, so it allows superclass generic types. Option C does not compile because the diamond operator is allowed only on the right side. Option D does not compile because a Set is not a List. Option E does not compile because upper bounds are not allowed when instantiating the type. Finally, option F does compile because the upper bound is on the correct side of the =.

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

===========================================================

===========================================================

B.  The record compiles and runs without issue. Line 8 gives a compiler warning for not using generics but not a compiler error. Line 7 creates the Hello class with the generic type String. It also passes an int to the println() method, which gets autoboxed into an Integer. While the println() method takes a generic parameter of type T, it is not the same <T> defined for the class on line 1. Instead, it is a different T defined as part of the method declaration on line 3. Therefore, the String argument on line 7 applies only to the class. The method can take any object as a parameter, including autoboxed primitives. Line 8 creates the Hello class with the generic type Object since no type is specified for that instance. It passes a boolean to println(), which gets autoboxed into a Boolean. The result is that hi-1hola-true is printed, making option B correct.

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

===========================================================

===========================================================

B, F.  We're looking for a Comparator definition that sorts in descending order by beakLength. Option A is incorrect because it sorts in ascending order by beakLength. Similarly, option C is incorrect because it sorts by beakLength in ascending order within those matches that have the same name. Option E is incorrect because there is no thenComparingNumber() method.

Option B is a correct answer, as it sorts by beakLength in descending order. Options D and F are trickier. First, notice that we can call either thenComparing() or thenComparingInt() because the former will simply autobox the int into an Integer. Then observe what reversed() applies to. Option D is incorrect because it sorts by name in ascending order and only reverses the beak length of those with the same name. Option F creates a comparator that sorts by name in ascending order and then by beak size in ascending order. Finally, it reverses the result. This is just what we want, so option F is correct.

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

===========================================================

===========================================================

B, F.  A valid override of a method with generic arguments must have a return type that is covariant, with matching generic type parameters. Options D and E are incorrect because the return type is too broad. Additionally, the generic arguments must have the same signature with the same generic types. This eliminates options A and C. The remaining options are correct, making the answer options B and F.

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

===========================================================

===========================================================

A.  The array is sorted using MyComparator, which sorts the elements in reverse alphabetical order in a case-insensitive fashion. Normally, numbers sort before letters. This code reverses that by calling the compareTo() method on b instead of a. Therefore, option A is correct.

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

===========================================================

===========================================================

A, B, D.  The generic type must be Exception or a subclass of Exception since this is an upper bound, making options A and B correct. Options C and E are wrong because Throwable is a superclass of Exception. Additionally, option D is correct despite the odd syntax by explicitly listing the type. You should still be able to recognize it as acceptable.

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

===========================================================

===========================================================

A, B, E, F.  The forEach() method works with a List or a Set. Therefore, options A and B are correct. Additionally, options E and F return a Set and can be used as well. Options D and G refer to methods that do not exist. Option C is tricky because a Map does have a forEach() method. However, it uses two lambda parameters rather than one. Since there is no matching System.out.println method, it does not compile.

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

===========================================================

===========================================================

B, E.  The showSize() method can take any type of List since it uses an unbounded wildcard. Option A is incorrect because it is a Set and not a List. Option C is incorrect because the wildcard is not allowed to be on the right side of an assignment. Option D is incorrect because the generic types are not compatible.

Option B is correct because a lower-bounded wildcard allows that same type to be the generic. Option E is correct because Integer is a subclass of Number.

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

===========================================================

===========================================================

C.  This question is difficult because it defines both Comparable and Comparator on the same object. The t1 object doesn't specify a Comparator, so it uses the Comparable object's compareTo() method. This sorts by the text instance variable. The t2 object does specify a Comparator when calling the constructor, so it uses the compare() method, which sorts by the int. This gives us option C as the answer.

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

===========================================================

===========================================================

A.  When using binarySearch(), the List must be sorted in the same order that the Comparator uses. Since the binarySearch() method does not specify a Comparator explicitly, the default sort order is used. Only c2 uses that sort order and correctly identifies that the value 2 is at index 0. Therefore, option A is correct. The other two comparators sort in descending order. Therefore, the precondition for binarySearch() is not met, and the result is undefined for those two. The two calls to reverse() are just there to distract you; they cancel each other out.

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

===========================================================

===========================================================

A, B.  Y is both a class and a type parameter. This means that within the class Z, when we refer to Y, it uses the type parameter. All of the choices that mention class Y are incorrect because it no longer means the class Y. Only options A and B are correct.

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

===========================================================

===========================================================

A, C.  A LinkedList implements both List and Queue. The List interface has a method to remove by index. Since this method exists, Java does not autobox to call the other method, making the output [10] and option A correct. Similarly, option C is correct because the method to remove an element by index is available on a LinkedList<Object> (which is what var represents here). By contrast, Queue has only the remove by object method, so Java does autobox there. Since the number 1 is not in the list, Java does not remove anything for the Queue, and the output is [10, 12].

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

===========================================================

===========================================================

E.  This question looks like it is about generics, but it's not. It is trying to see whether you noticed that Map does not have a contains() method. It has containsKey() and containsValue() instead, making option E the answer. If containsKey() were called, the answer would be false because 123 is an Integer key in the Map, rather than a String.

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

===========================================================

===========================================================

A, E.  The key to this question is keeping track of the types. Line 48 is a Map<Integer, Integer>. Line 49 builds a List out of a Set of Entry objects, giving us List<Entry<Integer, Integer>>. This causes a compiler error on line 56 since we can't multiply an Entry object by two.

Lines 51–54 are all of type List<Integer>. The first three are immutable, and the one on line 54 is mutable. This means line 57 throws an UnsupportedOperationException since we attempt to modify the list. Line 58 would work if we could get to it. Since there is one compiler error and one runtime error, options A and E are correct.

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

===========================================================

===========================================================

B.  When using generic types in a method, the generic specification goes before the return type and option B is correct.

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

===========================================================

===========================================================

F.  The first call to merge() calls the mapping function and adds the numbers to get 13. It then updates the map. The second call to merge() sees that the map currently has a null value for that key. It does not call the mapping function but instead replaces it with the new value of 3. Therefore, option F is correct.

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

===========================================================

===========================================================

B, D, F.  The java.lang.Comparable interface is implemented on the object to compare. It specifies the compareTo() method, which takes one parameter. The java.util.Comparator interface specifies the compare() method, which takes two parameters. This gives us options B, D, and F as the answers.

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

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

相关文章

代码随想录 贪心算法-难度题目-其他题目

目录 53.最大子数组和 134.加油站 968.监控二叉树 53.最大子数组和 53. 最大子数组和 中等 给你一个整数数组 nums &#xff0c;请你找出一个具有最大和的连续子数组&#xff08;子数组最少包含一个元素&#xff09;&#xff0c;返回其最大和。 子数组 是数组中的一个…

git提交代码描述时如何换行(更新时间24/3/12)

问题复现&#xff08;信心满满使用转义字符换行&#xff09; 解决方法&#xff1a; 写多个-m字符串的结构可以实现自动换行 注意空格 git commit -m"第一行描述" -m"第二行描述" 效果演示&#xff1a;&#xff08;强迫症福利&#xff09;

网络学习:BGP路径属性分类

目录 前言&#xff1a; 路径属性分类 公认必遵 公认任意 可选过渡 可选非过渡 前言&#xff1a; 在默认情况下&#xff0c;到达同一目的地&#xff0c;BGP只走单条路径&#xff0c;并不会在多条路径之间执行负载均衡。对于IGP路由协议&#xff0c;当有多条路径可以到达同…

dangzero环境配置问题

文章目录 安装虚拟机dangzeroCompile the KML kernelObtain Ubuntu 20.04Create VMInstall UbuntuRun UbuntuMove KML kernel to VMInside VM: Install KernelUpdate grub to auto-select KML kernelBoot parametersRun KMLTest KMLObtain glibc-2.31Install gcc-5 for kernel …

KEIL 5.38的ARM-CM3/4 ARM汇编设计学习笔记10 - STM32的SDIO学习2 - Card Identification

KEIL 5.38的ARM-CM3/4 ARM汇编设计学习笔记10 - STM32的SDIO学习2 - Card Identification 一、问题回顾二、本次的任务三、 需要注意的问题3.1 Card Identification Mode时的时钟频率3.2 CMD0指令的疑似问题3.3 发送带参数的ACMD41时要注意时间时序和时效3.4 CPSM的指令发送问题…

【Linux】深入探索:Linux网络调试、追踪与优化

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a;Linux ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 前言 正文 1. 调试网络问题 a. 使用ping和traceroute b. 使用netstat和ss c. 使用tcpdump和Wireshark 2. 追踪网络问题 a. 使用mtr b.…

北京公司注册地址想要迁到新疆该如何操作

尊敬的客户&#xff0c;您好&#xff01;我是经典世纪胡云帅&#xff08;游览器搜经典世纪胡云帅&#xff09;&#xff0c;您选择了北京经典世纪集团有限公司-资 质代办&#xff0c;我们将竭诚为您服务&#xff01;如果您的公司注册地址想要迁到新疆&#xff0c;这里有一些重要…

一台服务器,最大支持的TCP连接数是多少?

一个服务端进程最大能支持多少条 TCP 连接&#xff1f; 一台服务器最大能支持多少条 TCP 连接&#xff1f; 一、原理 TCP 四元组的信息&#xff1a;源IP、源端口、目标IP、目标端口。 一个服务端进程最大能支持的 TCP 连接个数的计算公式&#xff1a;最大tcp连接数客户端的IP…

基于springboot实现成人教育教务系统项目【项目源码+论文说明】

基于springboot实现成人教育教务系统演示 摘要 随着市场经济的产业化结构升级&#xff0c;人才结构也在不断发生这巨大的变化和变革。而且各大企业都在处于一个高速发展和壮大的阶段&#xff0c;在这个高速发展和结构化升级的时期对于人才的需求也在不断的增多。企业和用工单位…

【Python】新手入门学习:详细介绍组合/聚合复用原则(CARP)及其作用、代码示例

【Python】新手入门学习&#xff1a;详细介绍组合/聚合复用原则&#xff08;CARP&#xff09;及其作用、代码示例 &#x1f308; 个人主页&#xff1a;高斯小哥 &#x1f525; 高质量专栏&#xff1a;Matplotlib之旅&#xff1a;零基础精通数据可视化、Python基础【高质量合集…

Nmap最常用命令(非常详细)零基础入门到精通,收藏这一篇就够了

nmap是我们最常用的工具。但是命令太多了&#xff0c;没办法全部记下。我们在实际工作中只需要记住最常用的几条命令就行了。 主机发现 里面nmap&#xff0c;我们可以扫描在同一局域网内有哪些设备在线。常用命令如下&#xff1a; nmap 192.168.50.1/24 -sL 上面命令&#…

羊大师分析,羊奶和牛奶哪个更有营养

羊大师分析&#xff0c;羊奶和牛奶哪个更有营养 羊奶和牛奶都是营养丰富的奶制品&#xff0c;它们各自具有独特的营养价值和特点&#xff0c;因此无法简单地判断哪个更有营养。 羊奶中含有较高的脂肪和蛋白质&#xff0c;同时富含矿物质和维生素&#xff0c;如钙、磷、铁、锌以…

揭秘FastStone Capture:一款强大且高效的截图工具

目录 【引子】【FastStone Capture概述】【安装步骤】【使用攻略】【核心功能解析】【总结】 【引子】 在数字化信息时代&#xff0c;无论是工作汇报、在线教学&#xff0c;还是日常交流中&#xff0c;屏幕截图已经成为我们必不可少的辅助工具。今天&#xff0c;我要为大家详细…

git svn混用

背景 项目代码管理初始使用的svn, 由于svn代码操作&#xff0c;无法在本地暂存&#xff0c;有诸多不便&#xff0c;另外本人习惯使用git. 所以决定迁移至git管理 迁移要求&#xff1a; 保留历史提交记录 迁移流程 代码检出 git svn svn_project_url git代码提交 修改本…

得物布局构建耗时优化方案实践

一、背景 当谈到移动应用程序的体验时&#xff0c;页面启动速度是其中至关重要的一点&#xff0c;更快的页面展示速度确保应用程序可以迅速加载并响应用户的操作, 从而提高用户使用 App 时的满意度。在页面启动的整个流程中&#xff0c;随着 UI 复杂度的上升&#xff0c;布局的…

【Java探索之旅】解密Java中的类型转换与类型提升

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; Java编程秘籍 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f4d1;前言一、类型转化1.1 自动类型转换&#xff08;隐式类型转换&#xff09;1.2 强制类型转换…

Arduino IDE的下载和安装

一、Arduino的介绍 Arduino是一款开源电子原型平台&#xff0c;主要包含两部分&#xff1a;硬件&#xff08;各种型号的Arduino板&#xff09;和软件&#xff08;Arduino IDE&#xff09;。这个平台由意大利的Massimo Banzi、David Cuartielles等人共同开发设计&#xff0c;并于…

ES分片均衡策略分析与改进

从故障说起 某日早高峰收到 Elasticsearch 大量查询超时告警&#xff0c;不同于以往&#xff0c;查看 Elasticsearch 查询队列监控后发现&#xff0c;仅123节点存在大量查询请求堆积。 各节点查询队列堆积情况 查看节点监控发现&#xff0c;123节点的 IO 占用远高于其他节点。…

【论文阅读】IEEE Access 2019 BadNets:评估深度神经网络的后门攻击

文章目录 一.论文信息二.论文内容1.摘要2.引言3.主要图表4.结论 一.论文信息 论文题目&#xff1a; BadNets: Evaluating Backdooring Attacks on Deep Neural Networks&#xff08;BadNets:评估深度神经网络的后门攻击&#xff09; 论文来源&#xff1a; 2019-IEEE Access …

安卓内嵌uniapp的H5页面 android调用h5内部方法

uniapp H5与原生安卓的数据互通和方法调用_安卓代码中调用uniapp中页面中的方法-CSDN博客文章浏览阅读8.7k次&#xff0c;点赞4次&#xff0c;收藏22次。1、准备我这里是uniapp与原生安卓之间的相互调用&#xff0c;也就是原生安卓内嵌H5页面&#xff0c;下面先来准备一下安卓端…