CIS 260 Final Review

This review doesn't necessarily have all the topics covered on the final exam, but it does cover most of what will be on the exam. If you know this material, then you should be set.

Show all answers     Hide all answers

    Objects

  1. When are objects created? Answer For most objects, they are created when you use the keyword: new. Java has a special case for String objects to make them easier to use. A quoted string inside source code is automatically converted into a String constructor.
  2. When are reference variables created? Answer Reference variables are created when they are declared. If they are static variables, they are created when the class they are in is first loaded into memory. If they are instance variables, then they are created when an instance (object) of their class is created.
  3. Describe inheritance. Answer Inheritance is when one class extends another. The subclass (derived class) is the class that extends the superclass (base class). The subclass inherits (and therefore has) the data and methods of the superclass. Even though the subclass contains the data and methods, it might not be able to access them since the superclass may have made some of those members private access.
  4. Describe composition. Answer Composition is when an object has other objects as data members. This is very common. You have probably written many objects that contain either an instance or static String variable. That would be a simple example of composition.
  5. What is a subclass? Answer A class which is based on (extends) another class.
  6. What is a superclass? Answer A class which another class is based on (extends).
  7. Concatenation

  8. When is the toString() method called? Answer
    1. when it is explicitly called: obj.toString()
    2. when an object is being concatenated to a string: "" + obj becomes "" + obj.toString()
    3. when a non-string is sent to a PrintWriter: System.out.println(obj) becomes System.out.println(obj.toString())
  9. When is addition performed instead of concatenation? Answer Addition is performed when both operands are internally numbers. (byte, short, int, long, float, double, char)
  10. What happens if an object does not have a toString() method? Answer That doesn't happen since the Object class has a toString method and all other objects in Java (other than Object instances) have Object as their ultimate base class.
  11. Threads

  12. How do you start a thread? Answer You call its start() method.
  13. When does a thread end? Answer A thread ends when its run() method ends or it is interrupted.
  14. How can you pause a thread? Answer You can pause execution of a thread using the Thread.sleep method.
  15. What exception(s) do you have to watch for when using a thread? Answer InterruptedException
  16. String methods

  17. Create a new String in variable s3 which contains String s1 and String s2 concatenated. Answer String s3 = s1 + s2; or
    String s3 = s1.concat(s2);
  18. Create a lower case version of a string in variable s. Answer s.toLowerCase() [Note: this does not modify s]
  19. Create an upper case version of a string in variable s. Answer s.toUpperCase() [Note: this does not modify s]
  20. Compare the strings s1 and s2. Answer s1.compareTo(s2) [Note: this does not modify s1 or s2]
  21. Compare the strings s1 and s2 ignoring case. Answer s1.compareToIgnoreCase(s2) [Note: this does not modify s1 or s2]
  22. Test the strings s1 and s2 for equality. Answer s1.equals(s2) [Note: this does not modify s1 or s2]
  23. Test the strings s1 and s2 for equality ignoring case. Answer s1.equalsIgnoreCase(s2) [Note: this does not modify s1 or s2]
  24. Place the character in the fifth position in string s into a new char variable named ch. Answer char ch = s.charAt(4);
  25. Applets

  26. What three methods are guaranteed to be called when an applet starts up? List them in order. Answer
    1. public void init()
    2. public void start()
    3. public void paint(Graphics g)
  27. Data structures

  28. Add 1 and 2 to an empty stack and then remove one item. What is left on the stack? Answer 1
  29. Add 1 and 2 to an empty queue and then remove one item. What is left on the queue? Answer 2
  30. Add 1 and 2 to an empty linked list. Draw a diagram of the resulting linked list? Answer head -> 1 -> 2 -> null or
    head -> 2 -> 1 -> null
  31. List common operations on a binary tree in increasing order of difficulty. Answer
    1. traverse (inorder, preorder, postorder)
    2. find minimum or maximum value in the tree
    3. find/search
    4. insert
    5. delete
  32. List common operations on a binary tree in increasing order of difficulty. Answer
    1. find minimum or maximum value in the tree (about the same as traverse)
    2. traversal (about the same as finding minimum or maximum)
    3. find/search
    4. insert
    5. delete
  33. Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show what the tree looks like. Answer
          5
         / \
        1   7
       / \   \
      0   3   9
         / \
        2   4
  34. Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the inorder traversal of that tree. Answer 0, 1, 2, 3, 4, 5, 7, 9
  35. Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the preorder traversal of that tree. Answer 5, 1, 0, 3, 2, 4, 7, 9
  36. Add 5, 7, 1, 3, 2, 4, 0, 9 to an empty binary search tree. Show the postorder traversal of that tree. Answer 0, 2, 4, 3, 1, 9, 7, 5
  37. Where is the minimum value located in a binary search tree? Answer As far to the left from the root as possible.
  38. Where is the maximum value located in a binary search tree? Answer As far to the right from the root as possible.
  39. If you deleted the 1 in the tree used above, which node would take its place? Answer 2 (the successor), or 0 (the predecessor)
  40. If you deleted the 7 in the tree used above, which node would take its place? Answer 9 (the only child)
  41. If you deleted the 2 in the tree used above, which node would take its place? Answer none
  42. Add 5, 7, 1, 3, 2, 4 to an empty min heap. Show what the resulting heap looks like. Answer
             1
            / \
          /     \
         2       4
        / \     /
       7   3   5
  43. Add 5, 7, 1, 3, 2, 4 to an empty max heap. Show what the resulting heap looks like. Answer
             7
            / \
          /     \
         5       4
        / \     /
       3   2   1
  44. Draw a weighted directed graph with the following edges:
    A to B (one way, weight 5)
    A to C (both ways, weight 4)
    C to D (both ways, weight 3)
    D to B (one way, weight 2) Answer
          5
       A --> B
       |     ^
     4 |     | 2
       |     |
       C --- D
          3
  45. Given the weighted directed graph above, what are the neighbors of D? Answer B, C
  46. Given the weighted directed graph above, what are the neighbors of B? Answer There are none.
  47. Given the weighted directed graph above, what are the neighbors of A? Answer B, C
  48. Describe the mimimum spanning tree of a weighted undirected graph with the following edges:
    A to B (weight 1)
    A to C (weight 3)
    B to C (weight 1)
    B to D (weight 3)
    C to D (weight 1) Answer It would consist of the edges A-B, B-C, C-D. A real test question would include more edges and vertices.
  49. Describe the shortest path from A to D of a weighted undirected graph with the following edges:
    A to B (weight 1)
    A to C (weight 3)
    B to C (weight 1)
    B to D (weight 3)
    C to D (weight 1) Answer It would be the path from A to B to C to D. A real test question would include more edges and vertices.
  50. Write down the result of a breadth-first search of an undirected graph with these edges:
    A-B, B-D, B-E, A-C, C-F, C-G
    Answer One possibility: A B C D E F G
  51. Write down the result of a depth-first search of an undirected graph with these edges:
    A-B, B-D, B-E, A-C, C-F, C-G
    Answer One possibility: A C G F B D E
  52. What are some advantages of using a linked list versus using an array? Answer
    1. A linked list can grow as needed, so you don't need to know ahead of time how many items will be in the list.
    2. It is easy and efficient to insert items into the middle of a linked list.
    3. It is easy and efficient to delete items from the middle of a linked list.
  53. What are some disadvantages of using a linked list versus using an array? Answer
    1. Accessing a specific position in an array is much more efficient than finding an item in a linked list.
    2. Linked lists have more overhead than arrays.
    3. Linked lists don't have the built-in snytax support in Java that arrays do.
  54. Programming

  55. What should you do with the programming example on the exam that has an error? Answer Indicate where the error is and specify a fix.
  56. What should you do when the test requests that you write a recursive function and code to test it? Answer Write it correctly.