Вы находитесь на странице: 1из 4

Q - The Java read() method reads and returns a single byte from the standard input device.

It stores that byte according to what type. What does the method return if the user enters an eof? A - The Java read() method reads and returns a single byte from the standard input device and stores that byte in an integer. It returns an integer value of -1 if the user enters an eof. Q - What keystroke combination can be used to simulate an eof at the keyboard of a DOS system? A - An eof can be simulated on a DOS system keyboard by holding down the ctrl key and pressing the z key. Q - Provide a Java code fragment illustrating how you would read a stream of bytes from the standard input device until encountering an eof and quit reading when the eof is encountered. A - The following Java code fragment will read a stream of bytes from the standard input device until encountering an eof. while(System.in.read() != -1) { //do something } This code fragment accesses the read()method of the object referred to by the class variable named in of the class named System. Q - Provide a Java code fragment illustrating two different ways to display a String argument on the Java standard output device. Explain how your code works in object-oriented terms. Make certain that you explain the difference between the two. A - The following two code fragments will each display a string argument on the Java standard output device. System.out.println("String argument") System.out.print("String argument") In the first case, the code fragment accesses the println() method of the object referred to by the class variable named out of the class named System. In the second case, the print() method is accessed instead of the println() method. The difference between the two is that the println() method automatically inserts a newlineat the end of the string argument whereas the print() method leaves the display cursor at the end of the string argument. ===============

OOP
Q - In Object-Oriented Programming, an object is often said to be an ____________ of a class. A - An object is often said to be an instance of a class. Q - In Object-Oriented Programming, an object is often said to have s_______ and b_______. Provide the missing words which begin with the letters shown. A - In OOP, an object is often said to have state and behavior. Q - An object's state is contained in its ________ and its behavior is implemented through its

________. A - An object's state is contained in its member variables ( or data members) and its behavior is implemented through its methods ( or member functions). Q - The member variables of an object can be either ____________ or _________ . A - Its member variables can be either instance variables or class variables. Q - What is generally meant by the terminology "sending a message to an object?" A - We activate the behavior of an object by invoking one of its methods (sending it a message). Q - What are the two things that can usually happen when an object receives a message? A - When an object receives a message, it usually either performs an action, or modifies its state, or both. Q - What happens to the memory occupied by an object in Java when the object is no longer needed, and what do we normally do to make that happen? A - When an object is no longer needed in Java, we simply forget it. Eventually, the garbage collector may (or may not) come by and pick it up for recycling. Q - Identify as the stages of an object's life? A - The stages of an Object's life are: Creation Use Cleanup Q - The creation of an object involves three steps (which are often combined). What are the three steps? A - The three steps are: declaration (providing a name for the object) instantiation (setting aside memory for the object) optional initialization (providing initial values for the object's instance variables) Q - Java allows the instantiation of variables of primitive types in dynamic memory: True or False? If false, explain why and what you might be able to do to achieve almost the same result. A - False. Java does not allow the instantiation of primitive variables in dynamic memory. (However, there are wrapper classes for primitive types which can be used to turn them into objects for this purpose.) Q - An array of objects in Java is instantiated as an array of reference variables where each reference variable can then be used to instantiate an object pointed to by the reference variable: True or False. If false, explain why and either provide a code fragment that illustrates your answer A - True. Q - In Java, it is always necessary to declare (give a name to) all new objects: True or False? If false, explain why and either provide a code fragment that illustrates your answer A - It is not always necessary in Java to declare an object (to give it a name). Consider, for example a case where a

new object is instantiated to be used in an expression and there is no requirement to be able to access that object outside of the expression. Q - Instance variables and instance methods can be accessed using an object as the access mechanism. What is the difference in the syntax used to access an instance variable and an instance method. A - None. There is essentially no difference in the syntax used to access a variable or a method. Q - Once you have instantiated an object, it is always possible to access all of the instance variables and instance methods of that object by joining the name of the object to the name of the variable or method using a period: True or False? If false, explain why. A - False. Sometimes variables or methods may be hidden so as to make it impossible to access them. It is very common to hide the variables and to provide methods which can be accessed to serve as a pathway to the variables. Q - Given an object named obj that has a public instance method named myMethod(), provide a code fragment that shows the proper syntax for accessing the method. A - The proper syntax for accessing an instance method named myMethod() follows: obj.myMethod() Q - The object-oriented approach normally recommends hiding instance variables behind access methods: True or False? If true, explain why. A - True. The object-oriented approach normally recommends hiding instance variables behind access methods. There are a variety of reasons why. One important reason is that hiding the instance variables makes it possible to later modify the implementation of instance variables to improve the behavior of objects of the class, without a requirement for modifying code that uses the clsss, provided that the access methods are not modified. Q - The returning of memory (to the operating system) occupied by objects that are no longer needed is automatically accomplished in Java by a feature commonly known as the ____________________. A - The returning of memory to the operating system is taken care of automatically by a feature of Java known as the garbage collector. Q - All necessary cleanup in a Java program is performed automatically by the garbage collector: True or False? If false, explain why. A - False. Java does not support anything like a destructor that is guaranteed to be called whenever the object is no longer needed. Therefore, other than returning allocated memory, it is the responsibility of the programmer to explicitly perform any other required cleanup at the appropriate point in time. Q - When does an object become eligible for garbage collection? A - An object becomes eligible for garbage collection when there are no more references to that object. Q - What can your program do to purposely make an object eligible for garbage collection. A - Your program can make an object eligible for garbage collection by assigning null to all references to the object. Q - The purpose of garbage collection in Java is to perform all necessary cleanup and the memory

occupied by objects that are no longer needed will always be reclaimed by the garbage collector: True or False? If false, explain why. A - False. The sole purpose of garbage collection is to reclaim memory occupied by objects that are no longer needed, and it has no other purpose relative to necessary cleanup. An object becomes eligible for garbage collection when there are no more references to that object. However, just because an object is eligible for garbage collection doesn't mean that it will be reclaimed. The garbage collector runs in a lowpriority thread, and may not run at all unless a memory shortage is detected. Q - Before the garbage collector reclaims the memory occupied by an object, it always calls the object's _________ method. (Provide the name of the method.) Explain why this method is one of the methods in all new classes that you define. A - Before the garbage collector reclaims the memory occupied by an object, it calls the object's finalizemethod. The finalize method is a member of the Object class. Since all classes inherit from the Object class, your classes also contain the default finalize method. Q - What must you do to make effective use of the finalize method? Explain why you might want to do this. A - In order to make use of the finalize method, you must override it, providing the code that you want to have executed before the memory is reclaimed. Q - You can always be confident that the finalize method will be promptly executed to perform necessary cleanup in a Java program when an object becomes eligible for garbage collection: True or False? If false, explain why. A - False. Although you can be confident that the finalize method will be called before the garbage collector reclaims the memory occupied by a particular object, you cannot be certain when, or if that memory will be reclaimed. There is no guarantee that the memory will be reclaimed by the garbage collector during the execution of your program. Q - Provide a code fragment illustrating the method call that you can make to ask the garbage collector to run. This guarantees that garbage collection will take place: True or False? If false, explain why. A - False. Campione and Walrath indicate that you can ask the garbage collector to run at any time by calling the method shown below. They further point out, however, that making the request does not guarantee that your objects will be reclaimed through garbage collection. System.gc(); ======

Вам также может понравиться