Quiz 01 sample questions
The quiz will have three questions.
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;
}
Here is another example:
int mystery(char c, String s) {
int x = 0;
for (char k : s.toCharArray()) {
if (c != k) {
x++;
}
}
return x;
}
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;
}
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;
}
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 licenseNumber
s are equal.
Your definition should include the constructor and the equals
method, but should elide the getters and setters.
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)
.
You can check your answers here.