OCP Java17 SE Developers 复习题06

news2024/11/26 15:53:16

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

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

E.  Options A and B will not compile because constructors cannot be called without new. Options C and D will compile but will create a new object rather than setting the fields in this one. The result is the program will print 0, not 2, at runtime. Calling an overloaded constructor, using this(), or a parent constructor, using super(), is only allowed on the first line of the constructor, making option E correct and option F incorrect. Finally, option G is incorrect because the program prints 0 without any changes, not 2.

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

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

A, B, F.  The final modifier can be used with private and static, making options A and F correct. Marking a private method final is redundant but allowed. A private method may also be marked static, making option B correct. Options C, D, and E are incorrect because methods marked staticprivate, or final cannot be overridden; therefore, they cannot be marked abstract.

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

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

B, C.  Overloaded methods have the same method name but a different signature (the method parameters differ), making option A incorrect. Overridden instance methods and hidden static methods must have the same signature (the name and method parameters must match), making options B and C correct. Overloaded methods can have different return types, while overridden and hidden methods can have covariant return types. None of these methods are required to use the same return type, making options D, E, and F incorrect.

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

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

F.  The code will not compile as is, because the parent class Mammal does not define a no-argument constructor. For this reason, the first line of a Platypus constructor should be an explicit call to super(int), making option F the correct answer. Option E is incorrect, as line 7 compiles without issue. The sneeze() method in the Mammal class is marked private, meaning it is not inherited and therefore is not overridden in the Platypus class. For this reason, the sneeze() method in the Platypus class is free to define the same method with any return type.

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

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

E.  The code compiles, making option F incorrect. An instance variable with the same name as an inherited instance variable is hidden, not overridden. This means that both variables exist, and the one that is used depends on the location and reference type. Because the main() method uses a reference type of Speedster to access the numSpots variable, the variable in the Speedster class, not the Cheetah class, must be set to 50. Option A is incorrect, as it reassigns the method parameter to itself. Option B is incorrect, as it assigns the method parameter the value of the instance variable in Cheetah, which is 0. Option C is incorrect, as it assigns the value to the instance variable in Cheetah, not Speedster. Option D is incorrect, as it assigns the method parameter the value of the instance variable in Speedster, which is 0. Options A, B, C, and D all print 0 at runtime. Option E is the only correct answer, as it assigns the instance variable numSpots in the Speedster class a value of 50. The numSpots variable in the Speedster class is then correctly referenced in the main() method, printing 50 at runtime.

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

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

D, E.  The Moose class doesn't compile, as the final variable antlers is not initialized when it is declared, in an instance initializer, or in a constructor. Caribou and Reindeer are not immutable because they are not marked final, which means a subclass could extend them and add mutable fields. Elk and Deer are both immutable classes since they are marked final and only include private final members, making options D and E correct. As shown with Elk, a class doesn't need to declare any fields to be considered immutable.

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

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

A.  The code compiles and runs without issue, so options E and F are incorrect. The Arthropod class defines two overloaded versions of the printName() method. The printName() method that takes an int value on line 5 is correctly overridden in the Spider class on line 9. Remember, an overridden method can have a broader access modifier, and protected access is broader than package access. Because of polymorphism, the overridden method replaces the method on all calls, even if an Arthropod reference variable is used, as is done in the main() method. For these reasons, the overridden method is called on lines 14 and 15, printing Spider twice. Note that the short value is automatically cast to the larger type of int, which then uses the overridden method. Line 16 calls the overloaded method in the Arthropod class, as the long value 5L does not match the overridden method, resulting in Arthropod being printed. Therefore, option A is the correct answer.

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

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

D.  The code compiles without issue. The question is making sure you know that superclass constructors are called in the same manner in abstract classes as they are in non-abstract classes. Line 9 calls the constructor on line 6. The compiler automatically inserts super() as the first line of the constructor defined on line 6. The program then calls the constructor on line 3 and prints Wow-. Control then returns to line 6, and Oh- is printed. Finally, the method call on line 10 uses the version of fly() in the Pelican class, since it is marked private and the reference type of var is resolved as Pelican. The final output is Wow-Oh-Pelican, making option D the correct answer. Remember that private methods cannot be overridden. If the reference type of chirp was Bird, then the code would not compile as it would not be accessible outside the class.

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

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

