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,一经查实,立即删除!

相关文章

动态规划 Leetcode 96 不同的二叉搜索树

不同的二叉搜索树 Leetcode 96 学习记录自代码随想录 要点&#xff1a;1.递推公式&#xff0c;想到以根节点数字不同作为分类条件求和得到dp[i]&#xff1b; class Solution { public:int numTrees(int n) {if(n 1 || n 2) return n;// 1.dp[i]返回输入i时的满足条件的二…

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

目录 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;

大语言模型(LLM) RAG概念

RAG&#xff08;Retrieval-Augmented Generation&#xff09;是一种用于自然语言处理的模型架构&#xff0c;特别是针对生成式任务。RAG模型结合了检索和生成两种方法&#xff0c;以提高生成式任务的性能。它将信息检索&#xff08;Retrieval&#xff09;和文本生成&#xff08…

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

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

代码编写规范

一、程序风格的探讨 1、代码编写规范 按照阿里巴巴《Java开发手册》编码规约进行约束自己的编码风格。严格要求自己。 2、复杂逻辑编写 拆分功能&#xff1a;befHandler() aftHandler()等 按照步骤拆分&#xff1a;step1Task() step2Task() step3Task()等 只遵守法律…

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;这里有一些重要…

markdown(详细)快速入门

# markdown - 更简洁、更高效 很多人只把markdown用于网络文章发表&#xff0c;这糟蹋了markdown。 markdown不止是HTML的简化版&#xff0c;更重要的是txt的升级版、word的轻量版、笔记的最佳载体。 作为一种简单的格式标记语言&#xff0c;不同于txt的无格式&#xff0c;不…

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

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

代码随想录 贪心算法-难度题目-区间问题

目录 55.跳跃游戏 45.跳跃游戏|| 452.用最少数量的箭引爆气球 435.无重叠区间 763.划分字母区间 56.合并区间 55.跳跃游戏 55. 跳跃游戏 中等 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大…

【AI】举例说明计算机视觉(CV)技术的优势和挑战。

计算机视觉&#xff08;CV&#xff09;技术是一种让计算机能够理解和解释图像和视频内容的技术。以下是计算机视觉技术的优势和挑战的一些例子&#xff1a; 优势&#xff1a; 自动化处理&#xff1a;计算机视觉技术可以实现自动化处理&#xff0c;大大节省了人力成本和时间成本…

Linux下ifconfig,netstat 无法正常使用解决办法

yum install -y net-toolsifconfig替代查询命令&#xff1a; 查看网络接口信息&#xff1a; ip addr show显示某个特定接口&#xff08;例如eth0&#xff09;的详细信息&#xff1a; ip addr show eth0&#xff08;实际网卡名&#xff09;查看网络接口统计信息&#xff1a; ip …

JAVA中已有的栈和队列的实现

在Java中&#xff0c;有多种方式可以实现栈&#xff08;Stack&#xff09;和队列&#xff08;Queue&#xff09;的数据结构。以下是一些主要的实现方式&#xff1a; 1. 栈&#xff08;Stack&#xff09; 使用java.util.Stack类&#xff1a; java.util.Stack是Java提供的一个基…

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

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

Git高级玩法:Rebase、Cherry-pick与Stash实战解析

Git高级功能&#xff1a;理解Rebase、Cherry-pick与Stash 在软件开发过程中&#xff0c;Git作为版本控制系统&#xff0c;已经成为不可或缺的工具。而Git的高级功能&#xff0c;如Rebase、Cherry-pick与Stash&#xff0c;为开发者提供了更多的灵活性和便利性。本文将详细介绍这…

uView ui 安装步骤

uView是uni-app生态优秀的UI框架 安装说明 由于uView使用easycom模式&#xff0c;让您无需引入组件即可直接使用&#xff0c;但是此功能需要Hbuilder X 2.5.5及以上版本才支持&#xff0c;详见配置easycom组件模式。 easycom打包的时候是按需引入的&#xff0c;您可以放心引入…

关于应用程序自卸载能力的探讨

在当前数字化的时代背景下&#xff0c;应用程序的安装与卸载已成为我们日常生活及工作中常见的操作行为。对于“应用程序能否自我卸载”这一议题&#xff0c;引发了科技领域和广大用户群体的关注与讨论。本文将围绕该主题展开深入剖析&#xff0c;探究其实质内涵以及其可能产生…