Quiz 11
Version 1

 

 

Quiz for CS 5
Week 11

1. At a mythical Q&A, Margaret said something about the "fields" of a class and Josh then referred to the "instance variables" of that class. Were we talking about the same things?

2. If your program defines and uses several classes, can they all go in one source file? Must they all go in one source file?

3. Does an object variable contain an actual object (like an integer variable) or just a reference to an object (like an array variable)?

4. When the examples in the notes declare the instance variables for an object, why do they declare them all as public?

5. Suppose that we have defined a class Measurement, with two fields time and temperature (both declared as doubles). The following code attempts to set up a variable of this class, but has forgotten to do something critical. What?

   Measurement m1;
   m1.time = 73.5;
   m1.temperature = 30.35;    

6. Suppose that you have the same class Measurement, and also a class Experiment. Suppose that the data field of the Experiment class is a 1D array of Measurements. Suppose that you have a variable lab1 of class Experiment. What expression would you use to retrieve the temperature of the 37th measurement in lab1 (assuming that previous code has already put a value in this field).

7. When you declare an instance variable for a class, you must declare its type. What Java types are you allowed to use in this declaration?

8. Suppose that you have an object of type X, whose field foo is an object of type Y. How can you ensure that space for the object in field foo is allocated automatically when you allocate space for a variable of type X?

9. Suppose that we have defined a class Triple containing three fields. We then declare an array of Triples using the code

   Triple[] myarray = new Triple[30];

What storage does this statement allocate? What storage do we still need to allocate when we make use of this array?

10. Suppose that you define a class teaseuser containing the main method for your program, but you put this class in a file with a different name, e.g. FLE30PRO20.java. What inputs would you give to the commands javac and java to compile and run your program?

Last modified August 28 for Fall 99 cs5 by fleck@cs.hmc.edu


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Sat, Nov 14, 1998 at 1:03:58 AM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_12/quiz.html