B, E.  The signature must match exactly, making option A incorrect. There is no such thing as a covariant signature. An overridden method must not declare any new checked exceptions or a checked exception that is broader than the inherited method. For this reason, option B is correct, and option D is incorrect. Option C is incorrect because an overridden method may have the same access modifier as the version in the parent class. Finally, overridden methods must have covariant return types, and only void is covariant with void, making option E correct.

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

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

A, C.  Option A is correct, as this(3) calls the constructor declared on line 5, while this("") calls the constructor declared on line 10. Option B does not compile, as inserting this() at line 3 results in a compiler error, since there is no matching constructor. Option C is correct, as short can be implicitly cast to int, resulting in this((short)1) calling the constructor declared on line 5. In addition, this(null) calls the String constructor declared on line 10. Option D does not compile because inserting super() on line 14 results in an invalid constructor call. The Howler class does not contain a no-argument constructor. Option E is also incorrect. Inserting this(2L) at line 3 results in a recursive constructor definition. The compiler detects this and reports an error. Option F is incorrect, as using super(null) on line 14 does not match any parent constructors. If an explicit cast was used, such as super((Integer)null), then the code would have compiled but would throw an exception at runtime during unboxing. Finally, option G is incorrect because the superclass Howler does not contain a no-argument constructor. Therefore, the constructor declared on line 13 will not compile without an explicit call to an overloaded or parent constructor.

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

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

C.  The code compiles and runs without issue, making options F and G incorrect. Line 16 initializes a PolarBear instance and assigns it to the bear reference. The variable declaration and instance initializers are run first, setting value to tac. The constructor declared on line 5 is called, resulting in value being set to tacb. Remember, a static main() method can access private constructors declared in the same class. Line 17 creates another PolarBear instance, replacing the bear reference declared on line 16. First, value is initialized to tac as before. Line 17 calls the constructor declared on line 8, since String is the narrowest match of a String literal. This constructor then calls the overloaded constructor declared on line 5, resulting in value being updated to tacb. Control returns to the previous constructor, with line 10 updating value to tacbf, and making option C the correct answer. Note that if the constructor declared on line 8 did not exist, then the constructor on line 12 would match. Finally, the bear reference is properly cast to PolarBear on line 18, making the value parameter accessible.

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

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

C.  The code doesn't compile, so option A is incorrect. The first compilation error is on line 8. Since Rodent declares at least one constructor and it is not a no-argument constructor, Beaver must declare a constructor with an explicit call to a super() constructor. Line 9 contains two compilation errors. First, the return types are not covariant since Number is a supertype, not a subtype, of Integer. Second, the inherited method is static, but the overridden method is not, making this an invalid override. The code contains three compilation errors, although they are limited to two lines, making option C the correct answer.

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

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

A, G.  The compiler will insert a default no-argument constructor if the class compiles and does not define any constructors. Options A and G fulfill this requirement, making them the correct answers. The bird() declaration in option G is a method declaration, not a constructor. Options B and C do not compile. Since the constructor name does not match the class name, the compiler treats these as methods with missing return types. Options D, E, and F all compile, but since they declare at least one constructor, the compiler does not supply one.

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

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

B, E, F.  A class can only directly extend a single class, making option A incorrect. A class can implement any number of interfaces, though, making option B correct. Option C is incorrect because primitive variables types do not inherit java.lang.Object. If a class extends another class, then it is a subclass, not a superclass, making option D incorrect. A class that implements an interface is a subtype of that interface, making option E correct. Finally, option F is correct as it is an accurate description of multiple inheritance, which is not permitted in Java.

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

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

C.  The code does not compile because the isBlind() method in Nocturnal is not marked abstract and does not contain a method body. The rest of the lines compile without issue, making option C the only correct answer. If the abstract modifier was added to line 2, then the code would compile and print false at runtime, making option B the correct answer.

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

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

D.  The code compiles, so option G is incorrect. Based on order of initialization, the static components are initialized first, starting with the Arachnid class, since it is the parent of the Scorpion class, which initializes the StringBuilder to u. The static initializer in Scorpion then updates sb to contain uq, which is printed twice by lines 13 and 14 along with spaces separating the values. Next, an instance of Arachnid is initialized on line 15. There are two instance initializers in Arachnid, and they run in order, appending cr to the StringBuilder, resulting in a value of uqcr. An instance of Scorpion is then initialized on line 16. The instance initializers in the superclass Arachnid run first, appending cr again and updating the value of sb to uqcrcr. Finally, the instance initializer in Scorpion runs and appends m. The program completes with the final value printed being uq uq uqcrcrm, making option D the correct answer.

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

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

