Java Exam IE 499c November 20, 2001 1. Object-oriented languages like Java are thriving because they suggest appropriate designs throughout the class hierarchy. Each class is a type definition that provides the instructions (called the class declaration) on how to create an object of that type. Match the object-oriented term on the left with the appropriate important component definition on the right: (place one letter (A, B, C, or D) in front of each term) __C__ Attribute A. The class function that runs first whenever a new object of that class is created. __A__ Constructor B. The overall design intended to give controlled access to an object's internals. __B__ Interface C. A variable outside of any function scope that provides data storage for the object. __D__ Method D. The object-oriented term for a function that belongs to and is accessed from within an object . 2. Each object encapsulates its data and processing to only allow minimal access by other objects. The most common way to protect an object variable from damage by other objects is to: (circle the letter before the best answer) A. declare the variable private and provide limited public functions to access it. B. declare the variable public and provide limited private functions to access it. C. declare the variable hidden and use a subclass to access it. D. declare the variable hidden and use a public interface to access it. 3. The Java keyword implements is used in class declarations to: (circle the letter before the best answer) A. subclass that class from an existing parent class. B. subclass that class from a newly declared inner-class. C. enforce that a certain existing interface be used with the class. D. identify that a new interface is being declared within the class. 4. Thinking in terms of Tree and OakTree simulation classes, which Java declaration statement would you expect to see most (the word deciduous means leaf-bearing -- as opposed to coniferous meaning cone-bearing): (circle the letter before the best answer) A. public class OakTree extends Tree implements Deciduous { B. public class Tree extends OakTree implements Deciduous { C. public class OakTree extends Tree, Deciduous { D. public interface OakTree implements Tree, Deciduous { EXPLANATION: OakTree IS a type of Tree (so it is best implemented as a subclass of Tree). OakTree includes the behaviors of Deciduous-ness so it should implement the Deciduous interface. Would the Java compiler complain about any of the four declarations (assuming all the classes and interfaces mentioned did exist in the same Java package)? (circle Yes or No) Yes No If you answered Yes, which one of the four is most incorrect? __C_ If you answered Yes, why? __Java is a based on a single-parent hierarchy (we mentioned this in class). So, you CAN'T inherit from more than one parent. You CAN implement multiple interfaces (as you would absolutely need to with a complicated design).__ 5. What Java keyword would you use to make a class declaration from a different package available in a .java file compilation? (circle the letter before the best answer) A. consider B. import C. open D. use 6. Which of the following statements best describes what Java is (circle the letter before the best answer) A. A scripting language created primarily to support rapid Web browser applications. B. A programming language designed primarily by mathematicians and scientists to supercede FORTRAN C. A programming language focused on improving native hardware performance through machine specific keyword directives. D. A collection of object-oriented classes, interfaces, and built-in types extencible through keyword-driven compiler instructions. 7. Java programs dependent on a Web browser for execution are called Applets by the language creators. What method does Sun Microsystems (Java's inventor) recommend to get an Applet running in a Web browser? (circle the letter before the best answer) A. Sun provides a specific Browser interface which you can implement in your applet's controlling class. B. Sun suggests you use the HTML element APPLET where you set the CODE attribute value to your controlling class' filename. Note: This is how Project 1 does it. C. Sun provides a start() method which is for specific use to start processing with the controlling class only. D. Sun suggests you use the JavaScript-Java application programming interface to start your applet by any one of many ways. 8. To require one Java class to review all messaging between two other Java classes, you should: (circle the letter before the best answer) A. extend the other two Java classes from the controlling class. B. implement an interface that the controlling Java class declares internally. C. place the two other Java classes in different Java packages. D. maintain two attributes in the controlling class and make the other two classes access each other through those attributes only. 9. Besides places before the class declaration line itself, where else would you expect to see a package name appear in a Java file? (circle the letter before the best answer) A. After the keyword implements if the class is to implement all interfaces from that package. B. As a prefix before a class name to distinguish that class from another package's class with the same name. C. After the keywords exists in when writing a condition that should run only if the class is in that package. D. After the end of the class declaration when the class is to be exported to another package. The other answers are false. This one was to give credit to those who read the primer closely as I mention this answer in there. I didn't expect you to get this one really so just live and learn if you got it wrong. 10. Java provides a bunch of visual controls through the java.awt and java.swing packages. Often, we programmers take advantage of a base class to make our visuals easier to write. In project 1 you saw how we extended the Canvas base class to create both the CTextArea and SimpleWhiteboard2 classes. Both our classes included paint() methods even though parent classes had their own paint() methods. What happens in the case when a parent class and subclass have a method with the same exact name? (circle the letter before the best answer) A. Only the parent method runs when accessed from the subclass. B. Only the subclass method runs when accessed from the subclass but the subclass method can explicitly call the parent method. C. The subclass method runs first and then the parent method runs when accessed from the subclass. D. The parent method runs first and then the subclass method runs when accessed from the subclass. After the following 2 questions, circle Yes or No: Must the parent have a method by the name of any subclass method? Yes No The whole idea for extending the base class is to add NEW functionality not already at the parent. Must the subclass have a method by the name of any parent method? Yes No I think the best answer is No because the subclass need not have its own method just because the parent's method is already available. But, I accepted the answer of 'No' for those who explicitly said the parent's method was the subclass' method (the same method) Under what condition(s) does a subclass run the parent's method only? __When the subclass declares no such method__ Extra Credit: What Java keyword forces any subclass to have a method by the same name as that parent method? _abstract_ 11. A thread is defined by computer scientists to be any sequence of instructions under independent control by a computing machine. Thread programming has huge potential for improving performance of applications while making better use of shared computing components. Java has a pretty good thread computing model (though there are many thread programmers that complain). Using Java, how can you create a new thread for your class to process in its own space? (circle the letter before the best answer) A. By implementing the Runnable interface properly only. B. By extending the Thread baseclass properly only. C. By implementing the Runnable interface properly or extending the Thread subclass properly. D. By suggesting a class is Threadable so the Java compiler can create threads when suggested by its optimizer routines. 12. Which of the following classes are typically most easily reusable by incorporation into other systems? (circle the letter before the best answer) A. Those which connect to other classes through interface type attributes only. B. Those that make all their attributes private. C. Those that extend the basic java packages' classes. D. Those that make all their attributes and methods static. Explain why you chose the answer you did: _A is the best answer because it provides the most reusable flexibility without burdening the system. Think of it this way: You want one class to perform some services for another class but you want that servicing class to be able to implement a wide range of service handling approaches. So, you have an interface with just the services you need handled. Then, you implement the interface for any class you want to communicate with that reusable class. The class needs to know nothing except the fact it can perform the services requested of it (through the methods included in the interface). It's a beautiful thing and you have a great example of it in how the ClientThread communicates with our Dispatcher without having to know what a Dispatcher is. It just has to rely on the fact that the Dispatcher has implemented the ClientThreadObserver interface to know it will handle the services we require of it. Having said all that, I only deducted 2 points (the question was worth 4) for those of you who provided a good explanation of why you chose another answer instead. Which of those same four classes are least easily reusable? circle: A or B or C or D Why? _Private attributes have nothing to do with reusability unless you incorporate some other design feature that makes them valuable. Those who explained that got full points. Almost everyone supported their choice with a valid explanation so hardly anyone got any points taken off after I took the 2 points off for not choosing A in the first part._ 13. What Java keyword can you use to protect memory locations involved in a method accessed by more than one thread? (circle the letter before the best answer) A. semaphore B. serialized C. static D. synchronized 14. What three networking objects are most critical to programming a client connection to a server: (circle the letter before the best answer) A. A socket, the server address, and a port. B. A socket, the server address, and a subdirectory. C. The server address, a port, and a subdirectory. D. A socket, a port, and a subdirectory. Note the subdirectory can be important but is part of the server address when thinking of the three important components. If you don't provide a port, the server connects to a default port but that port is critical to the system working correctly. 15. Many object-oriented languages provide the keyword delete along with the concept of a destructor for use when the delete keyword is used. Actually, Java provides the delete keyword as well and it is easy to create destructor processing without any unique syntax having to be provided. Why, based on what you know, haven't we used the delete keyword in our code? (circle the letter before the best answer) A. We let the non-Java services in our Web browser manage all our system memory (Java or otherwise) for us. B. We don't expect the system objects to ever be deleted. C. The Java garbage collector does a good enough job without our explicit help. D. The Java delete keyword is reserved for deleting java package objects only and so it should only be used in the parent classes. The other three answers are false 16. When using the JavaScript to Java interface in the Internet Explorer Web browser, which of the following restrictions apply? (circle the letters before all correct answers) A. The JavaScript function and Java method must have the same exact name. B. The Java method must inplement the JavaScript interface in order to be within scope of JavaScript. C. The Java method called by JavaScript must return a non-void type. D. JavaScript functions can only call methods in classes that extend Applet. The other three answers are false as can be seen by our project 1 code. 17. As seen in the Whiteboard2 class of project 1, which of the following object-oriented terms describes an interface that makes one class behave like another: (circle the letter before the best answer) A. Adapter B. Factory C. Listener D. Observer A Factory by the way is a class responsible for creating objects that meet certain design requirements (A Factory class usually can create objects of a range of class types based on logic within its methods). 18. Which of the following is the most preferred object-oriented way to add a new feature to an existing Java system already in production: (circle the letter before the best answer) A. Change the interface implemented by the class that needs to be enhanced. B. Change the base class extended by the class that needs to be enhanced. C. Extend the existing class to create a new class with the enhancement (adding all possible enhancements at once). D. Change the class that needs to be enhanced. Why do you suggest that answer? _Because it keeps all existing systems using the current class clean ('in production' means relied upon by others). Note some of you gave compelling reasons for other answers but you should get to know this is considered the BEST way to do things._ 19. Which of the following code snippets is a correct Java example of getting a more human intelligent networking stream under programmer control: (circle the letter before the best answer) A. inputStream = socket.getInputStream(); dataInputStream = new DataInputStream(inputStream); B. inputStream = socket.getInputStream(); inputStream.parseData(); C. inputStream = socket.getInputStream(); dataInputStream = Net.inputToDataStream(inputStream); D. dataStream = socket.getDataStream(); Now, assume you have a wonderful new stream package available to you that turns data into voice control commands by offering a VoiceCommandInputStream. What additional Java statement would you add to your answer above to get the voice commands out of the stream? (Explain your rationale if you think I won't understand your thinking) _voiceCommandInputStream = new VoiceCommandInputStream(dataStream);_ _______________________________________________________________________________ 20. We discussed Hashtables during our investigations of the Java language. A Hashtable is just one of many useful Java objects in a category called containers. Containers hold data and organize it in a way that is most useful for the goals of the programs using the data. The Stack class makes the most recent data available most readily. The Queue class makes the least recent data available most readily. A LinkedList class makes addind data in the middle easiest. What is the main container goal of the Hashtable (which is also the main goal of a Map, if you ever want to check out another cool container). (circle the letter before the best answer) A. Checking for data validity before storage by applying a hash algorithm (on credit card numbers for example) B. Storing data on alternative storage media like CD-ROMs and floppy disks. C. Organizing huge amounts of data for quick access in computer memory. D. Organizing data based on the size of the data so smaller data is bunched together in an efficient manner. 21. If you wanted to add word wrapping and scrolling to your CTextArea and you wanted to add those features in the most reusable, suitable, and professional manner, which class declaration would you expect to see: (circle the letter before the best answer) A. class CTextArea extends Scrollable, WordWrapping { B. class CTextArea implements Scrollable, WordWrapping { C. class CTextArea imports Scrollable, WordWrapping { D. class CTextArea extends TextArea implements WordWrap { Scrollable and Wrapping are feature sets lots of visual controls would like to implement. But, they are services and so should be implemented as interfaces. I apologize for not giving an example in class of multiple interface implementation (in advanced systems it's not rare to implement 5 or 10 interfaces in a class declaration). I will do that next time I teach the course for sure to avoid confusion. Remember that A is illegal in Java (compiler would not compile that class) but possible in other object-oriented languages. 22. Which programmer's trick was the most critical implementation in project 1 to keep the Canvas class objects from flickering (note that all the terms are valid programmer tricks you might be interested in)? (circle the letter before the best answer) A. double-buffering to build an off-camera image first before quickly replacing the visual one on the Canvas. B. inverting to draw the chat lines from bottom up. C. time-splicing to draw all chat lines simultaneously. D. "dirty rectangles" to draw just those parts of the canvas that needed redrawing. 23. As you looked through all the Java classes involved in project 1, you probably noticed the try keywords that always had a catch keyword associated with them. try and catch are not unique to Java. In fact, they pre-date Java altogether. try and catch work together to provide a powerful concept called exception reporting. Exception reporting is the key to any future automated society (if we are to go down that somewhat frightening path). With exception reporting, you tell the computer what exceptions you want to be notified about (as basic as dividing by zero or as sophisticated as acceleration over 75 miles an hour) and you place your code inside of the try-catch block. The catch tells the computer what exceptions to look for and then you code your response to those exceptions inside of the catch (so, the exception doesn't have to be a so-called error condition though it usually is because most programmers (me included) don't use exceptions as often as they should. Exceptions in Java have a wonderful hierarchy with the most basic exception class called Exception. Given your understanding of our project 1 code, which Exceptions would you expect to see covered in our code if we wrote the best code possible? (circle all letters that are correct answers) A. FileNotFound (which is a subclass of the IOException class) exception B. NumberFormatException (which is a subclass of the DataFormatException class) C. NullPointer exception (which is a subclass of the RunTimeException class) D. LastOwnerException (when you try to delete the last owner of an access control list) 24. What is the main reason two Java programmers working on the same problem who have never met will most likely write code that is similar? (circle the letter before the best answer) A. They both started programming at the age of 12 and watched the same prime time televisions shows during their formative years. B. They understand the various java.* packages and the inherent design and effectively best inherit the classes and use the interfaces provided. C. They both attended the same four-year college and are proud members of the Class of 2000. D. They have Extra-Sensory Perception and are likely to sense each other's thoughts. E. They both have ancestors who came to America from the same country. F. They both ate the same Tex-Mex meal within hours before writing code. G. Their DNA encoding includes similar class declarations for their type and thus they exhibit similar behaviors. For fun (if you need some fun): List the order of the next 4 influences on their similarity of coding (sorry, there are no correct answers here): (place a letter after each cardinal number) Second: ______ Third: ______ Fourth: ______ Fifth: ______ Your answers were interesting here. The most popular influence for those who answered the question was A, the effect of television on the coders. I think that shows great insight. I am also happy few of you chose G as that doesn't make being human sound like we have choice and freedom in our lives. I have also found that our heritage has some impact on our coding. That is why I like to work with multi-cultural teams when I want to learn something new. Who knows if D might turn out to have some truth too in the long term. C would have great impact if they both received Computer Science degrees and had access to the same object-oriented classes (professors). But, since they didn't know each other, that sounds highly unlikely. 25. Already at question 25 (yes, the last) and I haven't reinforced the importance of messaging to the whole future of programming and communications. Messages are the key to object-oriented systems. Messages are key to networking. In fact, instant messaging on the Web is predicted to increase by 200 times current levels over the next six years. In your project 1, you worked with very simple messaging techniques. Knowing what you know about Java and object-oriented design, what things do you think we can do to make messaging better than passing text-based Strings around the Web? If you can't think of specifics, think of general ideas and write them down here (be sure to write down the benefits of each of your ideas): _This question was graded as extra credit only. No points were deducted for those who didn't answer it or answered it wrong. I was looking for the idea of shipping intelligent objects around the net whereby the messaging was built into the objects being shipped. Many of you said that verbatim. Others pointed to UDP data streams which is correct as well. Thanks for your insights on this one and keep thinking about it going forward_ _____________________________________________________________________________________ _____________________________________________________________________________________ _____________________________________________________________________________________ _____________________________________________________________________________________