CS2110 Prelim1 Prep
CS2110 Prelim1 Prep
know the two reasons for having wrapper classes (be able to treat a primi- tive-type value as an object;
provide useful static fields and methods)
Prelim 1
CS 2110, 3 October 2019, 5:30 PM
1 2 3 4 5 6 7 Total
Question Name Short OO Recursion Loop Exception new-
answer invariants handling exp
Max 1 42 20 14 8 12 3 100
Score
Grader
The exam is closed book and closed notes. Do not begin until instructed.
You have 90 minutes. Good luck!
Write your name and Cornell NetID, legibly, at the top of the first page, and your Cornell
ID Number (7 digits) at the top of pages 2-7! There are 6 questions on 7 numbered pages,
front and back. Check that you have all the pages. When you hand in your exam, make sure
your pages are still stapled together. If not, please use our stapler to reattach all your pages!
We have scrap paper available. If you do a lot of crossing out and rewriting, you might want
to write code on scrap paper first and then copy it to the exam so that we can make sense of
what you handed in.
Write your answers in the space provided. Ambiguous answers will be considered incorrect.
You should be able to fit your answers easily into the space provided.
In some places, we have abbreviated or condensed code to reduce the number of pages that
must be printed for the exam. In others, code has been obfuscated to make the problem more
difficult. This does not mean that it’s good style.
Academic Integrity Statement: I pledge that I have neither given nor received any unau-
thorized aid on this exam. I will not talk about the exam with anyone in this course who has
not yet taken prelim 1.
(signature)
1. Name (1 point)
Write your name and NetID, legibly, at the top of page 1. Write your Student ID Number (the 7
digits on your student ID) at the top of pages 2-7 (so each page has identification).
Cornell ID Number (7 digits):
(a) 6 points. Below are three expressions. To the right of each, write its value.
" "
1. 2 + 3 + "abc" + 1 + 4 Sabc 14
2. "abc".substring(0,1).indexOf('b')
2 of 7
Cornell ID Number (7 digits):
(d) 3 points To the right, class S has one field public class S {
and a constructor. Consider this new-expression: private int a= 2;
(e) 8 points. Implement function isReverse according to its specification below. Do not use
recursion. Do not create any more Strings.
(f ) 6 points. Write five (5) distinct test cases based on the specification of isReverse given in
part (e) above (black box testing). We don’t need formal assertEquals calls. Just specify what a
and b are in each case and state what the function should do/return in each case (exception, true,
or false).
3 of 7
Cornell ID Number (7 digits):
4 of 7
Cornell ID Number (7 digits):
(b) 10 points. To the right is the beginning class Rabbit extends Animal {
of class Rabbit, which extends class Animal of // how high it jumps
part (a). It has one field. Below complete the private int height;
constructor, method addToDiet, and method
equals, all of which go in class Rabbit.
/** Return true if this and ob are objects of the same class
* and have the same name and height. */
@Override public boolean equals(Object ob){
}
}
(a) 4 points Method F to the right calculates /** Return Fibonacci number n.
Fibonacci numbers. Below, write the calls made in * Precondition: 0 <= n. */
evaluating the call F(3) in the order they are called, public static int F(int n) {
starting with F(3) if (n == 0 || n == 1) return 1;
return F(n-1) + F(n-2);
}
5 of 7
Cornell ID Number (7 digits):
(b) 2 points Write the loop condition B so that the loop terminates properly when B is false.
Do not write a while loop; just write the loop condition.
(c) 4 points Write the repetend so that it makes progress toward termination and keeps the
invariant true. Do not write a while loop; just write the repetend.
6 of 7
Cornell ID Number (7 digits):
Consider method mystery. Function s.toString() returns the value of s. To the right of the
method are four calls on mystery. Under each write (1) the output printed by the call (on one line
is OK), including any exception that is not caught, and (2) the value returned by the call (if there
is one).
7. New-expression (3 Points)
Write the 3-step algorithm for evaluating the new-expression new ABC(5) .
7 of 7
Name: NetID:
Prelim 1, Questions
CS 2110, 27 September 2018, 5:30 PM
1 2 3 4 5 6 Total
Question Name Short Exception Recursion OO Loop
answer handling invariants
Max 1 34 8 16 31 10 100
Score
Grader
The exam is closed book and closed notes. Do not begin until instructed. This handout
contains the questions; you will answer the questions on the accompanying answer handout.
Write your name and Cornell NetID, legibly, at the top of every page of the answer
handout! There are 6 questions on 5 numbered pages, front and back. Check that you have
all the pages. When you hand in your exam, make sure your pages are still stapled together.
If not, please use our stapler to reattach all your pages!
You can use the back of the last page of this question handout as scrap paper. We have scrap
paper available. If you do a lot of crossing out and rewriting, you might want to write code
on scrap paper first and then copy it to the exam so that we can make sense of what you
handed in.
Ambiguous answers will be considered incorrect. Your answers should fit easily into the space
provided.
In some places, we have abbreviated or condensed code to reduce the number of pages that
must be printed for the exam. In others, code has been obfuscated to make the problem more
difficult. This does not mean that it’s good style.
1. Name (1 point)
Write your name and NetID, legibly, at the top of every page of the answer handoout.
1 of 5
Name: NetID:
(c) 7 points. Use classes Animal and Cow below to answer the question on the answer sheet.
(d) 6 points. Implement function isEqual whose specification is given on the answer sheet. Here
are examples:
2 of 5
Name: NetID:
On the answer handout, write the output to the console for the three statements foo(-1,-1);,
foo(0,0);, and foo(1,1);. Procedure foo is given below.
catch (ArithmeticException e) {
System.out.println("5");
if (b == 1) throw new RuntimeException();
System.out.println("6");
}
catch (Exception e) {
System.out.println("7");
}
System.out.println("8");
}
(a) 8 points Recursive function hailstone is given below. Execute the following three calls,
and for each write down on the answer handout what is printed. But on each, stop after 4 println
statements have been executed.
3 of 5
Name: NetID:
(b) 8 points The answer handout contains a recursive procedure comify; complete its method
body. You can use function p, given below. You must use recursion; do not use a loop!
4 of 5
Name: NetID:
(a) 5 points Recall that interface Comparable declares abstract function compareTo(Object ob).
This function is declared in class Chef. On the answer sheet, complete its body and explain one
other necessary change to class Chef so that it implements interface Comparable.
(b) 12 pts Class BakingChef on the answer handout extends Chef. On the answer sheet,
(1) Complete the body of the constructor.
(2) Complete the return statement in function canMakeFood, assuming the two boolean variables
are assigned properly (you do not have to do this).
(3) Complete the body of procedure makeFood.
(c) 5 points On the answer sheet, complete the body of function toString, which is to be added
to class BakingChef. The result should contain the name of the chef and the size of the inventory.
You could just return those two values with a space between them.
(d) 9 points Class CakeChef (on the answer sheet) is a subclass of BakingChef. It has three
syntax errors, so it doesn’t compile. On the answer sheet, Identify the three syntax errors.
Below are the precondition, postcondition, and invariant of a loop that swaps the negative values
in b[h..k] to the end of b[h..k]. Note that h is not necessarily 0, k is not necessarily b.length 1, and
both h and k should not be changed. You do not have to be concerned with declaring variables.
h k
Precondition: b ?
h t k
Postcondition: b >= 0 <0
h u t k
Invariant: b ? >= 0 <0
5 of 5