C, F.  Calling an overloaded constructor with this() may be used only as the first line of a constructor, making options A and B incorrect. Accessing this.variableName can be performed from any instance method, constructor, or instance initializer, but not from a static method or static initializer. For this reason, option C is correct, and option D is incorrect. Option E is tricky. The default constructor is written by the compiler only if no user-defined constructors were provided. And this() can only be called from a constructor in the same class. Since there can be no user-defined constructors in the class if a default constructor was created, it is impossible for option E to be true. Since the main() method is in the same class, it can call private methods in the class, making option F correct.

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

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

D, F.  The eat() method is private in the Mammal class. Since it is not inherited in the Primate class, it is neither overridden nor overloaded, making options A and B incorrect. The drink() method in Mammal is correctly hidden in the Monkey class, as the signature is the same and both are static, making option D correct and option C incorrect. The version in the Monkey class throws a new exception, but it is unchecked; therefore, it is allowed. The dance() method in Mammal is correctly overloaded in the Monkey class because the signatures are not the same, making option E incorrect and option F correct. For methods to be overridden, the signatures must match exactly. Finally, line 12 is an invalid override and does not compile, as int is not covariant with void, making options G and H both incorrect.

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

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

F.  The Reptile class defines a constructor, but it is not a no-argument constructor. Therefore, the Lizard constructor must explicitly call super(), passing in an int value. For this reason, line 9 does not compile, and option F is the correct answer. If the Lizard class were corrected to call the appropriate super() constructor, then the program would print BALizard at runtime, with the static initializer running first, followed by the instance initializer, and finally the method call using the overridden method.

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

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

E.  The program compiles and runs without issue, making options A through D incorrect. The fly() method is correctly overridden in each subclass since the signature is the same, the access modifier is less restrictive, and the return types are covariant. For covariance, Macaw is a subtype of Parrot, which is a subtype of Bird, so overridden return types are valid. Likewise, the constructors are all implemented properly, with explicit calls to the parent constructors as needed. Line 19 calls the overridden version of fly() defined in the Macaw class, as overriding replaces the method regardless of the reference type. This results in feathers being assigned a value of 3. The Macaw object is then cast to Parrot, which is allowed because Macaw inherits Parrot. The feathers variable is visible since it is defined in the Bird class, and line 19 prints 3, making option E the correct answer.

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

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

B, G.  Immutable objects do not include setter methods, making option A incorrect. An immutable class must be marked final or contain only private constructors, so no subclass can extend it and make it mutable, making option B correct. Options C and E are incorrect, as immutable classes can contain both instance and static variables. Option D is incorrect, as marking a class static is not a property of immutable objects. Option F is incorrect. While an immutable class may contain only private constructors, this is not a requirement. Finally, option G is correct. It is allowed for the caller to access data in mutable elements of an immutable object, provided they have no ability to modify these elements.

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

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

D.  The code compiles and runs without issue, making option E incorrect. The Child class overrides the setName() method and hides the static name variable defined in the inherited Person class. Since variables are only hidden, not overridden, there are two distinct name variables accessible, depending on the location and reference type. Line 8 creates a Child instance, which is implicitly cast to a Person reference type on line 9. Line 10 uses the Child reference type, updating Child .name to Elysia. Line 11 uses the Person reference type, updating Person.Name to Sophia. Lines 12 and 13 both call the overridden setName() instance method declared on line 6. This sets Child .name to Webby on line 12 and then to Olivia on line 13. The final values of Child .name and Person.Name are Olivia and Sophia, respectively, making option D the correct answer.

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

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

B.  The program compiles, making option F incorrect. The constructors are called from the child class upward, but since each line of a constructor is a call to another constructor, via this() or super(), they are ultimately executed in a top-down manner. On line 29, the main() method calls the Fennec() constructor declared on line 19. Remember, integer literals in Java are considered int by default. This constructor calls the Fox() constructor defined on line 12, which in turn calls the overloaded Fox() constructor declared on line 11. Since the constructor on line 11 does not explicitly call a parent constructor, the compiler inserts a call to the no-argument super() constructor, which exists on line 3 of the Canine class. Line 3 is then executed, adding q to the output, and the compiler chain is unwound. Line 11 then executes, adding p, followed by line 14, adding z. Finally, line 21 is executed, and j is added, resulting in a final value for logger of qpzj, and making option B correct. For the exam, remember to follow constructors from the lowest level upward to determine the correct pathway, but then execute them from the top down using the established order.

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

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

