V22.0002
Intro. to Computers & Programming
Summer 2009
Practice Final 2
  1. (10 points) What does the following code produce?
    public class WhatDoIDo {
    
          public static void main (String [] args) {
               print(8);
               print(3);
          }
    
          public static void print(int number) {
               int index, uScores, numToShow;
    
               numToShow = formula(number);
    
               for(index = 1; index <= number; index++) {
                     for(uScores = 1; uScores <= index; uScores ++)
                          System.out.print("_");
                     System.out.println(numToShow);
               }
          }
    
          public static int formula(int value) {
               return (value % 3) + 1;
          }
    }
    
  2. (15 points) The following code contains at least 5 errors (syntactic and logical). Identify them (comments and formatting or lack thereof are not errors).
    public class MyMax {
    
          public static void main(String args[]) {
                int max = maximum(5, 7);
                System.out.println("Maximum is: " + maximum(5, 7, 3));
                System.out.println("Maximum is: ", + max);
          }
    
          public static int maximum (int x, int y, int z) {
                int max = x;
                if (y > max);
                      max = y;
    
                if (z > max)
                      max = z;
    
                return max;
          }
    
          public static int maximum (int x, y) {
                if (x > y)
                      return x;
          }
    
  3. (3 points each)
    1. Write a method header for a method that takes three integers as parameters and prints the largest of the three to the screen. (Only write the header)
    2. How many times will the following loop execute the System.out.print statement?
      a = 1;
      while (a < 4) {
          System.out.print (a);
          a = a + a%3;
      }
      
    3. Given int a = 53, b = 4;

      What will be the output of the following statement?

          System.out.println ( (a % b) + (a / b) );
      
    4. What does void mean when seen as the return type in a method? (one sentence answer only)
    5. The following method header is part of the Math class. It returns the absolute value of its parameter. Write the line of code you would need to assign the absolute value of the variable number to a variable named absoluteValueOfNumber.
      static double abs(double a)
      
  4. (15 points) Complete the following method that places odd numbers found in the string s in consecutive elements of the array x.
    /* places odd numbers in consecutive elements of array */
    public static void assign(String s)
    {
         int len = __________;
    
         int[] x = new int[_________________];
    
         int index = 0;
         int digit;
    
         for(int j = 0; j < len; j++) {
            digit = s.charAt(j) - '0';//convert to int digit
    
            if( ______________________)//test for odd #
            {
                x[____________] = digit;
    
                ____________;
            }
         }
         System.out.println("The odd numbers are");
         for(int j = 0; j < index; j++)
         {
             System.out.println(x[j]);
         }
    }
    
  5. (20 points) Write a program that reads an integer n from the keyboard and then computes and displays the sum of the numbers from n to 2n if n is nonnegative, or the sum of the numbers from 2N to n if n is negative. The starting and ending points are included in the sum.

    Sample run:

       Enter number : 5
       Sum : 45
    

    Sample run:

       Enter number : -4
       Sum : -30
    

    Your program should not crash if the input is not an integer. Instead, if should re-prompt the user until an integer input is received.

    Hint: You should have only 1 loop to compute the sum; use the sign of n to determine the start point and the end point.

  6. (25 points) Write a method called CellFiller that takes an array of integers (call it cells) as a parameter and then:
    1. Reads in a positive integer k.
    2. If the element of cells with index k is set to −1, you will store k in that location.
    3. If the element of cells with index k is not set to −1, you will store k in the next available location of cells (i.e., at the next position that contains an element with value −1).

    So, if cells contains the following values: cells[3] = 3, cells[4] = 4, cells[5] = -1, cells[6] = 6 and cells[7] = -1. If the value read is 7 it will be stored in cells[7]; if the value read is 3, it will be stored in cells[5].

    You can assume that cells is infinitely large (i.e., you need not check the length of cells for out-of-bounds indices).


This document was translated from LATEX by HEVEA.