1
Name:_______________________
(50 minutes)
CSCI 1302 OO Programming
Armstrong Atlantic State University
Instructor: Y. Daniel Liang
Part I:
(A) (a) The program has a syntax error because x does not have the compareTo
method.
(b) The program has a syntax error because the member access operator (.) is
executed before the casting operator.
(B) Will statement3 be executed?
If the exception is not caught, will statement4 be executed?
If the exception is caught in the catch clause, will statement4 be
executed?
(C) The method throws a checked exception. You have to declare to throw the
exception in the method header.
(d)
Test is not Serializable.
Part II:
1.
import java.io.*;
public class Exercise17_05 {
public static void main(String[] args) throws IOException
{
2
}
}
}
2.
public class Hexagon extends GeometricObject implements
Comparable<Hexagon>, Cloneable {
private double side;
@Override
public int compareTo(Hexagon obj) {
// Implement it (compare two Hexagons based on their sides)
if (this.side > obj.side)
return 1;
else if (this.side == obj.side)
return 0;
else
return 1;
3
Draw UML diagram omitted in the solution.
Part III: Multiple Choice Questions:
1. The output from the following code is __________.
java.util.ArrayList<String> list = new java.util.ArrayList<>();
list.add(“New York”);
java.util.ArrayList<String> list1 =
(java.util.ArrayList<String>)(list.clone());
list.add(“Atlanta”);
list1.add(“Dallas”);
System.out.println(list);
a. [New York]
b. [New York, Atlanta]
c. [New York, Atlanta, Dallas]
d. [New York, Dallas]
#
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. 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. Suppose C is an interface, B is an abstract class that partially
implements C, and A is a concrete class with a default constructor that
extends B. Which of the following is correct?
a. A a = new C();
b. A a = new B();
4
c. C b = new A();
d. B b = new B();
e. C c = new B();
#
4. Which of the following is correct?
a. An abstract class does not contain constructors.
b. The constructors in an abstract class should be protected.
c. The constructors in an abstract class are private.
d. You may declare a final abstract class.
e. An interface may contain constructors.
#
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. none
b. “The constructor of B is invoked”
c. “The default constructor of A is invoked” “The constructor of B
is invoked”
d. “The default constructor 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;
}
}
}
5
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. Which of the following statements regarding abstract methods is not
true?
a. An abstract class can have instances created using the constructor
of the abstract class.
b. An abstract class can be extended.
c. A subclass of a nonabstract superclass can be abstract.
d. A subclass can override a concrete method in a superclass to declare
it abstract.
e. An abstract class can be used as a data type.
#
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.
6
c. When x.toString() is invoked, the toString() method in the
Integer class is used.
#
10. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = new Object();
String d = (String)o;
}
}
a. ArithmeticException
b. No exception
c. StringIndexOutOfBoundsException
d. ArrayIndexOutOfBoundsException
e. ClassCastException
#
11. What exception type does the following program throw?
public class Test {
public static void main(String[] args) {
Object o = null;
System.out.println(o.toString());
}
}
a. ArrayIndexOutOfBoundsException
b. ClassCastException
c. NullPointerException
d. ArithmeticException
e. StringIndexOutOfBoundsException
#
12. To append data to an existing file, use _____________ to construct a
FileOutputStream for file out.dat.
a. new FileOutputStream(“out.dat”)
b. new FileOutputStream(“out.dat”, false)
c. new FileOutputStream(“out.dat”, true)
d. new FileOutputStream(true, “out.dat”)
#
7
13. After the following program is finished, how many bytes are written to the file t.dat?
import java.io.*;
public class Test {
public static void main(String[] args) throws IOException {
DataOutputStream output = new DataOutputStream(
new FileOutputStream(“t.dat”));
output.writeInt(1234);
output.writeShort(5678);
output.close();
}
}
a. 2 bytes.
b. 4 bytes.
c. 6 bytes.
d. 8 bytes.
e. 12 bytes
#
14. Which of the following statements is not true?
a. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in
addition for primitive type values and strings.
b. Since ObjectInputStream/ObjectOutputStream contains all the functions of
DataInputStream/DataOutputStream, you can replace
DataInputStream/DataOutputStream completely by
ObjectInputStream/ObjectOutputStream.
c. To write an object, the object must be serializable.
d. The Serializable interface does not contain any methods. So it is a mark interface.
e. If a class is serializable, all its data fields are seriablizable.