C.  The code compiles and runs without issue, making options E and F incorrect. First, the class is initialized, starting with the superclass Antelope and then the subclass Gazelle. This involves invoking the static variable declarations and static initializers. The program first prints 1, followed by 8. Then we follow the constructor pathway from the object created on line 14 upward, initializing each class instance using a top-down approach. Within each class, the instance initializers are run, followed by the referenced constructors. The Antelope instance is initialized, printing 24, followed by the Gazelle instance, printing 93. The final output is 182493, making option C the correct answer.

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

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

B, C.  Concrete classes are, by definition, not abstract, so option A is incorrect. A concrete class must implement all inherited abstract methods, so option B is correct. Concrete classes can be optionally marked final, so option C is correct. Option D is incorrect; concrete classes need not be immutable. A concrete subclass only needs to override the inherited abstract method, not match the declaration exactly. For example, a covariant return type can be used. For this reason, option E is incorrect.

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

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

D.  The classes are structured correctly, but the body of the main() method contains a compiler error. The Orca object is implicitly cast to a Whale reference on line 7. This is permitted because Orca is a subclass of Whale. By performing the cast, the whale reference on line 8 does not have access to the dive(int… depth) method. For this reason, line 8 does not compile, making option D correct.

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

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

相关文章

E114-经典赛题-操作系统服务端口扫描渗透测试

任务实施: E114-经典赛题-操作系统服务端口扫描渗透测试 任务环境说明: 服务器场景:p9_bt5-1(用户名:root;密码:toor) 服务器场景操作系统:Back Track five 192.168.1.120 服务…

【LeetCode刷题(数据结构与算法)】:验证二叉树的前序序列化

序列化二叉树的一种方法是使用 前序遍历 。当我们遇到一个非空节点时,我们可以记录下这个节点的值。如果它是一个空节点,我们可以使用一个标记值记录,例如 # 例如,上面的二叉树可以被序列化为字符串 “9,3,4,#,#,1,#,#,2,#,6,#,#…

Eclipse使用教程

