Bryan Bishop October 5th, 02005 pg. 290 You will be tested on the following questions. Next Thursday. Self-Review questions What is a null reference? What does the "this" reference refer to? What is an alias? How are objects passed as parameters? What is the difference between a static variable and an instance variable? What is the difference between a class and an interface? Why might you declare an inner class? What is a dialog box? What is an event? What is a listener? Can a GUI-based program be a stand-alone application? Explain. Multiple Choice The this reference refers to --> the currently executing object. Which of the following should be used for a nobject reference variable that does not refer to any object? ---> null **** Which of the folloowing expressions can be used to test whether two objects, o1 and o2, are aliases of each other? o1.compareTo(o2) == 0 Which of the following reserved words is used in the class header of a class that implements an interface? ---> implements Which of the following is often static? constants Suppose s1 comes before s2 in the Unicode character set. What will be returned by this compareTo method? s2.compareTo(s1) a number greater than 0 A class that implements an interface must implement which methods of the interface? all the methods must be implemented Given the declarations String s1 = "James Gosling"; String s2 = "James Gosling"; which of the following statements is true? --> s1==s2 is true --> s1.equals(s2) is true A method that does not have an implementation is an abstract method What is the output of the following code? String word1 = "blue"; String word2 = "red"; String word3 = "green"; word2 = word3; word1 = word3; word3 = word1; System.out.println(word1 + " " + word2 + " " + word3); ----> green green green True: a reference variable that does not currently point to an object is called a null reference. True: The tihs reference lets an object refer to itself True: Two objects are aliases of each other if they are equal using the equals method False: The equals method and the === operator do the same thing for all objects. True: When an object is passed to a method, what is actually passed is a reference to that object. True: Static variables and methods are accessed through a class rather than through an instance of a class. False: A static method may use instance variables in the same class. True: Constructors may not be static. True: Constants may be declared in interfaces. True: A class may implement more than one interface. Short Answer 5.1 Discuss how Java passes parameters to a method. Is this technique the same for primtiive types and objects? Explain. Primitive data types do not have to be initialized. This means that they are given a value and they represent the value they hold. When they are passed through a method, it seems like it is "pass by value" because there is no instance of an object for Java to keep track of, so it's not really like "pass by reference". When an object is passed, it is passed by the reference. Explain why a static method cannnot refer to an instance variable. A static method does not have to be a part of an initialized object. An instance variable refers to one of an instance; a static method need not be in an instance (i.e. an object that has been constructed) Can a class implement two interface that each contain the same method signature? Explain. No. Two interfaces with the same method signature will create ambiguity errors. Create an interface called Visible that includes two methods: makeVisible and makeInvisible. Both methods should take no parameters and should return a boolean result. Describe how a class might implement this interface. A class may implement this as a means to determine visibility in some context. For example, if in a Game design project, a Player class could implement "Visibile" to determine if the Player should be drawn to the screen. Create an interface called VCR with methods that represent what a video cassette recorder does (play, stop, etc.). Define the method signatures any way you want. Describe how a class might implement this interface. interface VCR { void play(); void stop(); void rewind(); void fastforward(); boolean isOn(); void turnOn(); void turnOff(); } A class would have to implement each of the classes as defined by the method signatures. The means by which the functions of the methods are implemented vary between the products/types of VCRs.