Click to reopen instructions.

For a change of pace, this assessment does not require you to write working code though some of the answers will ask you to write bits of code. Instead you will need to read some code and answer questions about it. It is a closed book assessment and you should keep your eyes on your own screen and not talk with your neighbors while you take it. You do not need to submit a pull request this time; your answers are saved automatically when you select from a multiple choice answer or hit enter on a free-form answer.

Questions of

When this code runs, does it call the foo method?

int x = 10;

if (x % 2 == 0) {
  foo();
}

Yes

No

When this code runs, which methods are called?

int x = 37;

if (x < 37) {
  foo(x);
} else {
  bar(x);
}

Just foo

Just bar

Both foo and bar

Neither foo nor bar.

When this code runs, which methods are called?

int x = 37;

if (x * -1 < 37) {
  foo(x);
}
bar(x);

Just foo

Just bar

Both foo and bar

Neither foo nor bar.

When this code runs, which methods are called?

int x = 37;

if (x < 37) {
  foo(x);
} else if (x > 37) {
  bar(x);
}

Just foo

Just bar

Both foo and bar

Neither foo nor bar.

In this code:

int x = 0;
for (int i = 0; i < 10; i++) {
  x = x + i * 10;
}

what part is the “condition” of the for loop.

int x = 0

int i = 0

i < 10

i++

x = x + i * 10

Given this definition:

int n = 100;

How many times does the body of this loop execute?

for (int i = 0; i < n; i++) {
  // code here
}

99

100

101

Given these definitions:

int start = 13;
int end = 29;

How many times does the body of this loop execute?

for (int i = start; i < end; i++) {
  // code here
}

13

14

16

17

29

How many times is the foo method called when this code runs:

for (int i = 0; i < 5; i++) {
  for (int j = 0; j < 4; j++) {
    foo(i, j);
  }
}

4

30

5

20

12

Given this code:

int c = 0;
for (int i = 0; i < 4; i++) {
  for (int j = 0; j < i; j++) {
    c++;
  }
}

what is the value of c after the loop finishes?

4

3

6

10

What is the best description of when this loop will stop?

boolean done = false;

while (!done) {
  double r = Math.random();
  done = r < 0.01;
}

Never, it’s an infinite loop

Immediately, the loop body never executes

When Math.random() returns a number less that 0.01

When Math.random() returns a number greater than 0.01

When done is less than 0.01

In the following code, assume isPrime is a method that says whether its argument is a prime number.

public boolean foo(int limit, int max) {
  int primes = 0;
  for (int n = 2; n < max; n++) {
    if (isPrime(n)) {
      primes++;
    }
    if (primes > limit) {
      return true;
    }
  }
  return false;
}

Which of the following best describes what this method does?

It tests whether limit is a prime number.

It tests whether there are at most max prime numbers below limit.

It tests whether there are more than limit prime numbers below max.

It tests whether limit and max are both prime numbers.

Translate this for loop to an equivalent while loop.

for (int i = 0; i < max; i++) {
  System.out.println(i);
}

Simplify this method without changing its behavior.

public boolean willGetTakeout(boolean hungry, boolean broke) {
  if (hungry && broke) {
    return false;
  } else if (hungry && !broke) {
    return true;
  } else if (!hungry && !broke) {
    return false;
  } else if (!hungry && broke) {
    return false;
  }
}

Write your code here:

public boolean willGetTakeout(boolean hungry, boolean broke) {
}

Write a method table that takes an int argument n and prints out a multiplication table from 1 × 1 to n × n. For instance if called like table(5) it should print something like:

1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
    

What does this method do?

public String mystery(String s) {
  String r = "";
  for (int i = 0; i < s.length(); i++) {
    r = s.substring(i, i + 1) + r;
  }
  return r;

A prefix of a string is any substring that occurs at the beginning of the string. The shortest prefix is the empty string and the longest prefix is the string itself.

Write a method that takes a single String argument and prints out, one per line, all the prefixes of the string.