[Thanks to Khris Smithers for keeping me on my toes. Some of the answers below are revised based on correspondence with him.] How is Java both "compiled" and "interpreted"? | Source code is compiled into byte code, not native machine code. | This byte code program is then interpreted ---------------------------------------------------------------------- What is the static type of freakshow? The dynamic type? Object freakshow = new Integer(); | Object, Integer What is wrong with the following, given Integer defines a method intValue() with return type int? int n = freakshow.intValue(); | compile error w/o casting freakshow to Integer before call ---------------------------------------------------------------------- Most methods you use in Java are instance methods. Give an example of a situation in which static/class methods must be used. | I actually can't think of a situation where static/class methods | *must* be used,* but if you have a method that is completely | independent of any instance and applies to the class as a whole, you | should probably make it static. For instance, if you have a method | that only modifies static member variables and no instance | variables, consider whether the method should be static. For more | about how static methods are used, read up on factory classes and | classes used to encapsulate functionality (and no data), e.g., | java.lang.Math. | | [*Chris Baker reminded me of the primary example that escaped my | attention, possibly because it was right under my nose: Of course, | the main method is always static. Strictly speaking, this is a | somewhat technical requirement enforced by the language design, | which dictates that main (following the C and C++ traditions) is the | method that is automatically invoked when a .class file is run.] In what ways are constructors similar to instance methods? In what way are they similar to static/class methods? | You can access instance methods from within a constructor | (as in instance methods). On the other hand, as with | static methods, there doesn't need to be an instance of | the class to call a constructor. ---------------------------------------------------------------------- What are the advantages and disadvantages of an interpreter vs. compiler? | advantages of interpreter over compiler: | machine independent programs | discover errors before execution; no recompiling | disadvantages of interpreter over compiler: | slower than native code | but can get decent performance with JIT (just-in-time) compiler | need interpreter/VM on client to run; no stand-alone native binary | | Recall that in general, compilers take a high level language and | translate it into machine code (whether it's virtual or for an | actual architectrue). In contrast, an interpreter is a program that | "wraps" the underlying machine and to allow more direct execution of | a high level language. Some people characterize this informally by | saying a compiler brings the language down to the level of the | machine, while an interpreter brings the machine up to the level of | the language. In the case of Java, the high level language is | typically compiled to an intermediate virtual machine instructions, | then an architecture-specific interpreter is used to allow execution | of these VM instructions on an actual machine. ---------------------------------------------------------------------- Which one of these happens ONLY in derived classes? overloading overriding neither both | overriding True or False: Both overloading and overriding require methods to have the same return type. | False [previously incorrectly marked true] | | Overloaded functions may have different return types (but they must | be unique with respect to number of and/or types of parameters). In | the case of overriding, in Java, the return type must be the same as | in the superclass method (and the parameter types and count must | also be the same). (In some other OO languages, the subclass | method's return type can be more specific than, i.e., a subclass of, | the superclass method's return type.) Which of these is a form of polymorphism? overloading overriding neither both | "Both" is the best answer. Overloading is an example of | ad-hoc polymorphism. Overriding enables another kind of | polymorphism called subclass polymorphism. ---------------------------------------------------------------------- (AA: Ting and Alison) In Java, is there a way to store primitive type values in heap memory? | Yes, by "wrapping" or "boxing" with a real object, e.g., | Integer for int. In Java, is there a way to store object instances in stack memory? | No, only primitive types can be stored in stack memory. | As an interesting side discussion, however, it's worth | noting that reference variables, which point to object | instances (without the mess of C/C++ pointer syntax, of | course), can be stored on the stack. The object instances | are always in heap, but the references to them can be in | stack memory (and often are). ---------------------------------------------------------------------- Suppose both Ball and subclass ColorBall define instance method bounce() and static/class method getClassName(). What methods are called by the code below and which calls are dynamically dispatched? Ball ballA = new Ball(); Ball ballB = new ColorBall(); ballA.bounce(); ballB.bounce(); ballA.getClassName(); ballB.getClassName(); | Ball.bounce() | ColorBall.bounce() | Ball.getClassName() | Ball.getClassName() | | The first two are dynamically dispatched. Is it possible to catch dynamic type errors at compile time? | no Is Java a statically typed language? | "Yes" is the best answer, since all identifiers are | required to have a statically declared type and most | type-checking is done at compile time. ---------------------------------------------------------------------- How does the compiler determine whether a method call will be dispatched dynamically? | In Java, all instance method calls are dispatched | dynamically. This is in contrast to C++, where a number | of different conditions must be met for dynamic | dispatching to occur: (1) the call must be via a pointer | or reference and not directly on an instance, (2) the | method called must be virtual for the declared (static) | type of the pointer or reference. When would one want to use dynamic dispatching and when would one want to use static dispatching? | Dynamic dispatching's general advantage is that it enables | subclass-specific behavior. There are some important | situations where static dispatching is preferred, however, | since it can make code easier to understand and is | slightly (negligibly) more efficient. ---------------------------------------------------------------------- pass by value vs. pass by reference Which model does Java use to pass variable values to methods? | Everything is passed by value in Java, including | references to objects. (Note that the objects themselves | are not passed---just references to them.) Which model does Miranda use to pass variable values to functions? | by value (but remember, there are no side-effects) with | lazy evaluation ---------------------------------------------------------------------- (AB: Bill, Hamed, Ryan) Does a private method in a base class get inherited by the derived class? | inherited, yes, but it's not directly accessible, so you might think | otherwise | | Strictly speaking, private members (whether they're methods or | variables) *are* inherited, but there's no way to access them in the | subclass. If you have private data in a superclass, a subclass | instance still contains that data, but it can't be accessed directly | in the subclass. (If the superclass provides non-private accessor | methods, this would be one way the subclass could access them, but | not directly.) How about a protected method? | yes In Java, what is the difference between public, protected, private and package visibility? | access specifiers, straight from the source: | | http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html ---------------------------------------------------------------------- Write code showing one example each of overriding a function and overloading a function. ---------------------------------------------------------------------- Primitive type values are passed by __________, while class type values are passed by __________. | value, value (references/pointers passed by value, in contrast to | copies of the objects being made) ---------------------------------------------------------------------- When calling an instance method, the Java virtual machine uses __________ dispatching. | dynamic Java uses static dispatching when calling this kind of method. What is it? | class/static method...constructors, too! ---------------------------------------------------------------------- (AB: Tessa and Adrienne) How do you call a static/class method? A. Ball b = new Ball(); b.classMethod(); B. Ball.classMethod(); C. classMethod(); | Both A and B work, as far as the compiler is concerned, | but which is better? (hint: rhymes with "bee") ---------------------------------------------------------------------- Ball b = new Ball(); Ball c = b; How many Ball instances are allocated by the lines above? | just one; both b and c refer to the same Ball instance | | Squint your eyes and pretend you've got this in C++ syntax | with pointers and you get... | | Ball *b = new Ball(); | Ball *c = b;