CSE 132 (Spring 2011)
Quiz 1: Better late than never
Practice Problems

Model View Controller
  1. For each scenario below, discuss the appropriateness of the scenario in terms of the Model View Controller pattern:
    • A program sends messages to a JSlider to change the value of a model variable.
    • A user interacts wtih a JSlider to change the value of a model variable.
    • When the text of a model is updated, the view of that text does not show the changes that were made.
  2. Recall the Vector lab from CSE131 for representing two-dimensional vectors.

    How would you represent and manipulate Vectors using the model view controller pattern:

    • How would you represent the model?
    • What are potential interaction modes with a user and a Vector?
    • What are some potential display modes (views) for a Vector?
Exceptions
  1. How do exceptions and try...catch work?
  2. What is the difference between a checked and an unchecked exception?
  3. What options do you have in terms of writing software that deals with checked exceptions?
Persistence and I/O
  1. How is an int represented persistently using writeInt(int)?
  2. How is a boolean represented persistently using writeBoolean(boolean)?
  3. When you read a text file byte-by-byte, and receive each text character as a byte, what do you see?
  4. After writing the value 132, why did it read as -124?
  5. What is the 10's complement in 4 decimal digits of
    • 132
    • -132
  6. Using 10's complement arithmetic in 4 digits, show the addition of
             -132 + 241
    
  7. What is the 2's complement representation in 8 bits of
    • 101
    • -101
  8. Using 2's complement arithmetic in 8 bits, show the addition of
      -101 + 101
    
Threads
  1. What is the difference between .run() and .start()?
  2. How do you specify a thread's Runnable using an inner, anonymous class?
  3. Write a Java program that uses threads and has a race condition.
Polymorphism: .equals(Object) and .hashCode()
  1. Write the .equals(Object) and .hashCode() methods for a rectangle object, where two rectangles are considered equal iff they have the same width and length.
  2. Same as above, but now two rectangles are considered equal iff they have the same area.
  3. Same as above, but now all rectangles are considered equal to each other. Careful! Remember that .equals(Object) can be called on all objects and not just rectangles.
Synchronization and locks
  1. Which objects in Java have a lock associated with them?
  2. How do you obtain an object's lock before running a method on that object?
  3. How is a thread's execution affected by the use of the synchronized attribute in a method declaration?
  4. Write a java program that uses threads and requires the synchronized attribute on some methods for proper program behavior.
  5. What happened in Studio 3 when you made every method in Account synchronized?

    Why did that behavior occur?

  6. Show an example where the synchronized attribute should appear in a constructor method declaration.


Last modified 21:09:59 CST 18 January 2011 by Ron K. Cytron