COMP 170         Sample FINAL EXAMINATION     -      Fall, 1998-99

 I    (A)    For each of the following Java statements, determine the value stored in x.

(a)      x = 7 + 3 * 6   /   2 -1;
(b)      x = 2 % 2  + 2  * 2  -  2    /   2;
(c)      x =  (3 * 9 * ( 3 + ( 9 * 3 / (3))));

(B)  Which of the following assignment statements correctly assign the value of  ax3 +7  to the variable y?

(a)      y = a * x * x * x + 7;
(b)      y =  a * x * x * x * (x + 7);
(c)      y = (a * x) * x * (x+7);
(d)      y = (a * x) * x * x  + 7;
(e)      y = a * (x * x * x ) + 7;
(f)      y = a * x * (x * x + 7);
 

(C)  What is the precise output when the following program is run?

public class Printing {
     public static void main (String args[]) {
           for (int i = 0;  i < 10;  i++)  {
                for ( int j = 0;  j < 5;  j++)
                      System.out.print( ‘E’);
                System.out.println();
            }
      }
}

II     For the following Java program, determine the number of asterisks printed when the code is executed for each set of input values given in the table below:
 

public class Test {
     public static void main(String[] args){
           int[] b=new int[100];
           for(int i=0; i<b.length; i++){
               b[i]= i+5;
           }
           int beginIndex, endIndex, locate;
           System.out.print("Please enter the values");
           System.out.println(" of beginIndex, endIndex, locate.");
           beginIndex = SavitchIn.readLineInt();
           endIndex = SavitchIn.readLineInt();
           locate = SavitchIn.readLineInt();
           Search.find(b, beginIndex, endIndex, locate);
     }
}

public class Search{
    public static void find(int[]a, int first, int last, int key){

        final char STAR ='*';
        boolean success = false;
        while ((!success) && (first<=last)){
            int middle=(first+last)/2;
            System.out.print(STAR);
            if (key==a[middle]){
                success=true;
            }
            else if (key<a[middle]){
                last=middle-1;
                }
                else first = middle+1;
        }
    }
}
 

beginIndex     endIndex      locate     number of asterisks printed
0                      32                  21
4                      32                  30
13                     21                 43
1                      64                  29
 

 III     (A)  Given the following recursive method, what is the precise output if a client calls Calculate.whatDo(5)?

public class Calculate{
    public static void whatDo (int i){
          if (i <= 1) return;
          if (i > 1) {
                System.out.println(i);
                if (i%2 == 0)
                    whatDo(i/2);
                else
                    whatDo(i+1);
                System.out.println(i);
          }
    }
}
 

(B)  Given the following recursive method, what is returned if a client calls each of the following?:

Calculate.puzzle(3)?
Calculate.puzzle(6)?
Calculate.puzzle(10)?
Calculate.puzzle(30)?
Calculate.puzzle(300)?

For a general positive integer, n, what is the approximate result of calling Calculate.puzzle(n)?
 

public class Calculate{
    public static int puzzle (int n){
             if (n <= 1)
                    return 0;
             else
                    return 1 + puzzle(n/3);
     }
}

IV   (A)   Which of the following are valid declarations?  Of those which are valid Java statements, which actually instantiate an array?  Correct those declarations which are incorrect.

(a)  int primes = {2, 3, 4, 5, 7, 11};

(b)  float elapsedTimes[] = {11.47, 12.04, 11.72, 13.88};

(c)  int []  scores = int [30];

(d)  int{] primes = new {2, 3, 5, 7, 11};

(e)  int[] scores = new int [30];

(f)  char grades[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘f’);

(g)  char [] grades = new char [];
 

(B)     Describe what occurs when the following Java code is executed:

int[] numbers =  {3, 2, 3, 6, 9, 10, 12, 32, 3, 12, 6};
for (int count = 1;  count <= numbers.length;  count++) {
         System.out.println (numbers[count]);
}

 V    Consider the following Java program:

//testing the MinutesAndSeconds class
//author:  Jean Valjean
public class Testing{
       public static void main(String[] args){
               MinutesAndSeconds albertineTime = new MinutesAndSeconds (40, 23);
               MinutesAndSeconds borisTime = new MinutesAndSeconds (33, 50);
               MinutesAndSeconds sum = albertineTime.adder(borisTime);
               System.out.println("The sum of");
               albertineTime.printer();
               System.out.println("and");
               borisTime.printer();
               System.out.println("is");
               sum.printer();
       }
}

 public class MinutesAndSeconds {
         private int minutes, seconds;
            //constructor
            public MinutesAndSeconds(int d,int c){minutes = d; seconds = c;}

            public MinutesAndSeconds adder(MinutesAndSeconds x) {
                    minutes = x.minutes + minutes;
                    seconds = x. seconds + seconds;
                    return new MinutesAndSeconds(minutes, seconds);
            }

            public void printer(){
                    System.out.println(minutes+" minutes and "+seconds+" seconds ");
            }
 }
 

