CS 180 Spring 2002
Exam 1

Source: http://elwww.cc.purdue.edu/~cs180/Spring2002Web/exam1.S02.key.html

Part 1. Multiple Choice Questions

Answer the following multiple choice questions on the bubble sheet given.

Fill in the Instructor, Course, Signature, Test, and Date blanks. For "Instructor" put your Recitation Instructor's last name. For "Course" put CS 180. For "Test" put Exam 1.

Fill in the bubbles that correspond to your name, section and Student ID in the bubble sheet. For your section number, use 0730, 0830, 1130, 1230, 0130, or 0230 -- based on the start time of your Friday recitation.

Exams without names will be graded as zero.

Only the answers on the bubble sheet will be counted. The questions will be discarded.

Each question counts for 1 point. There are 20 multiple choice questions in total.

(1) Which of the following does NOT characterize a High-Level Language (HLL)?
(a) directly understood by hardware
(b) "portable" source code
(c) close to natural language
(d) uses variables and identifiers
(a)

(2) A Java .class file contains...?
(a) Java Virtual Machine
(b) Java Compiler
(c) Java Source Code
(d) Java Byte Code
(d)

(3) How do class objects differ from primitive variables?
(a) Objects have a type associated with them.
(b) There are scope rules for where objects are known.
(c) Objects have both data and methods.
(d) Objects can be given initial values.
(c)

(4) "A SchoolBus is a Bus, and since it is a Bus, it is also a Vehicle" demonstrates which OOP principle...?
(a) Encapsulation
(b) Modularization
(c) Inheritance
(d) Polymorphism
(c)

(5) An error that cannot be determined by either the compiler or the run-time system is called a (an)....?
(a) syntax error
(b) logic error
(c) class error
(d) exception
(b)

(6) Which is correct about Java identifiers?
(a) Total and total are the same identifier, but TOTAL is different.
(b) Total and TOTAL are the same identifier, but total is different.
(c) Total, total, and TOTAL are three different identifers.
(d) Total, total, and TOTAL are three aliases for the same identifer.
(c)

(7) Which of the following correctly declares and initializes a primitive variable?
(a) public final static double status 35.97;
(b) double status = new double (35.97);
(c) double status = 35.97;
(d) double status = = 35.97;
(c)

(8) Given that m and n are ints and x and y are doubles, which of the following requires an explicit cast?
(a) x = n;
(b) n = x;
(c) x = y-n;
(d) y = m/x;
(b)

(9) If a and b are two ints, what is the expression ((a/b)*b + a%b) always equal to?
(a) a
(b) b
(c) 2a
(d) a+b
(a)

(10) Which of the following two-statement combinations are equivalent to the following statement?
        result = m * n++;
(a) result = m * (n+1); n = n + 1;
(b) n = n + 1; result = m * (n+1);
(c) result = m * n; n = n + 1;
(d) n = n + 1; result = m * n;
(c)

(11) If String st = "Butterfly", what is st.substring(3)?
(a) "fly"
(b) "But"
(c) "tterfly"
(d) "terfly"
(d)

(12) What is the boolean equality test for two primitives in Java?
(a) =
(b) = =
(c) equals()
(d) eq()
(b)

(13) Which of the following tests if String st contains the same character string as String bf?
(a) st = bf
(b) equals(st,bf)
(c) st = = bf
(d) st.equals(bf)
(d)

(14) Which is correct about switch(weight) if weight is a float variable?
(a) It MUST contain a default Case_Label.
(b) Each Case_Label must be a float value or a double value.
(c) Each Case_Label must be a float value.
(d) A switch may not use a float variable as its controlling expression.
(d)

(15) Which loop(s) can execute the loop body zero times?
(a) while and for
(b) while and do-while
(c) while
(d) do-while and for
(a)

(16) How should the following statement be changed to avoid any chance of a divide-by-zero error?
        if ((sum/number > base) && (number != 0))
(a) It should be if ((number > 0) && (sum/number > base))
(b) It should be if ((sum/number > base) & (number != 0))
(c) Instead of the variable base, there should be a constant
(d) A boolean variable should be used in place of (number != 0)
(a)

(17) Which of the following correctly declares and instantiates an object of class Auto?
(a) Auto car;
(b) Auto car = new Auto("Brenda", "Toyota", 12, 7548);
(c) car = new Auto("Brenda", "Toyota", 12, 7548);
(d) Auto car = Auto("Brenda", "Toyota", 12, 7548);
(b)

(18) Class Auto has an instance variable gallons and a method
    public void setGas (double amount)
    {
        gallons = amount;
    }
Which line is equivalent to the statement in the method?
(a) this.gallons = amount;
(b) gallons = this.amount;
(c) this.gallons = this.amount;
(d) None of the above!
(a)

(19) A variable is declared as an int at the top of the main() method and is re-declared as a double later in the method. How does Java tell which type it is each time it appears?
(a) It depends upon an explicit cast ... that must be used in this situation.
(b) It depends upon the type of data (int or double) being stored.
(c) It depends upon the last declaration just before the current appearance.
(d) The above causes a syntax error. A variable name can be declared only once per method!
(d)

(20) What is the REAL truth about Java parameter passing?
(a) Java ALWAYS uses call-by-reference. It just works like call-by-value for primitives.
(b) Java ALWAYS uses call-by-value. It just works like call-by-reference for Class objects.
(c) Java uses call-by-value or call-by-reference dependent upon the VAL and REF modifiers in the method header.
(d) Java's call-by-value for primitives works exactly the same as if it were call-by-reference.
(b)



CS 180 Spring 2002
Exam 1

Part 2. Written Problems

Name: _________________________________

Section:________________________________

Student ID Number:______________________

Write your Name, Section, and Student ID Number. Without this information the exam will be graded as zero.

For your section number, use 0730, 0830, 1130, 1230, 0130, or 0230 -- based on the start time of your Friday recitation.

Each code segment is worth 10 points, except the last two -- each of which is worth 15 points. Problems will be graded on the merit of correct ideas. Only the last few points will be reserved for syntactic correctness (perfect code). Thus, writing explanatory pseudo-code in place of parts you cannot figure out is preferable to leaving the question blank.

IMPORTANT:
(1) Assume that any variable names in bold are already declared for you.
(2) Unless otherwise stated, we only want code fragments. There is no need to write a main method or ask for input if we have already given it to you.

(1) [10 points] Write some Java code to compute 1 - 1/x - 1/x2 - 1/x3 - ... - 1/x10. Use a loop of some type. In the loop you must use -= and /=. Do NOT use Math.pow().

double result = 1.0; double value = 1.0/x; for (int i=1; i<=10; i++) { result -= value; value /= x; }
(2) [10 points] Given an integer number, write a Java code fragment to examine number and print how many of its digits are even and how many are odd. For example, 28574 has 3 even digits (2, 8, and 4) and 2 odd digits (5 and 7).

int odds=0; int evens=0; int lastDigit; do { lastDigit = number % 10; if (lastDigit%2 == 0) evens++; else odds++; number = number / 10; } while (number > 0); System.out.println (evens + " even digits and " + odds + " odd digits");
(3) [10 points] Compare two strings s1 and s2. If they are exactly the same, print "same". Otherwise, if s1 concatenated with s2 with a space between them is the string "Purdue Pete", print "mascot". Otherwise, print "different". Note: Case does not matter for the "mascot" test. So, an s1 of "PURDUE" and an s2 of "pEtE" would work.

if (s1.equals(s2)) System.out.println("same"); else if ((s1+" "+s2).equalsIgnoreCase("Purdue Pete")) System.out.println("mascot"); else System.out.println("different");
(4) [10 points] You are given the variables x, y, op, and result. The variables x and y are integers. The variable op is a character. Write a switch statement that will add x and y and store the sum in the variable named result if op is a capital or lower case 'A', that will multiply x and y and store the product in the variable named result if op is a capital or lower case 'M', and will store 999 in result if op is anything else.

switch (op) { case 'A': case 'a': result = x + y; break; case 'M': case 'm': result = x * y; break; default: result = 999; break; }
(5) [10 points] Write a for loop that reads in up to 100 temperature readings (each is a double value). For each temperature, there will be a pair of lines...
    Input a temperature:76.4
    Reading 1 is 76.4
    Input a temperature:48.3
    Reading 2 is 48.3
    Input a temperature:53.28
    Reading 3 is 53.28
    ....
Keep a sum of all temperatures, so that this sum will be available when the loop ends. Exit the loop prematurely if the sum becomes greater than the constant FIRE or if a negative temperature is entered. In either case, output a line for the temperature that was input and include it in the sum. Then, exit the loop.

double sum=0.0; double temp; for (int index = 1; index <= 100; index++) { System.out.print ("Input a temperature:"); temp = SavitchIn.readLineDouble(); System.out.println ("Reading " + index + " is " + temp); sum += temp; if (sum > FIRE || temp < 0.0) break; }
(6) [15 points] Create the class Auto with four private instance variables for the owner (String), make (String), gallons (double), and miles (int). Create three public methods:
    setAuto() -- If car is an object of Auto,
car.setAuto("Brenda", "Toyota", 12, 7548) would set the four instance variables above in order.
    equals() -- If car and heap are Auto objects,
car.equals(heap) would return true if car and heap have the same make.
    addGas() -- If car has 12.0 gallons,
car.addGas(4.0) would leave car with 16.0 gallons of gas.

public class Auto { private String owner; private String make; private double gallons; private int miles; public void setAuto (String who, String what, double amount, int distance) { owner=who; make=what; gallons=amount; miles=distance; } public boolean equals (Auto car) { return (this.make.equals(car.make)); } public void addGas (double amount) { gallons+=amount; } }
(7) [15 points] Cheryl bought a new truck last week. She particularly liked that it has a driver side seat warmer. There are 2 seat-warmer buttons HIGH and LOW on the console. It works like this:

HIGH -- Pushing HIGH turns the seat warmer to high if it is off or on low. Pushing HIGH turns the seat warmer off it is already on high.

LOW -- Pushing LOW turns the seat warmer to low if it is off or on high. Pushing LOW turns the seat warmer off it is already on low.

But, there is no light or indicator to tell Cheryl whether the seat warmer is on high, on low, or off.

What is a minimal set of button pushes -- in order -- that will...

(a) Ensure that the seat warmer is now on high?
(Hint: Just pushing HIGH does not ensure that the seat warmer is now on high. If it already was on high, pushing HIGH just turned it off!)

LOW-HIGH

(b) Ensure that the seat warmer is now on low?

HIGH-LOW

(c) Ensure that the seat warmer is now off?

LOW-HIGH-HIGH or HIGH-LOW-LOW