OCP Java17 SE Developers 复习题11

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

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

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

A, C, D, E.  A method that declares an exception isn't required to throw one, making option A correct. Unchecked exceptions can be thrown in any method, making options C and E correct. Option D matches the exception type declared, so it's also correct. Option B is incorrect because a broader exception is not allowed.

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

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

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

F.  The code does not compile because the throw and throws keywords are incorrectly used on lines 6, 7, and 9. If the keywords were fixed, the rest of the code would compile and print a stack trace with YesProblem at runtime. For this reason, option F is correct.

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

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

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

A, D, E.  Localization refers to user-facing elements. Dates, currency, and numbers are commonly used in different formats for different countries, making options A, D, and E correct. Class and variable names, along with lambda expressions, are internal to the application, so there is no need to translate them for users.

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

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

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

E.  The order of catch blocks is important because they're checked in the order they appear after the try block. Because ArithmeticException is a child class of RuntimeException, the catch block on line 7 is unreachable (if an ArithmeticException is thrown in the try block, it will be caught on line 5). Line 7 generates a compiler error because it is unreachable code, making option E correct.

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

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

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

C, F.  The code compiles and runs without issue. When a CompactNumberFormat instance is requested without a style, it uses the SHORT style by default. This results in both of the first two statements printing 100K, making option C correct. If the LONG style were used, then 100 thousand would be printed. Option F is also correct, as the full value is printed with a currency formatter.

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

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

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

E.  A LocalDate does not have a time element. Therefore, a date/time formatter is not appropriate. The code compiles but throws an exception at runtime, making option E correct. If ISO_LOCAL_DATE were used, the code would print 2022 APRIL 30.

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

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

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

E.  The first compiler error is on line 12 because each resource in a try-with-resources statement must have its own data type and be separated by a semicolon (;). Line 15 does not compile because the variable s is already declared in the method. Line 17 also does not compile. The FileNotFoundException, which inherits from IOException and Exception, is a checked exception, so it must be handled in a try/catch block or declared by the method. Because these three lines of code do not compile, option E is the correct answer.

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

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

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

C.  Java will first look for the most specific matches it can find, starting with Dolphins_en_US.properties. Since that is not an answer choice, it drops the country and looks for Dolphins_en.properties, making option C correct. Option B is incorrect because a country without a language is not a valid locale.

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

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

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

D.  When working with a custom number formatter, the 0 symbol displays the digit as 0, even if it's not present, while the # symbol omits the digit from the start or end of the String if it is not present. Based on the requested output, a String that displays at least three digits before the decimal (including a comma) and at least one after the decimal is required. It should display a second digit after the decimal if one is available. For this reason, option D is the correct answer.

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

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

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

B.  An IllegalArgumentException is used when an unexpected parameter is passed into a method, making option B correct. Option A is incorrect because returning null or -1 is a common return value for searching for data. Option D is incorrect because a for loop is typically used for this scenario. Option E is incorrect because you should find out how to code the method and not leave it for the unsuspecting programmer who calls your method. Option C is incorrect because you should run!

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

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

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

B, E, F.  An exception that must be handled or declared is a checked exception. A checked exception inherits Exception but not RuntimeException. The entire hierarchy counts, so options B and E are both correct. Option F is also correct, as a class that inherits Throwable but not RuntimeException or Error is also checked.

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

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

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

B, C.  The code does not compile as is because the exception declared by the close() method must be handled or declared. Option A is incorrect because removing the exception from the declaration causes a compilation error on line 4, as FileNotFoundException is a checked exception that must be handled or declared. Option B is correct because the unhandled exception within the main() method becomes declared. Option C is also correct because the exception becomes handled. Option D is incorrect because the exception remains unhandled.

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

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

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

A, B.  A try-with-resources statement does not require a catch or finally block. A traditional try statement requires at least one of the two. Neither statement can be written without a body encased in braces, {}. For these reasons, options A and B are correct.

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

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

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

C.  Starting with Java 15, NullPointerException stack traces include the name of the variable that is null by default, making option A incorrect. The first NullPointerException encountered at runtime is when dewey.intValue() is called, making option C correct. Options E and F are incorrect as only one NullPointerException exception can be thrown at a time.

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

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

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

C, D.  The code compiles with the appropriate input, so option G is incorrect. A locale consists of a required lowercase language code and optional uppercase country code. In the Locale() constructor, the language code is provided first. For these reasons, options C and D are correct. Option E is incorrect because a Locale is created using a constructor or Locale.Builder class. Option F is really close but is missing build() at the end. Without that, option F does not compile.

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

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

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

