Midterm Examination

COMP 14-091 Summer Session I, 2000


  1. This exam is closed book and closed notes. The only thing you get is the writing implement of your choice.
  2. Write all answers on the test itself. Do not write any answers in a blue book or on scratch paper.
  3. If you finish before 4:30, you may bring your test up to the front.
  4. Keep your answers short and to the point. Longer is not necessarily better.
  5. Budget your time carefully. This exam has 90 points total (1 for each minute). The approximate number of minutes you should spend on each question is given by its point value.
  6. Write legibly. If I can't read it, you won't get credit for it.
  7. You do not need to put comments on the code you write for this exam. You will not be graded on coding style, but indenting correctly will greatly help me understand your code (and get you the points)
  8. Make sure it is clear where your answer is. If you do scratch work on the page, put a box around your answer!
  9. Assume any code segment is embedded in a correct program.
  10. Assume all variables have been appropriately declared before they are used. Their type will be obvious from the usage.
  11. When you show program output, you do not have to indicate the exact spacing, but do show when an output starts on a new line.
  12. If there's anything that's ambiguous, ask me what to do or make a NON-TRIVIALIZING assumption, state that assumption, and keep going.
I pledge that I have neither received nor given unauthorized aid on this examination.

Signed:_____________________________________________________________

Name (print clearly):__________________________________________________

--------------------- END PAGE 1---------------------------------
 
 

Question
Maximum Points
Your Score
1
8
 
2
2
 
3
8
 
4
8
 
5
10
 
6
15
 
7
5
 
8
8
 
9
14
 
10
10
 
11
2
 
     
Total
90
 
--------------------- END PAGE 2---------------------------------
 

1) (8 points) Java identifiers: For each of the following, indicate whether it's a valid Java identifier. If it isn't valid, indicate why.

    a) switch        INVALID -- Java reserved word
    b) finalCount    VALID
    c) best$Deal     VALID
    d) _last_owner   VALID
    e) 2ndSolution   INVALID -- starts with a digit
    f) #toPrint      INVALID -- # is not allowed
    g) test_value    VALID
    h) number@work   INVALID -- @ is not allowed
2) (2 points) Comments: What are the two methods for indicating comments in Java? You can just write two comments below to show this. 3) (8 points) Terms: Answer each of the following questions about terminology
a) What is the major difference between a class and an object?
The major difference between a class and an object is that a class has no instance data. The class data merely describes the instance data that is given space when an object of the class is instantiated. (See page 177 in your book. Specifically, read the last paragraph on that page.)
b) What is the purpose of constructors?
The purpose of a constructor is to initialize the instance data for an object. (One source of confusion on the exam was that people thought that the constructor was responsible for instantiating the object. In reality, it is the new operator that instantiates the object, and then the new operator calls the constructor to initialize the variables for this newly created object. See page 74 in your book, next to the key concept box.)
--------------------- END PAGE 3---------------------------------

4) (8 points) Comparing Loops: We have three types of loops (do, while and for). When would you use each type of loop?

A programmer uses a for loop when he/she knows the exact number of iterations to run the loop. The do and while loops are used when the number of iterations is unknown ahead of time. The difference between these two is that you use the do loop when you want the body to execute at least one time, whereas you use the while loop when the body might not be executed at all.
5) (10 points) Expressions: Evaluate the following expressions, given these declarations for the variables (this means give the value that would be assigned to a variable of the appropriate data type if the expression were on the right hand side of an assignment statement): --------------------- END PAGE 4---------------------------------
6) (15 points) Understanding code: Answer the following questions. --------------------- END PAGE 5---------------------------------

7) Loops (5 points): What does this code fragment print out?

int i, j;
for (i=1; i<=4; i++)
{
    for (j=1; j<=i; j++)
    {
        System.out.print("#");
    }
    System.out.println();
}
 

#
##
###
####

8) Scope (8 points): List each variable in the following code and specify it's scope (class or method/local):
public class Building
{
    private int numFloors;
    private double temperature;

    public int estimatePeople(int peoplePerFloor)
    {
        int numPeople;

        numPeople = numFloors * peoplePerFloor;

        return numPeople;
    }
}

Scope refers to where the variable can be accessed from. So, can it be accessed from any method in the class (in which case it's scope is class-wide, or class  for short), or only in one method (in which case it's scope is local or method-wide, method for short).

Variable
Scope
numFloors class
temperature class
peoplePerFloor method/local
numPeople method/local

These are the only 4 variables in this class. The variables with local scope can only be accessed from inside the method estimatePeople. Note that estimatePeople is a method and not a variable! This question was not asking about the visibility modifers (public and private). They don't affect anything within a class.

--------------------- END PAGE 6---------------------------------

9) Loop equivalence (14 pts): Take a look at the following loop:

int count = 1;
while (count < 6)
{
    System.out.println(count % 2);
    count++;
}

a) (3 points) Circle and label the 4 parts of the loop.
 

part
what it is
initialization int count = 1;
condition (count < 6)
body System.out.println(count % 2);
update count++;

b) (4 points) What is the output produced by the loop?

1
0
1
0
1
c) (4 points) Convert this loop into a for loop.
for (int count=1; count<6; count++)
{
    System.out.println(count % 2);
}
d) (3 points) Circle and label the 4 parts of the loop you wrote in part (c).
This is really the same as part (a)
--------------------- END PAGE 7---------------------------------

10) Loops (10 pts): Write a code fragment using loops that produces the following output (hint: you should use nested loops):

11) (2 points) What would you like to talk about the last week of class? Rank the following in order of interest:
___ Robotics

___ HTML hacking – how to write web pages

___ Applets – how to make java programs that can be embedded in web pages

___ Networking/Internet/World Wide Web – differences between these words, how the web works

___ Other topics (you name it): _________________________________________

--------------------- END PAGE 8---------------------------------