Midterm Exam

advertisement
COSC 211: Programming Data Structures
Fall 2012: Midterm Exam
100 pts.
Please print the following clearly:
Name: _______________________________________
EID #: ______________
All standard exam policy rules apply. Do your own work. Do not use electronic
devices during the exam. Don’t forget to put your name on the exam. Answer only two of
the questions in the programming exercises section (your choice.) Good Luck!
True/False (10 pts)
1. T
F
You can use super to access a base method that has been overridden. T
2. T
F
You can use super to access a private base method. F
3. T
F
The three main methods of object oriented programming are
encapsulation, inheritance, and polymorphism. T
4. T
F
Late binding is the process by which the compiler assigns values to
dynamic variables after runtime.
F
5. T
F
When using a BufferedReader, in the case of a text file, when reading
beyond the end of a file, an EOF exception is always thrown. F
6. T
F
When using a BufferedReader, in the case of a binary file, when reading
beyond the end of a file, an EOF exception is always thrown. T
7. T
F
Exams are awesome.
8. T
F
A recursive method is a method that includes a call to itself. T
9. T
F
Any algorithm that can be written recursively can be written in a nonrecursive manner. T
10. T
F
Recursive methods are always faster and more efficient than iterative
methods. F
1
Multiple Choice (5pts each)
11. Circle those of the following that are true.
AB
a.
b.
c.
d.
Friendly access is the same as default access
Package access is the same as friendly access
Private variables and methods can be inherited by derived classes
Protected methods can be inherited by derived classes, but no protected variables
can be
e. A private method can be accessed outside its class, so long as the class itself is
declared to be public.
12. Circle those of the following that are true.
a.
b.
c.
d.
e.
CD
Overriding is just another term for overloading
Overloading only can be done within an inheritance structure
Overriding only can be done within an inheritance structure
You can overload a constructor
You can override a constructor
13. Circle which of the following is true: D
a. An abstract method can be declared private, so long as you provide a body for it
b. An abstract method can be declared protected so long as you also declare it to be
static
c. You can use the abstract modifier on both variables and methods
d. An abstract method cannot be declared to be private
14. Circle which of the following is true: D
a. If an exception is thrown within a try block, the finally clause always executes.
b. If no exception is thrown within a try block, the finally clause always executes.
c. If an exception is thrown within a finally block, the exception is propagated out of
the finally block, and the finally block does not complete its execution.
d. All of the above.
15. Circle those of the following that are true: All
a. Unchecked exceptions should be handled by the code you write.
b. Checked exceptions must be handled by code that you write.
c. You don’t need to provide a catch clause for a checked exception if you provide a
throws clause.
d. Every Java program has the potential for throwing an unchecked exception.
2
16. Circle those of the following that are true: AC
a.
b.
c.
d.
If a method may throw an exception, it must either catch it or use a throws clause.
If a method may throw an exception, it must catch it.
A method may include a throws clause for an exception that it does not throw.
A method may not include a throws clause unless it actually throws an uncaught
exception.
Short answer (10 pts. each)
17. Define the words inheritance, superclass, and subclass.
See text
18. What is an exception handler? How does it work?
Something about try-catch blocks and catching exception types.
19. Explain why it is a good idea to close a file to which you have written, even though Java
will close it for you when your program ends.
Something about avoiding data errors due to poorly/extra written data
3
Programming Exercises (15 pts. each, choose two of the four)
20. Write the Java code to define the following class tree structure.
Assume Encyclopaedia is some predefined class that acts the upper most ancestor class,
and that all the other classes derive from it (or from its decendants.) Use Java inheritance
techniques to create classes for Science, Culture, Art, and Craft. Don’t worry about the
constructors or any enclosed methods/variables.
4
21. What does the following code print out? Why? Back your answer up with examples
from the code. ACEGI
5
22. Write out the Java code necessary to open a file and read a line using a BufferedReader.
Don’t forget to handle the IOException and the FileNotFound Exception.
See book
6
23. Write a method to calculate factorial recursively.
𝟏
𝒊𝒇 𝒏 = 𝟎
Remember that 𝒏! = {
(𝒏 − 𝟏)! ∗ 𝒏 𝒊𝒇 𝒏 > 𝟎
In other terms: 5! = 5 * 4 * 3 * 2 *1 = 120
(HINT: The stopping and recursive cases can be found in the first equation)
int factorial (int n) {
if (n = 0) {
return 1;
}else if(n > 0) {
return factorial(n-1);
}else{
//Error case. Quit here or return an error
}
}
7
Extra Credit (10 pts.)
Write a method to calculate the Fibonacci sequence recursively.
Remember that the first two numbers in the Fibonacci sequence are 0 and 1 and each
subsequent Fibonacci number is the sum of the previous two.
In mathematical terms:
𝟎
𝒇𝒊𝒃(𝒏) = { 𝟏
𝒇𝒊𝒃(𝒏 − 𝟏) + 𝒇𝒊𝒃(𝒏 − 𝟐)
𝒊𝒇 𝒏 = 𝟎
𝒊𝒇 𝒏 = 𝟏
𝒊𝒇 𝒏 > 𝟏
8
Download