1
Name:_______________________
(50 minutes)
CSCI 1302 OO Programming
Georgia Southern University (Armstrong)
Instructor: Dr. Y. Daniel Liang
I pledge by honor that I will not discuss this exam with anyone until my
instructor reviews the exam in the class.
Signed by ___________________ Date _____________________
Part I:
A. (2 pts)
What is wrong in the following code?
public class Test {
}
What is wrong in the following code?
public class Test {
}
B. (3 pts)
Suppose that statement2 causes an exception in the
following trycatch block:
statement4;
}
Answer the following questions:
Will statement3 be executed?
2
C. (2 pt)
Why does the following method have a compile error?
d. (2 pt)
Why is the following code incorrect for storing the content
of object?
import java.io.*;
}
Part II: Write Programs
(5 pts) Write a program that stores an array of the five int values 1, 2, 3, 4 and 5, a Date object
for the current time, and the double value 5.5 into the file named Test.dat.
3
(10 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:
}
@Override
public double getPerimeter() {
// Implement it
4
5
Part III: Multiple Choice Questions: (1 pts each)
(Please circle your answers on paper first. After you
finish the test, enter your choices online to LiveLab. Log
in and click Take Instructor Assigned Quiz. Choose Quiz2.
You have 5 minutes to enter and submit the answers.)
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);
#
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”);
}
}
6
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();
c. C b = new A();
d. B b = new B();
e. C c = new B();
#
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”
}
}
7
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.
#
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);
}
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.
8
d. None of the above.
#
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;
}
}
#
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”)
#
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 {
9
}
}
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
Please double check your answer before clicking the Submit
button. Whatever submitted to LiveLab is FINAL and counted
for your grade.
Have you submitted your answer to LiveLib? ______________