4.9 Problems

 < Day Day Up > 



4.9 Problems

  1. What is the difference between a runtime and a compile time data type? Does the distinction make any practical difference in a program?

  2. Why does Java not allow pointers? If they are unsafe, what (if anything) can be done to make them safe?

  3. At what time (compile time or runtime) is the data type of an interface checked?

  4. Given the following code, how many "int" primitives are created?

       int array[] = new int[10]; 

    If the array is changed from an int array to an object array, how many are created?

       Object array[] = new Object[10]; 

  5. Write a program that saves integer values from 1 to 100 in a Java array. What must be true of these integers? Use an object output stream to write these values to an Object output file. Read the Vector back into another program.

  6. Write a program that saves integer values from 1 to 100 in a java.util.Vector. What must be true of these integers? Use an object output stream to write these values to an Object output file. Read the Vector back into another program.

  7. Explain how the finalize method works. When is it called? How is it invoked when memory is recovered?

  8. Consider the four swap methods provided in Exhibit 20. Which (if any) work? Explain the behavior of each method.

    Exhibit 20: Four Swap Methods

    start example

     a. int swap(int a, int b) {     int tmp = a;     int a = b;   int b = tmp; } b. int swap(Integer a, Integer b) {   int tmp = a.intValue();   a = b;   b = new Integer(tmp); } c. int array[] = new int{1,2}; int swap(array, int a, int b) {   int tmp = array[a];   array[a] = array[b];   array[b] = tmp; } d. class SwapClass {int a, int b}; SwapClass sc = new SwapClass(); sc.a = 1; sc.b = 2; int swap(SwapClass sc) {   int tmp = sc.a;   sc.a = sc.b;   sc.b = tmp; } 

    end example

  9. For each numbered line in Exhibit 21, what is the output of the program? Explain how Java called the methods to create this result.

    Exhibit 21: Program with Numbered Lines

    start example

     class Parent {   String Name;   public void function_1() {     System.out.println("In Parent - function 1");   }   public void function_2() {     System.out.println("In Parent - function 2");     function_1();   } } class Child extends Parent {   String Name;   public void function_1() {     super.Name = "Childs Name";     System.out.println("In Child - function 1");   } } public class Question1 {   public static void main(String args[]) {     Parent parent = new Parent();     Child child = new Child();     parent.function_1();                //Part 1     parent.function_2();                //Part 2     child.function_1();                 //Part 3     child.function_2();                 //Part 4     ((Parent)child).function_1();     //Part 5     ((Parent)child).function_2();     //Part 6   } } 

    end example

  10. What objects are eligible for garbage collection at each of the identified points in the program shown in Exhibit 22? Why?

  11. Persistent objects are objects that persist (continue to exist) outside of a program. For example, objects that are written to a database or a file could be created in one program and used in another. Could the JVM be modified to manage persistent objects? Consider when these objects come into and go out of scope. What types of changes would have to be made to the JVM to allow it to handle persistent objects automatically?

  12. Can constructors and finalize methods be used to implement persistent objects? What would be some of the problems with using these methods?

  13. Create a memory leak and a dangling reference in Java. Describe any difficulties encountered when doing this.

  14. Does the instanceof operator check type of the identifier or the variable? Why?

  15. Explain why, in the following code fragment, the thread is not simply de-referenced and thus allowed to be garbage collected as soon as the call to start finishes.

       class MyRunnable implements Runnable {     public void run() {       //Do something in the thread     }   }   (new Thread(new MyRunnable())).start(); 

  16. Would concepts such as C/C++ templates or Ada generics be useful in Java? If not, why not? If so, give specific instances where they could be used. Would the usage be the same as in C/C++ or Ada?

  17. The Serializable interface in Java is referred to as a "tagged" interface because it simply provides a tag to the runtime that the object has some specific behavior. What other tagged interfaces can be found in the Java API?

  18. In the program shown in Exhibit 23, if the equals method in class Person is removed, the program does not work correctly. What is the problem? Why does this occur?

  19. What could be the reason for a runtime error (not a compile time error) being generated when attempting to write a non-serializable object to an object stream? Could the error be trapped at compile time? If so, is it not always preferable to trap errors at compile time rather than runtime?

  20. What is the difference between the instanceof operator and the getClass method of class Object? Can the two be used interchangeably?

  21. Some collection classes, specifically the Vector and the Hashtable class, synchronize all of their methods. This ensures that only one thread can be executing inside of the object at any time, thus eliminating the possibility of a race condition. An object with all of its methods synchronized is referred to as thread safe. When accessing all elements in a Vector or a Hashtable, either an Enumeration or Iterator type is used. Consider the following code fragment for accessing a Vector using an Enumeration. Is this code fragment safe if used in a concurrent program, given that the Vector class is thread safe? Why or why not?

       Vector v = new Vector;   v.add("1");   v.add("2");   Enumeration e = vector.elements();   while (e.hasMoreElements()) {     System.out.println(e.nextElement());   } 

  22. When Iterators are used in the place of Enumeration types, they are fail fast. Fail fast is described in the Java Application Programming Interface (API) for any collection class (Vector, LinkedList, etc.). What does fail fast mean? Does it provide protection for the collection class when it is run in a concurrent program?

  23. Consider the following code fragment. Explain why it is not safe, even though the Vector class methods are all fully synchronized.

       Vector table = new Vector();   if (!table.isEmpty()) {     Object o = table.elementAt(0);   } 

  24. Collection classes are not synchronized; instead, the application that uses it must provide the synchronization to make it safe. Consider the safe example shown below of the program in Problem 4.23. What unnecessary overhead is present? Based on the explanation of the unnecessary overhead, explain why synchronization was omitted from collection classes.

       Vector table = new Vector();   synchronized (table) {     if (!table.isEmpty()) {     Object o = table.elementAt(0);     }   } 

  25. Describe a method to safely access the Enumeration or Iterator for a collection class.

  26. Consider the program shown in Exhibit 24. What is the result of running it? Explain the result.

  27. For each numbered line in the program shown in Exhibit 25, state whether the line is valid or will result in a runtime or a compile time error.

    Exhibit 22: Garbage Collection Program

    start example

     class LinkedLetter {   char letter;   LinkedLetter nextLetter;   public LinkedLetter(char letter, LinkedLetter nextLetter) {     this.letter = letter;     this.nextLetter = nextLetter;   }   public LinkedLetter(char letter) {     this.letter = letter;     this.nextLetter = null;   } } public class GarbageCollect {   public static void main(String args[]) {     LinkedLetter A, B;     {       LinkedLetter C, D, E;       B = new LinkedLetter('B');       E = new LinkedLetter('E', B);       C = new LinkedLetter('C', E);       D = new LinkedLetter('E');       A = new LinkedLetter('A', D);     }                                   //Point 1     B = new LinkedLetter('B');          //Point 2   }                                     //Point 3 } 

    end example

    Exhibit 23: Equals Method in Class Person

    start example

     import java.util.*; class Name {   String firstName;   String lastName;   public Name(String firstName, String lastName) {     this.firstName = firstName;     this.lastName = lastName;   }   public boolean equals(Object name) {     if (! (name instanceof Name))       return false;     if ((((Name) name).firstName.equals(firstName)) &&       (((Name) name).lastName.equals(lastName)))       return true;     return false;   }   public int hashCode() {     String buffer = new String(firstName + lastName);     return buffer.hashCode();   } } class Person {   Name name;   int age;   char sex;   public Person(Name name, int age, char sex) {     this.name = name;     this.age = age;     this.sex = sex;   }   public String toString() {     return("Name = "+ name.firstName                              " " + name.lastName                              " age = " + age                              " sex = " + sex);   } } public class HashcodeExample {   public static void main(String argv[]) {     Hashtable myTable = new Hashtable();     myTable.put(new Name("Chuck," "Kann"),       new Person(new Name("Chuck," "Kann"), 42, 'M'));     Person P = (Person) myTable.get(new Name("Chuck," "Kann"));     System.out.println(P);   } } 

    end example

    Exhibit 24: Program for Problem 26

    start example

     import java.util.*; class MyObject {   int value;   public MyObject(int input) {     this.value = input;   }   public String toString() {     return ""+ value;   } } public class Question4 {   public static void main(String args[]) {     Hashtable ht = new Hashtable();     ht.put("3," new MyObject(3));     MyObject mo = (MyObject)ht.get("3");     Enumeration e = ht.elements();     while (e.hasMoreElements()) {       System.out.println(e.nextElement());     }   } } 

    end example

    Exhibit 25: Program with Numbered Lines

    start example

     class Person {   String Name;   public Person(String Name) {this.Name = Name;}   public String getName() {return Name;} } class Car {   String Name;   public Car(String Name) {this.Name = Name;}   public String getName() {return Name;} } class ques8 {   public static void main(String args[]) {     Object table[] = new Object[10];     table[0] = new Person("Chuck");     table[1] = new Car("BMW");     table[2] = new Object();     System.out.println(table[0].getName());                //1     System.out.println(table[2].getName());                //2     System.out.println(((Person)table[0]).getName());      //3     System.out.println(((Car)table[1]).getName());         //4     System.out.println(((Car)table[2]).getName());         //5     System.out.println(table[3].getName());                //6     System.out.println(((Car)table[3]).getName());         //7   } } 

    end example



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net