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 );  
    }
    
  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 ();
            }
        }
    }
    
  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);
      
    2. What is the value of the following expression?
          (66 / 6) + 15 * 3 % 2
      
    3. How many times does the following loop execute?
          int counter = 1;
          while (counter < 100)
          {
              System.out.println("counter is now: " +  counter);
              counter *= 2;
          }
      
    4. Would the following expression evaluate to true or false?
          (54 == 64) || (77 > 55) || (99 < 1)
      
    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");
      
  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
    
  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 ________________________________;
    }
    
  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.

This document was translated from LATEX by HEVEA.