4.5 Review Exercise

Exercise 4.5

  1. The fragment that follows is supposed to print a table of squares of the integers from 1 to 100:
       int i;
       for (i = 1; i < 100; i++);
          println(i + " " + i*i);  
    What would it actually print?
  2. What value(s) of the variable response will stop the following loops?
    1. while (response <= 'a' && response >= 'z' )...
    2. while (response >= 'A' || response <= 'E' )...
    1. What is wrong with the following fragment?
      char response;
      char transCode;
      do
      {
         transCode = getChar("Enter transaction code");
         println("Code entered: " + transCode);
         response = getChar("Is this correct? (Y/N)");
      } while (response != 'Y' || response != 'N'); 
    2. Modify the fragment so that it behaves in a more appropriate way.
  3. How many times will the following loop be executed? Justify your answer.
       int n = 40;
       while (n > 0);
       {
          println(n);
          n /= 2;
       } 
  4. What will be printed by each fragment?
    1. for (i = 0; i <= 5; i++)
         println(i*i); 
    2. n = 3;
      do
         println(++n);
      while (n < 8); 
    3.  for (i = 25; i > 0; i -= 4)
         println(i); 
    4. for (i = j = 1; i < 5; i++)
      {
         j *= i;
         println(j);
      } 
  5. Describe in a few words what would be stored in the variable value after execution of each fragment.
    1. for (i = value = 0; i <= 10; i++)
         value += i; 
    2. n = getlnt("");
      value = 0;
      while (n % 2 == 0)
      { 
         value++;
         n /= 2; 
      }
    3. n = getInt("");
      value = 0;
      do
      {
         value += n % 10;
         n /= 10;
      }
      while (n > 0);
  6. A prime number is a positive integer that has exactly two distinct divisors - one and the number itself. For example, 17 is a prime number because it is divisible only by 1 and 17. Write a program that reads an integer and then prints a message stating whether or not it is a prime number.
  7. Write a program that prompts the user for a natural number and, once one has been supplied, finds and prints all of its exact divisors.
    1. Write a program that could be used to play a simple game. The program should first ask one user to supply a positive integer less than 1000. Once this user has provided an appropriate value, the program should than ask a second user to guess the number that was given by the first user. After each incorrect guess, the program should tell this user whether the guess was too low or too high and ask for another guess. This should continue until the second user has found the correct number. The program should then print the number of guesses that the second user needed to determine the number.
    2. What guessing strategy will minimize the expected number of incorrect guesses?
  8. Let n be a positive integer consisting of the digits dk ... d1d0. Write a program that reads a value of n and then prints its digits in a column, starting with d0. For example, if n = 3467, then the program should print:
    7
    6
    4
    3
  9. Rewrite the program in the previous question so that it prints the digits of n in the opposite order, starting with dk and working down to d0.
  10. State the output from the following program:
    for (int i = 3; i <= 5; i++)
      for (int j = i; j >= 1; j--)
      {
         if (j % 2 == 0)
           println();
         print(j);
      } 
  11. State the output from the following program:
    for (int i = 3; i > 0; i--)
    {
      for (int j = 0; j <= i; j++)
        print(i + j);
      println();
    }