V22.0002
Intro. to Computers & Programming
Summer 2009
Practice Final #1
  1. (15 points) The following code contains at least 5 errors. Identify them. Comments, formatting, or the lack thereof are not any of the errors.
    import javax.swing.JOptionPane;
    
    public class ClassifyGrades {
    
        public static void main( String[] args )
        {
            int outstanding = 0, satisfactory = 0, unsatisfactory = 0;   
            int currentGrade,
            
            String currentGradeAsString;  
            
            currentGradeAsString = JOptionPane.showInputDialog(
                "Enter First Grade or -1 to Quit:" );
            
            currentGrade = Integer.parseInt( currentGradeAsString );  
            
            while ( currentGrade != -1 ) {  
                
                if ((currentGrade >= 90) && (currentGrade <= 100))
                    outstanding++;
                else if currentGrade >= 60) && (currentGrade <= 89))
                    satisfactory++;
                else  
                    System.out.println ("Not a valid grade");
                else if ((currentGrade >= 0) && (currentGrade <= 59))
                    unsatisfactory++;  
                        
                currentGradeAsString = JOptionPane.showInputDialog(
                    "Enter Next Grade or -1 to Quit:" );
            
                currentGrade = Integer.parseInt( currentGradeAsString ) 
            
            } 
            
            System.out.println ("Outstanding: " + outstanding);  
            System.out.println ("Satisfactory: " + satisfactory);  
            System.out.println ("Unsatisfactory: " + unsatisfactory);  
        } 
        System.exit( 0 );  
    }
    

    Answer:

  2. (10 points) What does the following code produce?
    public class NestedFor {
        public static void main( String[] args ) {
            int outer = 0, inner = 0;
        
            for(outer = 3; outer <= 7; outer ++) {
                for(inner = 0; inner <= outer; inner ++) {
                    if ((inner / outer) >= 1)
                        System.out.print(inner);
                }
                System.out.println ();
            }
        }
    }
    

    Answer:

    3
    4
    5
    6
    7
    
  3. (3 points each)
    1. Assuming the following declarations:
          int a = 40;
          int b = 5;
      

      What would be the output of the following code segment?

          if (a / b <= b)
              System.out.print (a);
          else
              System.out.print (b);
      

      Answer:

      5
      
    2. What is the value of the following expression?
          (66 / 6) + 15 * 3 % 2
      

      Answer:

          (66 / 6) + 15 * 3 % 2
          = 11 + 15 * 3 % 2
          = 11 + 45 % 2
          = 11 + 1
          = 12
      
    3. How many times does the following loop execute?
          int counter = 1;
          while (counter < 100)
          {
              System.out.println("counter is now: " +  counter);
              counter *= 2;
          }
      

      Answer: 7 times

      counter is now: 1
      counter is now: 2
      counter is now: 4
      counter is now: 8
      counter is now: 16
      counter is now: 32
      counter is now: 64
      
    4. Would the following expression evaluate to true or false?
          (54 == 64) || (77 > 55) || (99 < 1)
      

      Answer:

          (54 == 64) || (77 > 55) || (99 < 1)
          = false  || (77 > 55) || (99 < 1)
          = (77 > 55) || (99 < 1)
          = true || (99 < 1)
          = true
      
    5. Rewrite the following if statement as a switch statement:
          if (a == 1)
              System.out.println ("one");
          else if (a == 2)
              System.out.println ("two");
          else 
              System.out.println ("larger than 2");
      

      Answer:

      switch(a) {
      case 1: 
          System.out.println ("one");
          break;
      case 2:
          System.out.println ("two");
          break;
      default:    
          System.out.println ("larger than 2");
      }
      
  4. (15 points) Write a program that reads a positive integer n from the keyboard and outputs all of the prime numbers from 1 up to n. Write and use a method isPrime that takes an integer num as a parameter and returns true if num is prime, false if it is not.

    The main method will prompt for the number n and then it will test all of the numbers from 1 to n inclusive calling isPrime for each number. If the number is prime it will print it.

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

    Note: A prime number is a number divisible only by itself and 1.

    Sample run:

       Enter number : 9
    
       Prime numbers: 1 2 3 5 7
    

    Answer:

    import javax.swing.JOptionPane;
    
    public class PrimeNumbers
    {
        public static void main (String [] args)
        {
            int n = -1;
    
            do {
                String nAsString = JOptionPane.showInputDialog ("Enter number:");
                n = Integer.parseInt (nAsString);
            } while( n < 0 );
    
            for (int i = 2; i <= n; i++)
                if (isPrime(i))
                    System.out.print (i + "  ");
            
            System.exit(0);
        }
        
        public static boolean isPrime (int num)
        { //assumes num is positive
            int limit = (int) Math.sqrt (num);    
            if ((num == 1)  || (num == 2))
                return true;
            for (int i = 2; i <= limit; i++)
                if ((num % i) == 0)
                    return false;
            return true;
        }
    }
    
  5. (10 points) Complete the following method that adds the odd numbers from 1 to num
    //adds odd numbers from 1 to num
    public static int add(int num)
    {
         int sum = __________________________;
    
         for(int j = 1; ___________________; j++) {
    
             if(__________________________) {
    
                 sum = _______________________;
                 System.out.println(j);
             }
         }
         return ________________________________;
    }
    

    Answer:

    //adds odd numbers from 1 to num
    public static int add(int num)
    {
         int sum = 0;
    
         for(int j = 1; j <= num; j++) {
    
             if( j % 2 == 1 ) {
                 sum = sum + j;
                 System.out.println(j);
             }
         }
         return sum;
    }
    
  6. (25 points) Write a method eliminate that takes as parameters two integer arrays, x and y, and copies all the elements of the array x except the largest value into the array y. You may assume that the length of y is x.length - 1.

    Answer:

    public static void eliminate(int[] x, int[] y) {
        int max, maxIndex;
    
        /* Find the index of the maximum element */
        max = x[0];
        maxIndex = 0;
        for( int i=1; i < x.length; i++ ) {
            if( x[i] > max ) {
                max = x[i];
                maxIndex = i;
            }
        }
    
        /* Copy all but the max element to y */
        int yIndex = 0;
        for( int xIndex=0; xIndex < x.length; xIndex++ ) {
            if( xIndex == maxIndex ) {
                continue;
            }
            y[yIndex++] = x[xIndex];
        }
    }
    

This document was translated from LATEX by HEVEA.