Self-assessment 01: Sample answers

In the first question, you will read some code and write a concise, high-level, English description of the code’s action. Here is an example:

double mystery(int[] a) {
  if (a.length == 0) {
    return 0.0;
  }

  double x = 0.0;
  for (int i: a) {
    x += i;
  }
  return x / a.length;
}

A correct answer would read “The mystery method returns the mean of the values stored in its parameter a, or 0.0 if the a is empty.”

A partially correct answer (about 12 credit) might read “The mystery method returns the mean of the values stored in the parameter a” or “it computes the mean.” This answer loses points because it omits an important detail of the method’s behavior.

A partially correct answer (about 13 credit) might read “It checks if the length is zero then returns zero otherwise it declares a variable x then uses a for loop to add up the values of the array into x, then divides by the length of the array and returns it.” This answer loses points because it is not concise, and because it mechanically transcribes the code’s action into words, rather than providing a high-level description of the code.

Here is another example:

int mystery(char c, String s) {
  int x = 0;
  for (char k : s.toCharArray()) {
    if (c != k) {
      x++;
    }
  }
  return x;
}

A correct answer would read “The mystery method returns the number of characters in s that aren’t equal to c”.


In the second question, you’ll identify a logic error in a short snippet of code. That is, you will find a conceptual error, not an error that the compiler would catch. Here is an example:

/** Return the mean of the values stored in `a`. */
double mean(int[] a) {
  if (a.length == 0) {
    return 0.0;
  }

  double x = 0.0;
  for (int i = 0; i < a.length; i++) {
    x += i;
  }
  return x / a.length;
}

A correct answer would read “The method mistakenly sums the for loop’s i (index), rather than the value at cell i (a[i]).”

Here is another example:

/** Return the sum of the odd numbers from 0 to n, inclusive. */
int sumOddUpTo(int n) {
  int sum = 0;
  for (int i = 0; i <= n; i++) {
    if (i % 2 == 0) {
      break;
    }
    else {
      sum += i;
    }
  }
  return sum;
}

A correct answer would read “The body of the if statement mistakenly exits the loop (using break rather than continue), so that this method will always return 0.”


In the final question, you will write a short class or method according to a textual description. For example, a question might state:

Define a class Dog. A Dog instance has a name (which could change), a breed (which is immutable, that is, it cannot change) and a licenseNumber (an integer between 1 and 1,000,000, which is immutable). Two Dog instances are considered equal if and only if their licenseNumbers are equal.

Your definition should include the constructor and the equals method, but should elide the getters and setters.

A correct answer might look something like:

public class Dog {
  private String name;
  private final String breed;
  private final int licenseNumber;

  public Dog(String n, String b, int l) {
    name = n;
    breed = b;
    licenseNumber = l;
  }

  public boolean equals(Dog o) {
      return (licenseNumber == o.licenseNumber);
  }
}

Another question might state:

Write a method which, given two int arrays, determines whether the values in the first array a are a subset of the values in the second array b. In other words, return true if and only if every value in a is also present in b. Your method should have the signature boolean isSubset(int[] a, int[] b).

A correct answer would be:

boolean isSubset(int[] a, int[] b) {
 for (int i: a) {
   boolean iFound = false;
   for (int j: b) {
     if (i == j) {
       iFound = true;
       break;
     }
   }     
   if (!iFound) {
     return false;
   }
 }
 return true;
}

Full credit for code writing will generally be granted for answers that are correct, and that do not include extraneous (unnecessary) statements and expressions. Minor syntax errors will incur a penalty of somewhere between 110 and 13 credit, depending upon their extent and severity. Logical errors will incur a penalty of between 14 and all credit, again depending upon their extent and severity.