1
Name:_______________________
Covers chs9-17
Final Exam
CSCI 1302 Introduction to Programming
Armstrong Atlantic State University
Instructor: Dr. Y. Daniel Liang
Please note that the university policy prohibits giving the exam score by email. If you need to know your
final exam score, come to see me during my office hours next semester.
I pledge by honor that I will not discuss the contents of this exam with
anyone.
Signed by ___________________ Date ___________________
1. Design and use interfaces (10 pts)
Write a class named Octagon that extends GeometricObject
and implements the Comparable and Cloneable interfaces.
2
2. Design and create GUI applications (10 pts)
Write a Java GUI program to add two numbers from text fields, and
3
3. Text I/O (10 pts)
Write a program that will count the number of characters (excluding
control characters ‘\r’ and ‘\n’), words, and lines, in a file. Words
4
5. Multiple Choice Questions: (1 pts each)
(1. Mark your answers on the sheet. 2. Login and click Take
Instructor Assigned Quiz for QFinal. 3. Submit it online
within 5 mins. 4. Close the Internet browser.)
1. _________ describes the state of an object.
a. data fields
b. methods
c. constructors
d. none of the above
#
2. An attribute that is shared by all objects of the class is coded
using ________.
a. an instance variable
b. a static variable
c. an instance method
d. a static method
#
3. If a class named Student has no constructors defined explicitly,
the following constructor is implicitly provided.
a. public Student()
#
4. If a class named Student has a constructor Student(String name)
defined explicitly, the following constructor is implicitly provided.
a. public Student()
b. protected Student()
c. private Student()
d. Student()
e. None
#
5. Suppose the xMethod() is invoked in the following constructor in
a class, xMethod() is _________ in the class.
public MyClass() {
xMethod();
}
5
xMethod();
}
a. a static method
b. an instance method
c. a static or an instance method
#
7. What would be the result of attempting to compile and
run the following code?
public class Test {
static int x;
public static void main(String[] args){
System.out.println(“Value is ” + x);
}
}
a. The output “Value is 0” is printed.
b. An “illegal array declaration syntax” compiler error occurs.
c. A “possible reference before assignment” compiler error occurs.
d. A runtime error occurs, because x is not initialized.
#
8. Analyze the following code:
public class Test {
private int t;
a. The variable t is not initialized and therefore causes errors.
b. The variable t is private and therefore cannot be accessed in the
main method.
c. Since t is an instance variable, it cannot appear in the static
main method.
d. The program compiles and runs fine.
#
9. Suppose s is a string with the value “java”. What will be
assigned to x if you execute the following code?
char x = s.charAt(4);
a. ‘a’
b. ‘v’
6
public static void main(String[] args) {
int[] x = new int[3];
System.out.println(“x[0] is “+x[0]);
}
}
a. The program has a syntax error because the size of the array
wasn’t specified when declaring the array.
#
11. How can you get the word “abc” in the main method from the
following call?
java Test “+” 3 “abc” 2
a. args[0]
b. args[1]
c. args[2]
d. args[3]
#
12. Which code fragment would correctly identify the number of
arguments passed via the command line to a Java application,
excluding the name of the class that is being invoked?
a. int count = args.length;
b. int count = args.length 1;
c. int count = 0; while (args[count] != null) count ++;
d. int count=0; while (!(args[count].equals(“”))) count ++;
#
13. Show the output of running the class Test in the following code
lines:
interface A {
void print();
}
class C {}
7
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.
#
14. When you implement a method that is defined in a superclass, you
__________ the original method.
a. overload
b. override
c. copy
d. call
}
}
class B extends A {
a. none
b. “The default constructor of B is invoked”
c. “The default constructor of A is invoked” followed by “The
default constructor of B is invoked”
d. “The default constructor of A is invoked”
8
Have you submitted your answer to LiveLib? ______________