COMP 14-091 Summer Session I, 2000
Signed:_____________________________________________________________
Name (print clearly):__________________________________________________
--------------------- END PAGE 1---------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) What is the major difference between a class and an object?--------------------- END PAGE 3---------------------------------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.)
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):
int x = 3, y = 10, z = 7;a) y % x 1
double price = 2.75;
boolean answer = true;
miles = 4;
if (miles <= 1)
System.out.println("walk");
if (miles <= 10)
System.out.println("take public transportation");
if (miles <= 200)
System.out.println("take a car");
take public transportationb) (4 points) What is the output of this code fragment?
take a car
miles = 4;
if (miles <= 1)
System.out.println("walk");
else if (miles <= 10)
System.out.println("take public transportation");
else if (miles <= 200)
System.out.println("take a car");
take public transportationc) (7 points) Write a sequence of if statements that produce the same output as part (b), but does not nest the if statements
7) Loops (5 points): What does this code fragment print out?
int i, j;8) Scope (8 points): List each variable in the following code and specify it's scope (class or method/local):
for (i=1; i<=4; i++)
{
for (j=1; j<=i; j++)
{
System.out.print("#");
}
System.out.println();
}
#
##
###
####
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).
--------------------- END PAGE 6---------------------------------
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.
9) Loop equivalence (14 pts): Take a look at the following loop:
int count = 1;--------------------- END PAGE 7---------------------------------
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?
1c) (4 points) Convert this loop into a for loop.
0
1
0
1for (int count=1; count<6; count++)d) (3 points) Circle and label the 4 parts of the loop you wrote in part (c).
{
System.out.println(count % 2);
}This is really the same as part (a)
10) Loops (10 pts): Write a code fragment using loops that produces the following output (hint: you should use nested loops):
You know the exact number of iterations that each loop will run, so for loops work the best here. We want the outer loop to run three times because there are three rows (and you have to finish everything on a row before you can go to the next, so your outer loop must control the number of rows in the output). The inner loop controls the number of stars on each row. Since there are three on the first row, it would be nice to include the number three in the outer loop somehow... Here's my solution:
int i,j;
for (i=3; i>=1; i--)
{
for (j=1; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}
___ Robotics--------------------- END PAGE 8---------------------------------___ 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): _________________________________________