How to Answer a Programming Question on a Test

Consider the following possible test question.

Write a Java method that receives an int parameter n and returns n/2 if n is even, and n × 2 if n is odd.

A good solution to this is:

int answer(int n) {
  if (n % 2 == 0)
    return n / 2;
  else
    return n * 2;
} // answer()

Things to consider for those who do not want to lose points or for those who want partial credit:

Creative Commons License