CSCI 1302 Sample Answers for Midterm Summer 2008 1. vi TextFile 2. or [ 3. ZZ or :wq or :x 4. 20k 5. ls -a 6. cp -r dir1 dir2 7. cat TextFile (or pg, less, or more in place of cat) 8. chmod u+x myscript (or 700 in place of u+x) 9. This is in the context of a subclass implementation: super.method( ), or super( ) for a constructor only. 10. This is in the context of the implementation of a class: class variables are declared static, are shared by all instances of the class, and can be accessed directly from the class name; instance variables are not static, exist separately for each instance of the class, and can be accessed only from the instance name. (Here a variable can reference a datum or a method.) 11. If a subclass implements a method with the same name, signature, and return type as an implemented (not abstract) method of its superclass then the superclass method is overridden. This is used to supply the appropriate behavior for the method in the subclass when that is different (e.g. taking into account the more specific context of the subclass) from the behavior which would otherwize be inherited from the superclass. This is one mechanism which supports polymorphism, the other being dynamic binding. Overloading is quite different; within the same class a method name can be implemented in different ways for different signatures (ordered sequence of input parameter types). 12. Similar: neither may have instances, both are classes. Different: keyword extends is for subclass of an abstract class (or of any class) vs. implements for a class declared ot adhere to an interface; interfaces are not allowed to have method bodies, and any data must be static and final (constants), whereas an abstract class my contain method implementations and data members of all sorts; implementing an interface requires that all the methods be implemented, but a subclass is allowed not to implement methods of the superclass; every interface all all of its members are public, whereas a class and its members can have any of four levels of visibility (private, package, protected, or public). 13. Generic types enable the same code to be applied to different reference types with no rewriting (thus promoting code re-use) and with compiler support for type checking (reducing the chances for run-time errors). 14. Yes, it's legal. (a) intarray --> [60 consecutive bytes, 4 for each int value] (b) [60 consecutive bytes, 4 for each int value] (now floating garbage) intarray --> [40 consecutive bytes, 4 for each int value] 15. This is in the context of the Java Collections Framework, in which an interface may specify (in the documentation) that a method is optional. The optional method is declared to throw the UnsupportedOperationException, with the understanding that a class which implements the interface may choose to implement the method by throwing the exception. Any method which is not optional is required, which means that a class implementing the interface is expected to supply a working implementation of the method. 16. An abstract class can't have instances, whereas a concrete class can. An abstract class can have abstract methods, while a concrete class can't. 17(a). public class TestClass { static public void main (String [] args ) { SubClass noArgs = new SubClass( ); SubClass testOne = new SubClass("test1"); noArgs.sayName( ); testOne.sayName( ); } } 17(b). SubDefaultName SubAssignedName:test1 17(c). Yes, the same as part (b) including the output. The compiler supplies a default no-args constructor for BaseClass to use with the SubClass constructors. 17(d). No, it will not run and there will be no output. The problem is that the compiler won't supply a default no-args constructor for BaseClass to use with the SubClass constructors, due to the presence of the other constructor. Whether the failure occurs at compile time or run time depends on the sequence in which the three classes are compiled. 17(e). Yes, the same as part (b) including the output. The compiler in this case uses the no-args constructor included in BaseClass to use with the SubClass constructors. 18. See answer 3 on page 747 of the class text. 19. See the three conditions given on page 730 of the class text. 20(a)(b)(c). See the three parts of answer 33 on page 680 of the class text. 21. See answer 3 on page 792 of the class text.