Loyola College in Maryland

CS 201 - Computer Science I
Fall 2008


Loyola College > Department of Computer Science > CS 201 > Homework > Exam 2 Practice Problems > Solutions

Problem 1: Give a brief definition of each of the following.


Problem 2: Suppose we have the rank of a card in the rank variable and the suit in the suit variable. Assume that both variables are ints and that there are constants such as RANK_KING and SUIT_CLUBS declared. Write a code fragment that determines the value the card would have in the game of Hearts and saves that value in the variable pointValue (also an int). pointValue should be 1 if the card is a heart, 13 if the card is the queen of spades, -10 if the card is the jack of diamonds, and 0 otherwise.

  int pointValue;

  if (suit == SUIT_HEARTS)
  {
    pointValue = 1;
  }
  else if (suit == SUIT_SPADES && rank == RANK_QUEEN)
  {
    pointValue = 13;
  }
  else if (suit == SUIT_DIAMONDS && rank == RANK_JACK)
  {
    pointValue = -10;
  }
  else
  {
    pointValue = 0;
  }

Problem 3: The following code fragment is intended to return the area, in hectares, of a rectangular plot of land given the coordinates (in meters) of either pair of opposite corners of the plot. As written, it does not accomplish its task. (Note: there are 10000 square meters in a hectare.)

  Scanner scan = new Scanner(System.in);
  System.out.println("Enter coordinates of opposite corners:");
  int x1 = scan.nextInt();
  int y1 = scan.nextInt();
  int x2 = scan.nextInt();
  int y2 = scan.nextInt();

  final int SQ_METERS_PER_HECTARE = 10000;

  int width = x2 - x1;
  int height = y2 - y1;

  if (width < 0)
  {
    width = -width;
  }
  else if (height < 0)
  {
    height = -height;
  }

  System.out.println(width / 10000 * height; (double)width * height / SQ_METERS_PER_HECTARE);
}

Problem 4: The following code fragment was purposefully formatted poorly and has errors. Assume that x and y were properly declared and initialized.

  int z;
  z = 2;

  if (x == 1 || y > 0)

  if (y < x)
  {
  if (x > 0)
    z = 1;
  }
  else
    z = 0;

  System.out.println(z);

Problem 5: Rewrite the following code fragment using a switch statement. Assume that lines has been properly declared and initialized.

int score;
if (lines == 1)
{
  score = 100;
}
else if (lines == 2)
{
  score = 300;
}
else if (lines == 3)
{
  score = 700;
}
else if (lines == 4)
{
  score = 1500;
}
else
{
  score = 1500 + 1000 * (lines - 4);
}
int score;

switch (lines)
{
  case 1:
    score = 100;
    break;

  case 2:
    score = 300;
    break;

  case 3:
    score = 700;
    break;

  case 4:
    score = 1500;
    break;

  default:
    score = 1500 + 1000 * (lines - 4); // formula would work for case 4 too
    break;
}

Problem 6: Consider the following code fragment, which purportedly computes the sum of the first n odd integers. Assume that n has been properly declared and initialized.

  // compute the sum of the first n odd integers

  int sum = 0;
  int newSum = 0;
  for (int i = 0; i <= n; i++)
  {
    newSum = (i + 2) + sum;
    newSum += i;
  }
  System.out.println(newSum);
For each of the following values of n, give the value displayed by the code fragment. (Remember, the code does not do what the comment claims it does.)

Problem 7: Consider the following code fragment, which purportedly computes the number of digits in the decimal representation of n. Assume that n was properly declared and initialized.

  // compute the number of digits in n

  int digits = 0;
  do
  {
    digits++;
  } while (n % 10 > 0);

  System.out.println(digits);
When answering the following questions, keep in mind that the code does not do what the comment claims it does.

Problem 8: For each of the following sequences of statements, write loops that are equivalent. Each answer should have only one line that invokes the drawLine method.
g.drawLine(0, 0, 100, 100);
g.drawLine(20, 0, 80, 100);
g.drawLine(40, 0, 60, 100);
g.drawLine(60, 0, 40, 100);
g.drawLine(80, 0, 20, 100);
g.drawLine(100, 0, 0, 100);

for (int l = 0; l < 6; l++)
{
  g.drawLine(l * 20, 0, 100 - l * 20, 100);
}
g.drawLine(0, 0, 20, 0);
g.drawLine(0, 10, 20, 10);
g.drawLine(0, 20, 20, 20);
g.drawLine(40, 0, 60, 0);
g.drawLine(40, 10, 60, 10);
g.drawLine(40, 20, 60, 20);
g.drawLine(0, 40, 20, 40);
g.drawLine(0, 50, 20, 50);
g.drawLine(0, 60, 20, 60);
g.drawLine(40, 40, 60, 40);
g.drawLine(40, 50, 60, 50);
g.drawLine(40, 60, 60, 60);
for (int col = 0; col < 2; col++)
{
  for (int row = 0; row < 2; row++)
  {
    for (int line = 0; line < 3; line++)
    {
      g.drawLine(col * 40, row * 40 + line * 10, col * 40 + 20, row * 40 + line * 10);
    }
  }
}

Problem 9: Write code fragments to do each of the following.