Name______

CS162 ~ FINAL EXAM

Please create a folder called <lastNameFirstName>FinalExam, wher you use your actual last name and first name (mine would be JessJosephFinalExam).

Do your best to answer each of the following questions:

  1. (12 pts) Write a program that includes a method that you create called fact that accepts an int as an argument, recursively calculates the factorial of that argument and returns that value (do not print along the way). Create a main that asks the user for a value, uses your method to calculate the factorial of that value, and then prints out the factorial of that input value (not each intermediate step along the way).
  1. (18 pts) Write a program that includes a method that takes an array of String objects as an argument and sorts the array in ascending order (smaller values to larger values)(do not print the values along the way). Create a main that asks the user for 5 names, fills in an array of String, sorts that array using your newly created sort method, and then prints the values of the array to the string each on a new line.

(5 pts) MORE Extra Credit: Write the sorted array to a file named SortedNames.dat, in the same folder

3. (5 pts) Draw a class hierarchy diagram for the various creatures in the biological subfamily of caninae, including at least three genera (plural of genus) of the subfamily (explicitly including canis), and at least three species in each of those genus (explicitly including the species Canis lupus); you may choose if you wish to include more branches on the parent side of the diagram (canidae and higher) and you may choose if you wish to include info on tribes and such from biological taxonomy (if you are not familiar much with biological taxonomy, then just focus on species, genus, and subfamily). (not the detailed UML)

4. (3 pts) Assume I ran the following code:
Animal[] zoo = new Animal[10];
zoo[0] = new Canis(“lupus”);
What is the data type of the actual object stored at zoo[0]?
Is it Animal, Canis, lupus, or something else?

  1. (4 pts) If the class Canis contained a method called howl() would I be able to call it on the zoo[0] shown above? Explain why or why not.
  1. (4 pts) When using inheritance, explain what it means to “override” a method?
  1. (3 pts) How many catch clauses are there possible for a single try? Explain your answer.
  1. (4 pts) Explain how a staticvariableis different from and similar to other instance variables?
  1. (3 pts) When the modifier “final” is used with a method in a parent class, how does this affect any child classes?
  1. (10 pts) Sort the following array of numbers into ascending order using Selection Sort.Show the list at each intermediate stage (trace the movement of values):

Array [0] = 12

Array [1] = 9

Array [2] = 7

Array [3] = 999

Array [4] = 5

Array [5] = 1

Array [6] = 19

Array [7] = 24

14. (3 pts) Write the code to declare a timer object and set the timer to go off every 150 milliseconds. It should use the listener MyTimeListener. Then, write the code to get your timer started.

15.(4 pts) Explain what happens in memory when the following line of code is executed.

String[] Names = new String[12];

  1. what is stored (if anything) on the stack?
  2. what else is stored (if anything) and where is it stored?
  3. what is stored (if anything) at Names[4]?
  4. how much memory (if any) has actually been allocated (you may find conflicting answers to this in a brief internet search)?

16. (3 pts) Explain what an initializer list is, and write an example of using one.

17. (6 pts) Describe the difference between a linear search and a binary search in terms of what is being done with the data. Is one more efficient than the other? If so, then which one is more efficient and how much difference is there in efficiency?

18. (4 pts) Using recursive programming, explain a) what a base case is and b) why it is necessary to have one.

19.(3 pts) How is the protected visibility modifier different from the public and private modifiers? Are there any other visibility modifiers available?

20. (3 pts) Explain the difference between direct and indirect recursion

21. (4 pts)
a) What is a mnemonic in a GUI?

b) Given a button object named HitMe, write the code to add a mnemonic for the letter H to the button.

22. (4 pts) What is the difference between checked and unchecked exceptions? What must the programmer do differently if an exception is defined as checked?

Page 1