CIS435 Final

This is the KEY for the final. Correct answers are in bold.
  1. In Java, classes can have
    1. multiple parents
    2. one and only one parent
    3. at most two parents
    This is called single inheritance. Java classes can implement any number of interfaces.
  2. Private methods in the parent class are
    1. public in the child class
    2. private in the child class
    3. protected in the child class
    Protected methods are public in the child class.
  3. Given the classes defined below
  4. 
    public class parent {
    	public void answer() { System.out.println("99"); }
    	public void guess() {System.out.println("102");}
    }
    public class child1 extends parent
    {
    	public void answer() {System.out.println("42");}
    	public void guess()  {System.out.println("84");}
    }
    public class child2 extends parent
    {
    	public void guess() {System.out.println("101");}
    }
    and some instances
    parent p = new parent();
    child1 c1 = new child1();
    child2 c2 = new child2();
    
    For each method call below, what is printed?
    1. c1.answer(); 42
    2. c2.answer(); 99
    3. c2.guess(); 101
    The child2 class doesn't have an answer() method, so it used the one it inherited from its parent.
  5. Using the classes described above, is it legal to do this?
  6. 
    parent p2 = new child2();
    
    1. Yes
    2. No
    Variables of the parent type can hold objects of any child type, grandchild type and so on. This is the nature of the is-a inheritance relationship.
  7. If a class implements an interface, it must have methods that override all the ones in the interface
    1. True
    2. False
    This is different from inheritance where you can override methods from the parent class, but don't have to.
  8. If an exception occurs in a try block the catch statement is executed
    1. after executing the rest of the statements in the try block
    2. before exceuting the rest of the statments in the try block
    3. immediately, the rest of the statements in the try block are not executed
    Control jumps to the catch block that matches the exception in the nearest surrounding try block.
  9. In this bit of code, what is printed
  10. 
    // some code
    try {
    	// some code
    	try {
    	x.method(); //  method throws MyException
    	} catch(Myexception m2) { 
    			System.out.println("inner try");
    	  }
    } catch MyException m1) { 
            System.out.println("outer try");
      }
    
    1. outer try
    2. inner try
    The outer catch would only be done if the inner one threw the exception again.
  11. Java models input and output as
    1. streams
    2. ponds
    3. a network of pipes

  12. If you create a File object like this:
    File f = new File("testdata.txt");
    Which method calls are legal?
    1. f.exists()
    2. f.read()
    3. f.println()
    The File class represents the file, so you can only use it to check on the file. Other classes represent reading and writing the file.
  13. Which of the following is true for JPanels?
    1. They can hold exactly one component
    2. They can hold many components but no panels
    3. They can hold components or panels
    Panels are mainly used as containers to organize the position and appearance of other graphical components.
  14. Which of the following is true for listeners?
    1. They can only be an inner class
    2. They can be any class that implements the listener interface
    3. Are created when the graphic component is created
    We saw several ways to make listeners, inner classes were only one of them.
  15. Threads can only be used to handle graphics
    1. True
    2. False
    They can be used to do anything. Other examples we discussed were to do database or network I/O while computation was being done.