F.  The code compiles, but the first line produces a runtime exception regardless of what is inserted into the blank, making option F correct. When creating a custom formatter, any nonsymbol code must be properly escaped using pairs of single quotes ('). In this case, it fails because o is not a symbol. Even if you didn't know o wasn't a symbol, the code contains an unmatched single quote. If the properly escaped value of "hh' o''clock'" were used, then the correct answers would be ZonedDateTimeLocalDateTime, and LocalTime. Option B would not be correct because LocalDate values do not have an hour part.

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

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

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

D, F.  Option A is incorrect because Java will look at parent bundles if a key is not found in a specified resource bundle. Option B is incorrect because resource bundles are loaded from static factory methods. Option C is incorrect, as resource bundle values are read from the ResourceBundle object directly. Option D is correct because the locale is changed only in memory. Option E is incorrect, as the resource bundle for the default locale may be used if there is no resource bundle for the specified locale (or its locale without a country code). Finally, option F is correct. The JVM will set a default locale automatically.

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

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

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

C.  After both resources are declared and created in the try-with-resources statement, T is printed as part of the body. Then the try-with-resources completes and closes the resources in the reverse of the order in which they were declared. After W is printed, an exception is thrown. However, the remaining resource still needs to be closed, so D is printed. Once all the resources are closed, the exception is thrown and swallowed in the catch block, causing E to be printed. Last, the finally block is run, printing F. Therefore, the answer is TWDEF and option C is correct.

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

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

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

D.  Java will use Dolphins_fr.properties as the matching resource bundle on line 7 because it is an exact match on the language of the requested locale. Line 8 finds a matching key in this file. Line 9 does not find a match in that file; therefore, it has to look higher up in the hierarchy. Once a bundle is chosen, only resources in that hierarchy are allowed. It cannot use the default locale anymore, but it can use the default resource bundle specified by Dolphins.properties. For these reasons, option D is correct.

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

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

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

G.  The main() method invokes go(), and A is printed on line 3. The stop() method is invoked, and E is printed on line 14. Line 16 throws a NullPointerException, so stop() immediately ends, and line 17 doesn't execute. The exception isn't caught in go(), so the go() method ends as well, but not before its finally block executes and C is printed on line 9. Because main() doesn't catch the exception, the stack trace displays, and no further output occurs. For these reasons, AEC is printed followed by a stack trace for a NullPointerException, making option G correct.

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

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

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

C.  The code does not compile because the multi-catch block on line 7 cannot catch both a superclass and a related subclass. Options A and B do not address this problem, so they are incorrect. Since the try body throws SneezeException, it can be caught in a catch block, making option C correct. Option D allows the catch block to compile but causes a compiler error on line 6. Both of the custom exceptions are checked and must be handled or declared in the main() method. A SneezeException is not a SniffleException, so the exception is not handled. Likewise, option E leads to an unhandled exception compiler error on line 6.

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

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

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

B.  For this question, the date used is April 5, 2022 at 12:30:20pm. The code compiles, and either form of the formatter is correct: dateTime.format(formatter) or formatter.format(dateTime). The custom format m returns the minute, so 30 is output first. The next line throws an exception as z relates to time zone, and date/time does not have a zone component. This exception is then swallowed by the try/catch block. Since this is the only value printed, option B is correct. If the code had not thrown an exception, the last line would have printed 2022.

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

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

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

A, E.  Resources must inherit AutoCloseable to be used in a try-with-resources block. Since Closeable, which is used for I/O classes, extends AutoCloseable, both may be used, making options A and E correct.

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

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

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

G.  The code does not compile because the resource walk1 is not final or effectively final and cannot be used in the declaration of a try-with-resources statement. For this reason, option G is correct. If the line that set walk1 to null were removed, then the code would compile and print blizzard 2 at runtime, with the exception inside the try block being the primary exception since it is thrown first. Then two suppressed exceptions would be added to it when trying to close the AutoCloseable resources.

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

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

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

A.  The code compiles and prints the value for Germany, 2,40 €, making option A the correct answer. Note that the default locale category is ignored since an explicit currency locale is selected.

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

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

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

B, F.  The try block is not capable of throwing an IOException, making the catch block unreachable code and option A incorrect. Options B and F are correct, as both are unchecked exceptions that do not extend or inherit from IllegalArgumentException. Remember, it is not a good idea to catch Error in practice, although because it is possible, it may come up on the exam. Option C is incorrect because the variable c is declared already in the method declaration. Option D is incorrect because the IllegalArgumentException inherits from RuntimeException, making the first declaration unnecessary. Similarly, option E is incorrect because NumberFormatException inherits from IllegalArgumentException, making the second declaration unnecessary. Since options B and F are correct, option G is incorrect.

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

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

相关文章

隐私保护和带宽有效的联邦学习:在医院死亡率预测中的应用-文章翻译

隐私保护和带宽有效的联邦学习:在医院死亡率预测中的应用 摘要 机器学习,特别是联邦机器学习,在医学研究和患者护理方面开辟了新的视角。尽管联邦机器学习在隐私方面比集中式机器学习有所改进,但它不提供可证明的隐私保证。此外,联邦机器学习在带宽消耗方面相当昂贵,因…

【Easy云盘 | 第三篇】登录注册模块上篇(获取验证码、发送邮箱验证码、登录、注册、重置密码)基于AOP实现参数校验

文章目录 4.2登录注册模块设计4.2.1获取验证码(1)思路(2)接口(3)controller层(4)CreateImageCodeUtils工具类(5)测试结果 4.2.2发送邮箱验证码(1&…

leetcode热题100.跳跃游戏2

Problem: 45. 跳跃游戏 II 文章目录 题目思路复杂度Code 题目 给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。 每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i j] 处: …

Hadoop MapReduce

MapReduce分为两个阶段,分为Map阶段和Reduce阶段,可以自定义map函数和reduce函数, map函数的输入是行在文件的字节偏移量,value是文件的一行数据。 reduce函数的输入是key和对应key的value组,然后reduce函数可以对这…

FebHost:墨西哥外贸独立站为何钟情于.MX域名?

在数字化时代的大潮中,墨西哥的互联网用户对于 .mx 域名展现出了浓厚的兴趣。这一现象背后的原因引发了广泛的关注。 首先,本地相关性无疑是 .mx 域名吸引墨西哥用户的重要因素。当用户在网络世界中漫游时,一个带有 .mx 域名的网站往往能迅速…

DSO9254A安捷伦DSO9254A示波器

181/2461/8938产品概述: 安捷伦DSO9254A的带宽为2.5 GHz,配备15英寸XGA液晶显示屏,采用静音封装,厚度仅为9英寸(23厘米),重量仅为26磅(11.8千克)。DSO9254A集成了一个功…

MongoDB初探:安装与图形化界面保姆级使用指南

文章目录 前言一、MongoDB下载安装下载解压配置环境变量打开mongoDB 二、配置本地MongoDB服务创建文件下载服务测试服务 三、图形化界面Compass GUINavicat GUI 总结 前言 MongoDB是一种流行的开源、面向文档的NoSQL数据库程序。与传统的关系型数据库不同,MongoDB将…

观《你想活出怎样的人生》有感

《你想活出怎样的人生》 四月六号,和赵茜小美女观看了宫崎骏导演拍的《你想活出怎样的人生》,感受颇丰,特此写一篇文章以记之。 电影简介 《你想活出怎样的人生》是宫崎骏执导的动画电影,不仅是宫崎骏的复出之作,也…

【LeetCode热题100】74. 搜索二维矩阵(二分)

一.题目要求 给你一个满足下述两条属性的 m x n 整数矩阵: 每行中的整数从左到右按非严格递增顺序排列。每行的第一个整数大于前一行的最后一个整数。 给你一个整数 target ,如果 target 在矩阵中,返回 true ;否则,…

【第六篇】使用BurpSuite重发请求

在研究目标网站对不同输入的响应时,不必每次都拦截请求,否则造成时间浪费,我们可使用 Burp Repeater 模块实现重发。 如图,我们可以选择某个请求包,单击右键,将其发送至Repeater模块: 接着&…

day03-Docker

1.初识 Docker 1.1.什么是 Docker 1.1.1.应用部署的环境问题 大型项目组件较多,运行环境也较为复杂,部署时会碰到一些问题: 依赖关系复杂,容易出现兼容性问题开发、测试、生产环境有差异 例如一个项目中,部署时需要依…

基于51单片机电子钟万年历设计—LCD1602显示

基于51单片机电子钟万年历设计 (仿真+程序+设计报告) 功能介绍 具体功能: 1.DS1302记录时间; 2.LCD1602实时显示日期、星期、时分秒; 3.按键设置日期、时间、闹钟; 4.使用蜂鸣器…