The goal of Jean Valjean is to create a Java program which will add two times.  For example, if Albertine uses her computer for 40 minutes and 23 seconds and Boris uses the same computer for 33 minutes and 50 seconds, then Valjean’s program will calculate the sum of their times and print 74 minutes and 13 seconds.  (It is not intended to convert minutes to hours.)

(a)  Determine the actual output of Jean Valjean’s program.  Be precise.
 
 
 
 
 

(b)  Has Jean Valjean achieved his goal?  If not, make all necessary corrections to the above program.  (Do not make unnecessary stylistic improvements.)
 

 VI   For the following Java program, determine the associated output when the code is executed.  Be precise, carefully indicating the position of every character on each line of output.

//testing the PositiveNumber class
 public class Surprise {
        public static void main(String[] args){
                PositiveNumber m = new PositiveNumber (100);
                System.out.println(m.find(13));
                System.out.println(m.find(10));
                System.out.print("m=");
                m.print();
                int ct=1;
                while (ct < 9){
                    m=m.change();
                    m.print();
                    ct++;
                }
                System.out.println("ct=" + ct);
        }
}

public class PositiveNumber {
    private int number;
    public PositiveNumber(int n) {number = n;}

    public PositiveNumber change(){
        if ((number%2)==0)
                return new PositiveNumber (number/4);
        else
                return new PositiveNumber(3*number+1);
    }

    public int find(int factor) {
        int  q = number, ct = 0;
        while (q % factor == 0){
                ct++;
                q = q/factor;
        }
        return ct;
    }

    public void print() {
            System.out.println(number);
    }
}

 VII     Let k be a positive integer and n be a non-negative integer.  Consider the following clever observation:

              1            if n is 0,
kn  =     (kn/2)2       if n is even,
              k (kn/2)2   if n is odd,

where n/2 means integer division.

Using this observation, complete the following recursive method which computes kn.

public class Calculate{
    public static int power (int k, int n){
           if (n       )  // base case
                 return (          );

          if (        )   // n odd
                return (             );
          else   // n even
                return (             );
    }
}
 
 

 VIII     Describe precisely the output to the following Java program:

public class ArrayTest {
      public static void main (String[] args) {
              ArrayParameters tester = new ArrayParameters ();
              int[] list = { 11, 22, 33, 44, 55};
              int[] list2 = {99, 99, 99, 99, 99};
              tester.print ("Original array:", list);
              tester.passElement(list[0]);
              tester.print("After passing one element:", list);
              tester.changeElements (list);
              tester.print ("After changing individual elements:", list);
              tester.changeReference (list, list2);
              tester.print ("After attempting to change a reference:", list);
              tester.copyArray (list, list2);
              tester.print ("After copying each array element:", list);
              list = tester.returnReference (list2);
              tester.print ("After returning a reference:", list);
     } // method main
}  // class ArrayTest

 class ArrayParameters {
     int[] a;  // instance variable
     public ArrayParamters(){ // constructor
              a = new int[100];
     }
     public void passElement (int num) {
          System.out.println ("The value of num is " + num);
          num = 4321;
          System.out.println ("The value of num is now  " + num);
     } // method passElement
    public void changeElements (int[] myList) {
          myList[2] = 77;
          myList[4] = 88;
    } // method changeElements

     public void changeReference (int[] myList, int[] myList2) {
          myList = myList2;
     }   // method changeReference

     public void copyArray (int[] myList, int[] myList2) {
              for (int index = 0;  index < myList.length; index++)
                       myList[index] = myList2[index];
      }  // method copyArray

     public int[] returnReference (int[] myList2) {
              myList2[1] = 9876;
              return myList2;
     }  // method returnReference

     public void print (String message, int[] myList) {
              System.out.println (message);
              for (int index = 0; index < myList.length; index++)
                    System.out.print(myList[index] + "  ");
                    System.out.println ();
      }  // method print
}   // class ArrayParameters
 

Course Home Page          Department Home Page        Loyola Home Page