SampleExam07(Ch10, 1..

advertisement
Name:_______________________ CSCI 1302 OO Programming
Armstrong Atlantic State University
(50 minutes)
Instructor: Y. Daniel Liang
Covers Chapters 10-11
Part I: Multiple Choice Questions:
1.
a.
b.
c.
d.
e.
A subclass inherits _____________ from its superclass.
private method
protected method
public method
a and c
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("b is an instance of A");
if (b instanceof C)
System.out.println("b is an instance of C");
}
}
a.
b.
c.
d.
3.
a.
b.
c.
d.
Nothing.
b is an instance of A.
b is an instance of C.
b is an instance of A followed by b is an instance of C.
When you implement a method that is defined in a superclass, you
__________ the original method.
overload
override
copy
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 "A";
}
}
class B {
public String toString() {
return "B";
}
}
a.
b.
c.
d.
e.
AB
BA
AA
BB
None of above
5. What is the output of running class C?
class A {
public A() {
System.out.println(
"The default constructor of A is invoked");
}
}
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("The constructor of B is invoked");
}
}
a.
b.
c.
d.
none
"The
"The
"The
"The
constructor of B is
default constructor
constructor of B is
default constructor
invoked"
of A is invoked"
invoked"
of A is invoked"
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;
}
}
}
2
a.
The program has a syntax error because Test1 does not have a main
method.
b.
The program has a syntax error because o1 is an Object instance
and it does not have the compareTo method.
c.
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.
b.
c.
d.
private double xMethod(int x) {…}
protected int xMethod(double x) {…}
public double xMethod(double x) {…}
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.
b.
c.
d.
Remove abstract in the setCode method declaration.
Change protected to public.
Add abstract in the class declaration.
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());
}
3
}
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.
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.
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.
13. The __________ class is inherited by every Java class.
a.
b.
c.
d.
Class
Object
Number
Comparable
Part II: Trace Programs
(2 pts) The java.util.Date class implements
java.lang.Cloneable and overrides the equals method to
4
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;
System.out.println("(1) " + (d1 == d2));
System.out.println("(2) " + (d1 == d3));
System.out.println("(3) " + d1.equals(d2));
System.out.println("(4) " + d1.equals(d3));
}
}
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:
public class Hexagon extends GeometricObject implements Comparable {
private double side;
/** Construct a Hexagon with the specified side */
public Hexagon(double side) {
// Implement it
5
}
/** Implement the abstract method findArea in
GeometricObject */
public double findArea() {
// Implement it ( 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
Key
Part I: Multiple Choice Questions: (1 pts each)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
e
d
b
a
c
e
d
c
c
b
d
c
b
Part II:
(1)
(2)
(3)
(4)
false
true
false (because they are created at different times)
true
7
Download