Exam6(Ch9, 10)
更新时间:2023-11-16 04:44:01 阅读量: 教育文库 文档下载
- exam678678推荐度:
- 相关推荐
Name:_______________________ Introduction to Programming Harbin Normal University (50 minutes) Instructor: Fullway Powered By: Y. Daniel Liang
Part I: Multiple Choice Questions:
1. A subclass inherits _____________ from its superclass. a. private method b. protected method c. public method d. a and c e. b and c
2. Show the output of running the class Test in the following code:
interface A { void print(); }
class C {}
class B extends C implements A { public void print() { } }
public class Test {
public static void main(String[] args) { B b = new B();
if (b instanceof A)
System.out.println(\ if (b instanceof C)
System.out.println(\ } }
a. Nothing. b. b is an instance of A. c. b is an instance of C. d. b is an instance of A followed by b is an instance of C.
3. When you implement a method that is defined in a superclass, you __________ the original method.
a. overload b. override c. copy d. call
4. What is the output of running the class C.
public class C {
public static void main(String[] args) { Object[] o = {new A(), new B()}; System.out.print(o[0]); System.out.print(o[1]); } }
1
class A extends B {
public String toString() { return \ } }
class B {
public String toString() { return \ } }
a. AB b. BA c. AA d. BB
e. None of above
5. What is the output of running class C?
class A {
public A() {
System.out.println(
\ } }
class B extends A { public B(String s) {
System.out.println(s); } }
public class C {
public static void main(String[] args) {
B b = new B(\ } }
a. none
b. \
c.
\\
d. \
6. Analyze the following code:
public class Test1 {
public Object max(Object o1, Object o2) { if ((Comparable)o1.compareTo(o2) >= 0) { return o1; }
else {
return o2; } } }
a.
The program has a syntax error because Test1 does not have a main method.
2
b. c. The program has a syntax error because o1 is an Object instance and it does not have the compareTo method.
The program has a syntax error because you cannot cast an Object instance o1 into Comparable. d.
The program would compile if ((Comparable)o1.compareTo(o2) >= 0) is replaced by (((Comparable)o1).compareTo(o2) >= 0). e. b and d are both correct.
7. The method _____ overrides the following method: protected double xMethod(int x) {…}; a. private double xMethod(int x) {…} b. protected int xMethod(double x) {…} c. public double xMethod(double x) {…} d. public double xMethod(int x) {…}
8. Which of the following possible modifications will fix the errors in this code?
public class Test { private double code;
public double getCode() { return code; }
protected abstract void setCode(double code); }
a. Remove abstract in the setCode method declaration. b. Change protected to public. c. Add abstract in the class declaration. d. b and c.
9. Analyze the following code.
class Test {
public static void main(String[] args) { Object x = new Integer(2);
System.out.println(x.toString()); } }
a. The program has syntax errors because an Integer object is assigned to x. b. When x.toString() is invoked, the toString() method in the Object class is used. c. When x.toString() is invoked, the toString() method in the Integer class is used. d. None of the above.
10. Analyze the following code.
class Test {
public static void main(String[] args) { Object x = new Integer(2);
System.out.println(x.doubleValue()); } }
a. The program has syntax errors because an Integer object is assigned to x.
b. The program has syntax errors because doubleValue() is not a method in the Object class. c. The program compiles and runs fine. d.
None of the above.
3
11. Analyze the following code.
class Test {
public static void main(String[] args) { Inner inner = new Inner(); System.out.println(inner.k); }
private class Inner { protected int k; } }
a. The program has a syntax error because the Inner class does not have a constructor and you cannot create an object from it. b. The program has a syntax error because the Inner class is private and it cannot be accessed in the main method . c. The program has a syntax error because k is protected in the Inner class and it cannot be accessed in the main method. d. The program has a syntax error because the Inner class is not static and it cannot be used to create an object in the main method. 12. What is the best suitable relationship between House and Address?
a. Association b. Aggregation c. Composition d. Inheritance 13. What is the best suitable relationship between Student and Course?
a. Association b. Aggregation c. Composition d. Inheritance 14. The relationship between the Number class and the Integer class is ________.
a. Association b. Aggregation c. Composition d. Inheritance 15. Analyze the following code.
Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef;
a. You cannot assign an Integer object into a variable of the Number type. b. The compiler detects that numberRef is not an instance of Double.
c. A runtime class casting exception occurs, since numberRef is not an instance of Double. d. The program runs fine, since Integer is a subclass of Double.
e. You can convert an int to double, so you can cast an Integer instance to a Double instance.
16. The __________ class is inherited by every Java class.
a. Class
4
b. Object c. Number d. Comparable
Part II: Trace Programs
(2 pts) The java.util.Date class implements java.lang.Cloneable and overrides the equals method to return true if two objects have the same date and time. Show the output of the following code.
import java.util.*;
public class Test extends Object {
public static void main(String[] args) { Date d1 = new Date();
Date d2 = new Date(349324); Date d3 = d1;
Date d4 = (Date)d1.clone();
System.out.println(\ System.out.println(\ System.out.println(\ System.out.println(\ } }
Part III: Write Programs
(5 pts) Write a method to find the max in an array of comparable objects (Assume that the objects in the argument are instances of Comparable). The method signature is as follows:
public static Object max(Object[] a)
(5 pts) Write a class named Hexagon that extends GeometricObject and implements the Comparable
interface. Assume all six sides of the hexagon are of equal size. The Hexagon class is defined as follows:
5
public class Hexagon extends GeometricObject implements Comparable { private double side;
/** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it }
/** Implement the abstract method findArea in GeometricObject */
public double findArea() {
area?3*3*side*side) }
/** Implement the abstract method findPerimeter in GeometricObject */
public double findPerimeter() { // Implement it }
/** Implement the compareTo method in the Comparable interface to */ public int compareTo(Object obj) {
// Implement it (compare two Hexagons based on their areas) } }
6
// Implement it (
public class Hexagon extends GeometricObject implements Comparable { private double side;
/** Construct a Hexagon with the specified side */ public Hexagon(double side) { // Implement it }
/** Implement the abstract method findArea in GeometricObject */
public double findArea() {
area?3*3*side*side) }
/** Implement the abstract method findPerimeter in GeometricObject */
public double findPerimeter() { // Implement it }
/** Implement the compareTo method in the Comparable interface to */ public int compareTo(Object obj) {
// Implement it (compare two Hexagons based on their areas) } }
6
// Implement it (
正在阅读:
Exam6(Ch9, 10)11-16
中南大学C语言报告612-21
2019届高三数学理一轮复习课时跟踪检测五十六 排列与组合重点高中 含解析 精品09-12
浙江全面推行新型居住证制度了04-19
高三地理高考第一轮单元复习 人类的居住地与地理环境 105-23
大学校园主持人大赛主题活动策划完整方案11-27
关于“城市空间发展战略研究”的思考04-26
2018年中国高拍仪扫描仪市场现状调研与发展前景分析报告目录03-03
《管理信息系统》电子教案05-04
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- Exam6
- Ch
- 10
- 班级誓词汇总
- 二级建造师《市政工程》城镇道路工程典型例题详解
- 2013最新经典个人简历模板—word版可编辑(二十八)
- 东师2015春季 管理学基础(新)(在线作业二)答案
- 《红楼梦》阅读测试
- 安徽省体育局深入学习实践科学发展观活动简报第13期
- 二年级上长度单位练习题
- 南开15春学期《C语言程序设计》在线作业 满分答案
- 动物的冬眠是一种奇妙的现象 - 图文
- 材料力学4套模拟试题及答案
- 物流运输名词解释
- 车间消毒规程 - 图文
- 计算机基础模块练习(4)
- 香菇废弃菌渣利用研究现状
- 洛阳牡丹导游词
- 国民收入核算习题及答案
- 当代发展型社会政策研究的新进展及其理论贡献
- 工程经济学复习题10年
- 2013兰州大学网络教育计算机基础100分试题
- 关于解放区政权的研究