CSCE 146 Spring 2001
FIRST MIDTERM EXAM
Tuesday 01/02/20
Chapter 1: The Phases of Software Development
Short Answer---three points each
- What is a precondition? What is a
postcondition?
A
precondition is a condition that is supposed to be true when a method is
called. A postcondition is a statement
that described what is true when a method call is completed.
- Which tool can provide nicely formatted
documentation about how a method works?
javadoc
- Give a concise formula that gives the
approximate number of digits in a positive integer. The integer is written
in base 10.
Floor(log
n) + 1
Multiple
Choice---one point each
- Why is writing easily modifiable code
important?
- A. Easily modifiable
code generally has a quicker run time.
- B. Most real world
programs require change at some time.
- C. Most text editors
make it easy to modify code.
- D. Several people may
be writing the same function at the same time.
B
- Answer true or false for this statement: When
programming in teams, the specification of a method must be written by the
person writing the code for the method.
FALSE
- If the precondition fails, it is a good idea to
throw an exception that will generally halt the program. Why is the
program halted?
- A. The Java Runtime
System forbids continuation.
- B. The method is no
longer guaranteed to make the postcondition true.
- C. The method's memory
requires have become exponential (or worse).
- D. The method's
running time has become exponential (or worse).
B
- Which of these function calls will cause an
exception to be thrown when x is 42. (x is an int variable).
o
o if (0 < x)
o throw new IllegalArgumentException("Bad x");
o if (0 == x)
o throw new IllegalArgumentException("Bad x");
o if (0 > x)
o throw new IllegalArgumentException("Bad x");
- D. None of the above
will cause an exception when x is 42.
A
- What does a run-time analysis usually count?
- A. The number of
arithmetic and other operations required for the program to run
- B. The number of
megabytes required for the program to run
- C. The number of
seconds required for the program to run
- D. The number of
seconds plus the number of megabytes
- E. The number of
seconds times the number of megabytes
A
- Which of these is the correct closed-form
expression for 1+2+3+...+n?
- A. 2n
- B. (n2+n)/2
- C. (n3+n)/2
- D. n log n
B
- Why is it important to test boundary values
when testing programs?
- A. Calculating by
hand, it's easy to find the right answers for boundary values.
- B. Debuggers are
easier to use when testing boundary values.
- C. In practice, a
large proportion of errors arise from boundary values.
- D. The correct
execution of a function on all boundary values proves a function is
correct.
C
Chapter 2: Abstract Data Types and Java Classes
Short Answer---three points each
- Here is part of the Throttle declaration:
public class Throttle
{
public Throttle(int size)
...
public double getFlow()
...
public void shift(int amount)
...
}
Write several lines of Java code to create a
Throttle called quiz with 100 positions, activate the method that shifts quiz's
flow to the halfway point, and then print the current flow from quiz.
Answer: Throttle quiz = new Throttle(100);
quiz.shift(50); System.out.println(quiz.getflow());
- Part of the Throttle implementation from
Chapter 2 is shown below. Mark each method method as follows: Mark C for
any constructor; mark M for any modification method. Mark A for any
accessor method.
public class Throttle
{
public Throttle(int size)... C
public double getFlow( )... A
public boolean isOn( )... A
public void shift(int amount)... M
public void shutOff( )... M
}
- Here is a method with a parameter named spot,
which is a reference to a Location object:
public static foo(Location spot)
{
spot.shift(2,0);
}
Now, suppose that s is a reference to a Location
with s.getX( ) equal to 40. Then the method foo(s) is activated. What is the
value of s.getX( ) after this activation?
42
- Here is a method with a parameter named spot,
which is an integer:
public static foo(int spot)
{
spot += 2;
}
Now,
suppose that s is an integer with a value of 40. Then the method foo(s) is activated.
What is the value of s after this activation?
40
- Suppose I implement a class with a clone
method. How should I modify the class head to inform the Java compiler
that you plan to implement the clone method.
Add; implements cloneable
Multiple Choice---one
point each
- Suppose I create two Throttles using the class
from Chapter 2:
Throttle mower = new Throttle(...);
Throttle copter = new Throttle(...);
Which
statement best describes the instance variable called top for these two
Throttles:
- A. mower.top and
copter.top will always be two separate instance variables.
- B. mower.top and
copter.top will only be separate if the two throttles are created with a
different number of positions.
- C. mower.top and
copter.top will never be two separate instance variables.
A
- Is it possible for a method of a class to
activate another method of the same class?
- A. No.
- B. Yes, but only
public methods.
- C. Yes, but only
private methods.
- D. Yes, both public
and private methods can be activated within another method.
D
- What is the common pattern of class definitions
that is used in Chapter 2?
- A. Methods and
instance variables are both private.
- B. Methods are
private, and instance variables are public.
- C. Methods are public,
and instance variables are private.
- D. Methods and
instance variables are both public.
C
- What is the primary purpose of a constructor?
- A. To allow multiple
classes to be used in a single program.
- B. To copy an actual
argument to a method's parameter.
- C. To initialize each
object as it is declared.
C
- Suppose that the Foo class does not have a
clone method. What happens when an assignment
x=y;
is given for two Foo
objects?
- A. x is set to refer
to the a copy of y's object
- B. x is set to refer
to the same object that y refers to
- C. Compiler error
- D. Run-time error
B
- Suppose that the Foo class has a clone method.
What typically happens when an call
x=(Foo) y.clone( );
is given for two Foo
objects?
- A. x is set to refer
to the a copy of y's object
- B. x is set to refer
to the same object that y refers to
- C. Compiler error
- D. Run-time error
A
- Consider this class definition:
public class Quiz
{
private double score;
public int f( )...
public static int g( )...
}
Where
could the assignment score=1.0;
appear for the private instance variable score
?
- A. Both f and g can
carry out the assignment.
- B. f can carry out the
assignment, but not g.
- C. g can carry out the
assignment, but not f.
- D. Neither f nor g can
carry out the assignment.
B
- Here is a small method implementation, using
the Location type from Chapter 2:
public static void f(int i, Location k)
{
i += 1;
k.shift(2, 0);
}
Suppose
that a main program has an integer variables a (equal to zero), and a Location
object b (with b.getX( ) equal to zero). Then the main program calls f(a,b);
What are the values of a
and b.getX( ) after the method f finishes?
- A. Both a and
b.getX( ) are still 0.
- B. a is now 1, but
b.getX( ) is still 0.
- C. a is still 0, but
b.getX( ) is now 2.
- D. a is now 1, and
b.getX( ) is now 2.
C
- Suppose that the Foo class does not have an
equals method. What happens when an expression
x==y;
is evaluated for two
Foo objects?
- A. The expression is
true if x and y refer to the same object
- B. The expression is
true if x and y refer to objects with the same values
- C. Compiler error
- D. Run-time error
A
- Suppose that the Foo class has a typical equals
method. What happens when an expression
x.equals(y);
is evaluated for two
Foo objects?
- A. The expression is
true if x and y refer to the same object
- B. The expression is
true if x and y refer to objects with the same values
- C. Compiler error
- D. Run-time error
B
Chapter 3: Collection Classes
Short Answer---three points each
- What happens if you call
new
but the heap is out of
memory?
The exception
OutOfMemoryError is raised.
- Suppose that you are storing a collection of
items in a partially-filled array. You will declare the array with some
initial size. What else must you keep track of?
The number of elements in
the collection.
- Consider the Bag from Chapter 3 (using an
array). What happens if the insert method is activated, and the array is
already full? Does the method work even if the array had a weird length of
zero?
The capacity is
approximately doubled. Yes: the
capacity is set to one.
- Suppose that I have the following declarations:
int[ ] data = new int[100];
int i;
Write
a small segment of Java code that will shift data[50]...data[98] up one spot to
the locations data[51]...data[99]. Then insert the number 42 at location
data[50]. Use a for loop (not System.arraycopy).
For (I = 98; I > 49; I--)
data[I+1] = data[I]; data[50] = 42;
- Suppose that I have the following declarations:
int[ ] data = new int[100];
int i;
Write
a small segment of Java code that will shift data[51]...data[99] down one spot
to the locations data[50]...data[98]. The value of data[99] remains at its
original value. Use a for loop (not System.arraycopy).
For (I =51; I < 99; I++)
data[I-1] = data[I];
Multiple
Choice---one point each
- Suppose I have
int b = new int[42]
. What are the highest
and lowest legal indexes for b?
- A. 0 and 41
- B. 0 and 42
- C. 1 and 41
- D. 1 and 42
A
- Who needs to know about the invariant of an
ADT?
- A. Only the programmer
who implements the class for the ADT.
- B. Only the programmer
who uses the class for the ADT.
- C. Both the programmer
who implements the class and the programmer who uses the class.
- D. Neither the
programmer who implements the class nor the programmer who uses the
class.
A
- Suppose that the Bag class is efficiently
implemented with an array with a capacity of 4000, as in Chapter 3 of the
class notes. Choose the best description of b's instance variables after
we execute these statements:
Bag b;
b.add(5);
b.add(4);
b.add(6);
What
will be the values of b.manyItems and b.data after the statements?
- A. b.manyItems is 3,
b.data[0] is 4, b.data[1] is 5, and b.data[2] is 6
- B. b.manyItems is 3,
b.data[0] is 5, b.data[1] is 4, and b.data[2] is 6
- C. b.manyItems is 3,
b.data[0] is 6, b.data[1] is 4, and b.data[2] is 5
- D. b.manyItems is 3,
b.data[0] is 6, b.data[1] is 5, and b.data[2] is 4
B
- I have an array named data, which contains n
integers. I want to print all of the numbers, starting at data[0]. BUT if
the number 42 occurs, then I want to stop my printing just before the 42
(without printing the 42!) Here is most of my for-loop to accomplish my
goal:
for (i = 0; ___________________________________________; i++)
System.out.println(data[i]);
What
is the correct way to fill in the blank? (If there is more than one correct
answer, please select E.)
- A.
(data[i] != 42)
&& (i < n)
- B.
(data[i] != 42) || (i
< n)
- C.
(i < n) &&
(data[i] != 42)
- D.
(i < n) || (data[i]
!= 42)
- E. More than one of
the above answers is correct.
C
- In Chapter 3, there was a suggested
implementation of the Sequence class with these private member variables:
class Sequence
{
int[ ] data;
int manyItems;
int currentIndex;
};
The
Sequence's constructor creates a new array for data to refer to, but does not
place any values in the data array. Why?
- A. The first
activation of addAfter or addBefore initializes the entire array.
- B. The array
initialization was part of the project that was left to the student.
- C. The programmer who
uses the Sequence is responsible for initializing the array.
- D. When a Sequence is
first created, none of the array is being used.
D
- Suppose the Bag and Sequence are both
implemented with an array as in Chapter 3. What is the difference between
the private instance variables of the Bag and the private instance
variables of the Sequence?
- A. The Bag has an
extra array of integers.
- B. The Bag has one
extra int instance variable.
- C. The Sequence has an
extra array of integers.
- D. The Sequence has
one extra int instance variable.
D
- What kind of object is created to wait for
actions such as a button click?
- A. ActionListener
- B. Canvas
- C. Event
- D. TextArea
- E. TextField
A
Chapter 4: Linked Lists
Short Answer---three points each
- What is the single instruction needed to insert
a new item (say, newData) at the head of a linked list of IntNodes?
Head = new IntNode(newData,
head).
Multiple
Choice---one point each
- Suppose cursor refers to a node in a linked
list (using the IntNode class with instance variables called data and
link). What statement changes cursor so that it refers to the next node?
- A. cursor++;
- B. cursor = link;
- C. cursor += link;
- D. cursor =
cursor.link;
D
- Suppose cursor refers to a node in a linked
list (using the Node class with instance variables called data and link).
What boolean expression will be true when cursor refers to the tail node
of the list?
- A.
(cursor == null)
- B.
(cursor.link == null)
- C.
(cursor.data == null)
- D.
(cursor.data == 0.0)
- E. None of the above.
B
- Which boolean expression indicates whether the
numbers in two IntNodes (p and q) are the same? Assume that neither p nor
q is null.
- A. p == q
- B. p.data == q.data
- C. p.link == q.link
- D. None of the above.
B
- Suppose that p is a reference variable that
contains the null reference. What happens at runtime if the program tries
to activate a method of p?
- A.
IllegalArgumentException
- B.
IllegalStateException
- C.
NullPointerException
- D. The results are
unpredictable.
C