Chapter 07 The object memory mode
Review questions
-------------------------------------------------------------------------------
1. Define the following terms: Bit, byte, and word.
Answer:
Bit: can be in one of two possible states, 0, and 1
Byte: 8 bits
Word: 32 bits or 4 bytes
-------------------------------------------------------------------------------
2. What is the etymology of the word bit?
Answer:
A unit of measurement of information (from binary + digit)
-------------------------------------------------------------------------------
3. How many bytes of memory are there in a 384 Mb machine?
Answer:
384 Mb = 1024*1024*384 = 402653184b
-------------------------------------------------------------------------------
4. convert each of the following decimal numbers to its hexadecimal equivalent:
A) 17
(B) 256
(C) 1729
(D) 2766
Answer:
A) 0x10
B) 0x100
C) 0x6c1
D) 0 xace
-------------------------------------------------------------------------------
5. convert each of the following hexadecimal numbers to decimal:
A) 17
B) 64
C) CC
D) fad
Answer:
A) 23
(B) 100
(C) 204
(D) 4013
-------------------------------------------------------------------------------
6. What is an address?
Answer:
The Byte's index position in the bytes 'sequence
-------------------------------------------------------------------------------
7. How many bytes does Java assign to a value of Type Int? How many bytes are
Required for a double?
Answer:
INT: 4
Double: 8
-------------------------------------------------------------------------------
8. What are the three memory regions in which values can be stored in a Java
Program?
Answer:
Memory authorized Ted to the program code and static data
Stack
Heap
-------------------------------------------------------------------------------
9. Using the example in section 7.2 as a model, trace the heap and stack
Operations that occur in the execution of the following method:
Public void run (){
Rational x = New Rational (4, 5 );
Rational y = New Rational (5, 2 );
Rational z = x. Multiply (Y). Subtract (z );
Println (x + "X" + Y + "-" + Y + "=" + Z );
}
Which objects on the heap are garbage when the println statement is reached.
Answer:
X, Y
-------------------------------------------------------------------------------
10. True or false: When you pass a primitive data value from one method
Another, Java always copies that value into the frame of the method being
Called.
Answer:
True
-------------------------------------------------------------------------------
11. True or false: When you pass an object from one method to another, Java copies the data in that object into the new frame.
Answer:
False
-------------------------------------------------------------------------------
12. Describe the two phases in a simple mark-and-sweep garbage collector.
Answer:
(Do not understand the question)
-------------------------------------------------------------------------------
13. What is meant by the term Wrapper class? What purpose do Wrapper Classes
Serve?
Answer:
Encapsulate each of the primitive types in a full-fledeged object
To substitue the primitive types in some situation
-------------------------------------------------------------------------------
14. What methods can you use to convert between integers and the string
Representations of those integers in a particle base?
Answer:
Pasrseint (string STR int Radix)
Tostring (int I, int Radix)
-------------------------------------------------------------------------------
15. What property identifies a linked structure?
Answer:
Contains references to other objects
-------------------------------------------------------------------------------
16. Given that objects of a particle class require a certain amount of space
In memory, it is clear that an object in the heap cocould never physically
Contain another object of that same class and still have room for additional
Data. What can a Java programmer do to achieve the same effect?
Answer:
Class P = new class;
-------------------------------------------------------------------------------
17. What is a stub?
Answer:
A simple implementation that you intend to replace later.
-------------------------------------------------------------------------------
18. Why is important for Java to include the special value null? What does this value represent?
Answer:
The constant null represents a reference to a nonexistent value and can be assigned to any variable that holds an object reference.
Bytes ----------------------------------------------------------------------------------------------------------
1. Use the static methods integer. parseint and integer. tostring to write
Program that converts hexadecimal values into their decimal equivalents. Your
Program shocould continue to read hexadecimal values until the user enters a 0.
Sample run of this program might look like this:
/* <Br/> * file: hextodecimalconverter. java <br/> * ------------------------------ <br/> * the program that converts hexadecimal values into their decimal equivalents. <br/> */<br/> Import ACM. program. *; <br/> Import Java. lang. *; <br/> public class hextodecimalconverter extends leleprogram {<br/> Public void run () {<br/> printheader (); <br/> while (true) {<br/> string hex = Readline ("enter a hexadecimal number:"); <br/> int dec = integer. parseint (Hex, 16); <br/> If (dec = Sentinel) {<br/> break; <br/>}< br/> println (hex + "hex =" + integer. tostring (DEC) + "decimal"); <br/>}< br/> private void printheader () {<br/> println ("this program converts hexadecimal to decimal. "); <br/> println (" enter 0 to stop. "); <br/>}< br/> Private Static final int sentinel = 0; <br/>}< br/>
2. The fact that the integer. parseint method makes it possible for a program to read
User input as a string and then convert it to an integer makes it possible to write
Programs that use something other than integer-such as a blank line-as
188 the art and science of Java
Sentinel to signal the end of the Input. Rewrite the averagelist program from
Exercise 4-6 so that it uses a blank line to mark the end of the input.
/* <Br/> * file: averagelist. java <br/> * --------------------- <br/> * The program reads in a list of integers representing exam scores <br/> * And then prints out the average. <br/> */<br/> Import ACM. program. *; <br/> Import Java. lang. *; <br/> public class averagelist extends consoleprogram {<br/> Public void run () {<br/> printheader (); <br/> double sum = 0; <br/> int CNT = 0; <br/> while (true) {<B R/> string line = Readline ("Please enter" ++ CNT + "Num:"); <br/> If (line. length () = 0) <br/> break; <br/> sum + = integer. parseint (line); <br/>}< br/> If (CNT! = 0) <br/> println ("The avarge is" + sum/CNT); <br/> else <br/> println ("null "); <br/>}</P> <p> private void printheader () {<br/> println ("the program reads in a list of integers representing" + <br/> "exam scores and then prints out the average. "); <br/> println (" Enter values, one per line. "); <br/> println (" Enter return to signal the end of the list. "); <br/>}< br/>
3. But don't panic. base 8 is just like base 10 really-if you're missing two fingers.
-Tom Lehrer, "The new math," 1965
Rewrite the math quiz program from Exercise 6-5 so that it poses its questions in
Base 8 instead of base 10, as shown in the following sample run: