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)
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().
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