Docsity

CS 100J Prelim 2 Answers - Prof. D. Gries, Assignments of Computer Science

Toggle sidebar
bg1
CS 100J Prelim 2 Answers 13 March 2008
Have a good break!!!
Question 0. (answer omitted)
Question 1. (a)
(b) Variable i and k are created when the frame for
the call Numbers.f(1)is created. Variable s
is created when the frame for the call p(i+1) is
created.
Question 2.
/** = number of female ancestors of r
(= 0 if r is null) */
public static int fa(Rhino r) {
if (r == null) return 0;
return fa(r.father) +
(r.mother == null ? 0 : 1 + fa(r.mother));
}
/** = n**p. p can be any integer. */
public static double pow(double n, int p) {
if (p == 0) return 1;
if (p < 0) return 1 / pow(n, -p);
return n * pow(n, p-1);
}
Question 3(a).
// Store in x the length of the longest String in v.
int x= v.get(0).length();
// invariant: 1 <= k && x = length of
// longest string in v[0..k-1]
for (int k= 1; k < v.size(); k= k+1) {
if (v.get(k).length() > x)
x= v.get(k).length();
}
// x = length of longest string in v
Question 4.
(a) 1: 5. 2: 11. 3: 10. 4: true. 5: false. 6: true. 7: true.
8: 4.
(b) 1: One, One. 2: One, Two.
Question 5. (a) A parameter is a variable that is de-
clared within the parentheses of a method header. An
argument is an expression that appears within the
parentheses of a method call.
(b) /** Constructor: An instance with
temperature 0 on day d*/
public C(Day d) {
this(0, d);
}
(c) Write the cast as (Cat) v . This will not produce an
error if the real class of v is Cat or a subclass of Cat.
f: 1 Numbers
i k
1
p: 2 Numbers
n s
2
4

Partial preview of the text

Download CS 100J Prelim 2 Answers - Prof. D. Gries and more Assignments Computer Science in PDF only on Docsity!

CS 100J Prelim 2 Answers 13 March 2008

Have a good break!!!

Question 0. (answer omitted)

Question 1. (a)

(b) Variable i and k are created when the frame for

the call Numbers.f(1)is created. Variable s

is created when the frame for the call p(i+1) is

created.

Question 2.

/** = number of female ancestors of r

(= 0 if r is null) */

public static int fa(Rhino r) {

if (r == null ) return 0;

return fa(r.father) +

(r.mother == null? 0 : 1 + fa(r.mother));

/** = n**p. p can be any integer. */

public static double pow( double n, int p) {

if (p == 0) return 1;

if (p < 0) return 1 / pow(n, - p);

return n * pow(n, p-1);

Question 3(a).

// Store in x the length of the longest String in v.

int x= v.get(0).length();

// invariant: 1 <= k && x = length of

// longest string in v[0..k-1]

for ( int k= 1; k < v.size(); k= k+1) {

if (v.get(k).length() > x)

x= v.get(k).length();

// x = length of longest string in v

Question 4.

(a) 1: 5. 2: 11. 3: 10. 4: true. 5: false. 6: true. 7: true.

(b) 1: One, One. 2: One, Two.

Question 5. (a) A parameter is a variable that is de-

clared within the parentheses of a method header. An

argument is an expression that appears within the

parentheses of a method call.

(b) /** Constructor: An instance with

temperature 0 on day d*/

public C(Day d) {

this (0, d);

( c) Write the cast as (Cat) v. This will not produce an

error if the real class of v is Cat or a subclass of Cat.

f: 1 Numbers

i k 1

p: 2 Numbers

n s 2 4