CS 5 Midterm practice problems

First Midterm on Friday, October 7


Here are five practice problems for the midterm exam on October 7, 2005. You will be able to pick up the real midterm exam after Friday at 8pm on the 7th from the bin beside my office in Olin 1241.

The midterm exam itself will be worth 100 points (the equivalent of 2 HWs) and will consist of 4 problems. It will have a time limit of 3 hours.

The midterm exam is closed-book and is to be taken without the help of outside resources or the use of a computer. However, you may use a double-sided page of notes that you have prepared beforehand. You should hand in the notes with your exam.

The exam is due to me by midnight on Sunday, Oct 9th, 2005. Simply slide it under my door.

Practice Problems

Problem 1

Write a java program that prompts the user to type two words. The program should then print out the two words in reverse order and with their letters reversed. For example, if the input words were

computer
science
The output should be
ecneics
retupmoc

You don't have to worry about the lines

class CS5App
{
starting your program -- you can simply start with
void main()
{
  // put your code here
}
There is no need to write out the full signature of the main method. (It's given below in the example programs.)

You may assume the user will input two words and hit return after each. As a result, you may use either nextWord() or nextLine() to take in the two words.

Recall that if s is a String, then

 s.charAt(i)
returns the ith character of the String s. Also,
 s.length()
returns the length of the String s.



Problem 2

Write a java program that continues to prompt the user to input an integer and then reads in an integer. The program should only stop taking input when the user types a -1.

After the program stops taking input, it should print two things:

Here is an example run of the program:

  Please type an integer (-1 to stop):  1
  Please type an integer (-1 to stop):  2
  Please type an integer (-1 to stop):  3
  Please type an integer (-1 to stop):  4
  Please type an integer (-1 to stop):  25
  Please type an integer (-1 to stop):  -1

  There were 5 integers entered.

  Their average is 7.0



Problem 3

What will this program print if a user types in the string

fcllqvfctc
when prompted for input?

Here is the code:

public static void main(String[] s)
{
  H.out.println("Type a string at the prompt:  ");
  String s = H.in.nextWord();
  H.out.println();

  for (int i=0 ; i < s.length() ; ++i)
  {
    char c = mixedUpChar( s.charAt(i) );
    H.out.print( c );
  }
  H.out.println();
}

public static char mixedUpChar(char c)
{
  char result = c;

  if (c <= 'm')
  {
    result += 2;
  }

  if (result >= 'm')
  {
    result -= 2;
  }
  
  return result;
}



Problem 4

What does this program print if a user types in the string

disappearing
when prompted for input?

Here is the code:

public static void main(String[] s)
{
  H.out.print("Type a string at the prompt:  ");
  String s = H.in.nextWord();
  H.out.println();

  for (int i=0 ; i < s.length() ; ++i)
  {
    for (int j=i ; j < s.length() ; ++j)
    {
      H.out.print( s.charAt(j) );
    }
    H.out.println();
  }

}



Problem 5

(A) Write a java program that uses one or more for loops to print out the first 10 terms of this sequence:

2 4 8 16 32


(B) Write a java program that uses one or more for loops to print out the first 10 terms of this sequence:
2 44 888 16161616 3232323232
Notice that the basic terms are the same as in part (A), but here there are two of the second term (that is, two fours), three of the third term (that is, three eights), etc.