Exam6(Ch9, 10)

更新时间:2023-11-16 04:44:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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 (

本文来源:https://www.bwwdw.com/article/qmhv.html

Top