To ExamInfo

Review Questions for Final Exam

  1. Draw the reference diagram and predict the output:

  2. What does this statement mean? "To write an effective recursive algorithm, one must think like an executive."

    Ans: You must get the recursive calls (your subordinates) to do most of the work. The executive only does the nonrecursive work in the call, which is usually relatively easy.

  3. What is wrong with this constructor?

    Ans: It makes a recursive call to itself, which will form an infinite chain of recursions.

  4. If a is an array defined by What is the output of f(5) if f is defined by

    Ans: The output is 3 1 5 7 3 5.

  5. What does finally mean?

    Ans: It is the part of a try/catch block that is always executed, whether or not an exception occurs:

  6. What does this mean? Using throws is lazier than using try and catch.

    Ans: It is always easier just to throw the exception (let it occur) than it is to write the code for the catch block.

  7. Name as many exceptions as you can.

    Ans: Look at the list of exceptions in the Final Review Guide.

  8. What is the output?

    Ans: 5456

  9. How would you read gender (char) as a command line argument?

    Ans: char gender = args[0].charAt(0);

  10. How would you read gpa (double) as a command line argument?

    Ans: double gpa = Double.parseDouble(args[0]);

  11. Suppose that the interface C is defined by
    What must you do to implement this interface?

    Ans: (1) Use implements C in the first line of the class, (2) Provide definitions of the functions f and g that have the same signatures of the f and g in the interface.

  12. Given the array a defined in Problem 5, write as little code as possible to print the elements of the array in descending sorted order. Write as little code as possible.

    Ans:

  13. Write a loop to remove all of the items from a Stack s of Object objects. Recall that the Stack methods are push, pop and isEmpty.

  14. Suppose that the Person class has instance variables name, gender, and age. Write an equals method for this class. Two persons are equal if they have the same gender.

    Ans:

  15. What does it mean when one variable shadows another variable?

    Ans: It means that a local variable is defined with the same name as an instance variable. The local variable hides the instance variable unless the instance variable is prefixed with the this reference.