CSE142 Spring Quarter
University of Washington
Midterm #1
April 25, 2003

  Closed book, closed notes, closed neighbor; calculators?
2 points per part except as noted

 
 
1 pt. each
In the Acrobat simulation, Mary (a Choregrapher) told Jack (an Acrobat) to twirl three times, which Jack did without saying anything back.  In object-oriented terms...  choose the best match (use each term at most once)

. Mary is a/an __________.

. Acrobat is a/an ________.

. "twirl" is a/an _________.

. three is a/an _________.

. "Jack did" is a/an _______.

A. class

B. message

C. method execution

D. object

E. parameter


D; A; B; E; C
 
1 pt. each
Here's a bit of Java.  Study the code with a view to identifying parts of the program.
class Apple {
    double price;
    int year;
    
    public Apple(int yearOfHarvest) {
        year = yearOfHarvest;
        price = 0.0;
    }
    
    public double getPrice() {
        if (price == 0.0) {
            PriceExpert expert = new PriceExpert();
            price = expert.calculateValue(year);
        }
        return price;
    }   
}

Use this key for your answers:

A. 0    B. 1    C. 2    D. 3    E. 4 or more

. How many instance variables are declared in the code shown? ____

. How many boolean expressions are there? ____

. How many constructors are declared? ____

. How many times are constructors called? ____

. How many methods have a double return type? ____

. How many method calls (invocations) are there (not counting constructors)? ____

. Of the following five words:  class  double  price  if  new ... how many are Java keywords (reserved words)? ____

. In method getPrice, is price == 0.0 an example of an assignment statement? Answer A for TRUE, B for FALSE: _____


6. C -- 2: price and year

7. B -- 1: price == 00

8. B -- 1: Apple

9. A or B -- the answer was intended to be 1, for new PriceExpert().  That is the correct answer from a static point of view, which is what is normally intended in questions of this sort.  However, from a dynamic point, the answer could be 0, depending on the value of price.  (For furture reference, on questions like this, where you are asked to pick the single best answer, and there is no "none of the above" option, 1 would be your best answer strategy.)

10. B or C -- The answer was intended to be 1 (the getPrice method), but it was noted that  the line  price = expert.calculateValue(year); suggests that calculateValue has a return type of double.  Actually, the code would compile and run without error if calculateValue had any numeric return type: int, double, float, etc. (So the single best answer to this question would have been B).

11. B -- just price = expert.calculateValue(year)

12. E -- All are reserved words except price.

13. B

 
What is the value of this expression?

     (13 >= 10) && ((1 + 2) <= 3)

A. true

B. false

C. 3

D. 13

E. can't tell without more information

A --This reduces to true && true which is true.
 
What is the value of this expression?

     (4 + 5) / (2 + 3)

A. true

B. false

C. 1

D. 1.8

E. 2


C -- 9/5 is integer division, because neither expression is double; so the answer is 1.  9 % 5 would be 4.
 
What is the value of this expression?  x and y are integer variables.

     (x <= x) || (y == 2)

A. true

B. false

C. 0

D. 2

C E. can't tell without more information


A -- (x <= x) is true regardless of the value of x.  For || (or), that's enough -- it doesn't matter whether (y == 2) is true or false.
 
Suppose this method is in the Calculator class (as developed in Homeworks #1 and #2). After studying the method, determine what value the answer variable (i.e., result) gets in each of the two questions
public double mysteryMethod(double num1, double num2) {
        double result;
        if (num1 < 0.0 || num2 < 0.0) {
            result = 0.0;
        } else {
            if (num1 <= num2) {
                result = num2;
            } else {
                result = num1;
            }
        }
        return result;
    }
.
         Calculator calcA = new Calculator();
         double answer1 = calcA.mysteryMethod(3.5, 6.0);
         
.
         Calculator calcB  = new Calculator();
         double answer2 = calcB.mysteryMethod(3.5, -6.0);
Answer choices:

A. -6.0

B. -3.5

C. 0.0

D. 3.5

E. 6.0

E -- 6.0

C -- 0.0

 
Study the method below. 
    public int examQuestionI(int j, int k) {
        while (j > 0) {
            k = k + 1;
            j = j - 1;
        }
        return k;
    }
. If this method is invoked with the first parameter as 2 and the second parameter as 3, what value does it return? ____

. If this method is invoked with the first parameter as 0 and the second parameter as 1, what value does it return?

A. 1

B. 2

C. 3

D. 4

E. 5

 

E -- 5.  j takes the sequence of values 2, 1, 0; k takes values 3, 4, 5

A -- 1.  The loop body is never executed.

 
Study the method below.

 

    public int examQII(int num1) {
        for (int i = 1; i <= 3; i = i + 1) {
            num1 = num1 + i;
            if (i >= 3) {
                System.out.print(i);
            }
        }
        return num1 + num1;
    }
. If this method executes with num1 equal to 5, what value is returned? __

A. 5

B. a value greater than 5 but less or equal to  10.

C. a value greater than 10 but less than or equal to 15.

D. a value greater than 15 but less than or equal to 20.

E. a value greater than 20.

. If this method executes with num1 equal to 5, what value is printed? ___

A. 3

B. 123

C. 333

D. 3333

E. Nothing is printed

E.    The loop body executes 3 times.  Inside the loop body, i takes the sequence of values 1, 2, 3.  num1 takes the values 5, 5+1=6, 6+2=8, 8+3=11.  The returned value is 11+11=22.

A. -- 3 [Note: this item was scored incorrectly on the Scantron -- it will be corrected, and everyone who gave the right answer WILL get the points!]

 
1 pt. each

Judge each of these bits of advice as a rule of syntax or as just a programming convention.

. "Don't start variable names with a digit"

. "Start class names with a capital letter"

. "Place parentheses around any parameter list"

. "Don't choose else as a method name"

A. rule of Java syntax

B. programming convention

A; B; A; A
 
. Worth 4 multiple choice questions
Fact: Strings have a method called length which tells how long the String is (i.e., how many characters it has).  For example.
    String title = "CSE 142";
    System.out.println(title.length());
    

will print the number 7.

Problem: A class Page is being designed as part of a text processor.  All lines of a Page have the same number of characters.  That number is an instance variable of page named lineLYour task:  implement one method of Page called printCentered. The printCentered method (a command) prints a line of text, centered.  The method takes one parameter:  the line to be printed (as a String).  If the string is too long for the line, print "ERR" only.  

An example: if there are 60 characters on a line, then the result of the method call printCentered("CSE 142 is fun") will be this (the dashes stand for spaces, just for this example):

-----------------------CSE-142-is-fun-----------------------

Write complete, compilable code for the method.  Since time is short, you can skimp on comments.  But don't skimp on legibility, please!  Write your entire answer here:

 

 

 

    
    public void center(String theLine) {
        if (theLine.length() > lineL) {
            System.out.println("ERR");
        } else {
            int numSpaces = (lineL - theLine.length())/ 2;
            for (int c = 1; c <= numSpaces; c++) {
                System.out.print(" ");  // use  " " or ' ' for real; 
                                        //use "-" for testing
            }
            System.out.print(theLine);
            //The following doesn't appear visually, but helps for testing
            for (int c = 1; c <= lineL - numSpaces - theLine.length(); c++) {
                System.out.print(" "); //use "-" for testing
            }
            System.out.println("");  // or use print("\n") or '\n'
        }
    }