一、前期准备 JDK环境变量得配置好(java需要先安装好) 【下载Eclipse解压包,可选择去Eclipse官网下载】 Eclipse的安装方式: 下载后解压直接点击进入选择工作区间就可运行 二、Eclipse基本概述: 工作区(w…

2023年全国职业院校技能大赛软件测试赛题第10套

2023年全国职业院校技能大赛 软件测试赛题第10套 目录 任务一 环境搭建及系统部署 任务二 单元测试 任务三 测试文档 任务四 功能测试 任务五 自动化测试 任务六 性能测试 任务七 接口测试 有问题可私信我 赛项名称: 软件测试 英文名称&…

LockSupport-park和unpark编码实战

package com.nanjing.gulimall.zhouyimo.test;import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport;/*** author zhou* version 1.0* date 2023/10/16 9:11 下午*/ public class LockSupportDemo {public static void main(String[] args) {…

ZC-CLS381RGB颜色识别+8x8点阵指示——WS2812点阵驱动(中)

文章目录 前言一、WS2812简介二、WS2812配置说明三、波形图绘制1.配置模块2.控制模块 总结 前言 RGB888点阵是一个由64个WS2812 RGB LED灯珠组成的点阵显示屏,可以用于艺术装饰、玩具和游戏、时钟计时器和状态指示器等各种场景。   本文将向各位读者展示&#xff…

漏洞复现--安恒明御安全网关文件上传

免责声明: 文章中涉及的漏洞均已修复,敏感信息均已做打码处理,文章仅做经验分享用途,切勿当真,未授权的攻击属于非法行为!文章中敏感信息均已做多层打马处理。传播、利用本文章所提供的信息而造成的任何直…

思维训练1

题目描述2 Problem - A - Codeforces ​ 题目分析 思路一: 由于是连续的数,我们可以使用f1, f2, f3来记录连续数的开头数字,只可能有三种情况 ①开头数为第一个数-1 ②开头数为第一个数 ③开头数为第一个数1 分别观察这三个情况&…

osWorkflow-1——osWorkflow官网例子部署启动简单使用(版本:OSWorkflow-2.8.0)

osWorkflow-1——osWorkflow官网例子部署启动简单使用(版本:OSWorkflow-2.8.0) 1. 前言——准备工作1.1 下载相关资料1.2 安装翻译插件 2. 开始搞项目2.1 解压 .zip文件2.2 简单小测(war包放入tomcat)2.3 导入项目到 I…

2023年信息科学与工程学院学生科协第一次软件培训

2023年信息科学与工程学院学生科协第一次软件培训 文章目录 2023年信息科学与工程学院学生科协第一次软件培训运行第一个代码(“Hello World!”)初识C语言代码的具体结构头文件(主)函数 输入与输出scanf函数printf函数转义字符 变量的定义进一…

如何选择全面型企业备份软件以提高数据可用性?

通常,备份数据是指服务器正在运行的工作负载所需的所有数据。这可以包括文档、媒体文件、配置文件、机器映像、操作系统和注册表文件。本质上,您想要保留的任何数据都可以存储为备份数据。 在日益数字化的商业环境中,数据备份对于组织的生存…

C语言复杂表达式与指针高级

一、指针数组与数组指针 1.指针数组VS数组指针 (1)指针数组:实质是一个数组,因为这个数组中传参的内容全部是指针变量。 (2)数组指针:实质是一个指针,这个指针指向一个数组 2.分析指…

C语言,打印指定大小的X

要打印一个X,无非是在一个二维数组一个矩形中操作,将不是X的部分赋值为空格字符,将是X部分打印为*字符。 矩形的边长就是输入的n,由于矩形的边长是不固定的,所以要找到应该被赋值为*的坐标之间有什么数学关系。 以矩…

宏电股份AI BOX新产品首次亮相2023中国移动全球合作伙伴大会,以创新性AI、5G技术推动数实共生

10月11-13日,2023中国移动全球合作伙伴大会在广州开幕,本次大会以“算启新程,智享未来”为主题,作为中国移动多年来的重要合作伙伴,宏电股份AI BOX新产品在大会上首次精彩亮相,并重点展示了5G Redcap工业智…

格雷码加相移三维重建

之前所做的三维重建系统基本上都是基于多频率外差 的方法 (交流方式可以点这个链接),最近整理了一下格雷码加相移的算法,与多频外差相比格雷码在对反光物体的重建效果稍微好一点,也可以投诉更少的图像 。采用6介互补格…

Python使用Selenium库如何绕过Cloudflare验证,网页请确认你是不是机器人

大家好,我是淘小白~ 前段时间使用selenium库写chatGPT的脚本,遇到过一个问题,那就是cloudflare的机器验证,让你点击确认不是机器人,这个问题最后找人解决掉了,我也是百度了很久没找到答案,B站找…

053:mapboxGL中sources的6种类型及各类型的示例代码

第053个 点击查看专栏目录 本篇文章是mapbox的source的归纳总结。 mapbox中 sources 是什么 sources:数据源集合(必填,用于包含一系列数据源 source,这些数据源提供了在地图上显示的数据) sources 是对象 {} 的形式,其属性名就是 数据源的名称(或者说 数据源的 id),…

网络基础初谈

0.一些无关紧要的心里话 ​ 一转眼学习计算机知识已经一年多了,中间起起伏伏,断断续续,但还算好也是坚持到了今天,之所以把这些基础知识写成一个系列,一方面方便知识巩固,另一方面至少还有三三两两的几个朋…

Linux命令之chpasswd命令

一、chpasswd命令简介 chpasswd命令用于同时更改多个用户的密码。它可以从标准输入或指定的文件中读取用户名和密码的组合,并将其应用于系统中的用户。chpasswd命令通常用于批量更改用户密码,特别是在自动化脚本或批处理任务中,该命令需要roo…

ERP系统供应商协同:优化企业供应链管理

一、ERP系统供应商协同的概念和功能 供应商协同是指在供应链中,企业与供应商之间通过ERP系统进行紧密合作和信息共享,实现供应链各个环节的协调和优化。ERP系统供应商协同功能涉及以下方面: 1. 供应商管理:ERP系统提供完善的供应…