CSC 211 -- Practice Final Exam C

Work only 5 out of 6 problems.

  1. Answer all three parts a, b, and c.

    1. Explain the difference between an object and a class.

    2. If a and b are declared as
      String a, b;
      explain the difference between a == b and a.equals(b);.

    3. Write a main program that accepts an arbitrary number of command line arguments that represent integers. Print the sum of these integers.

  2. What is the output of the following Java program?

    public static void main(String[] args)
    {
        String[] a = {"grapefruit", "orange", "lemon",
             "date", "watermelon"};
        String temp, k;
        int i, j, m;
        int[] c = {2, 1, 2, 3, 1, 3, 4, 4};
        int[] d = {0, 0, 4, 0, 4, 1, 2, 1};
        temp = a[4];
        a[4] = a[2];
        a[2] = temp;
        for(i = 0; i <= 7; i++)
        {
           j = c[i];
           k = a[j];
           m = d[i];
           System.out.print(k.charAt(m));
        }
    }

     
     
     
     
     
     
     
     
     
     
     

  3. What is the output of the following Java program. Hint: draw an object diagram to keep track of the data values in the objects.

    public class A
    {
        int a;
        String b;

        public A(int intVal, String strVal)
        {
           a = intVal + 10;
           b = "xxx" + strVal;
        }

        public A(String strVal, int intVal)
        {
           a = intVal + 20;
           b = "yyy" + strVal;
        }

        public void update()
        {
           a += 5;
           b += "zzz";
        }

        public void update(int intVal)
        {
           a += intVal;
           b += String.valueOf(intVal);
        }

        public String toString()
        {
           return a + " " + b;
        }

    }

    public class Main
    {
        public static void main(String[] args)
        {
           int i;
           A[] x = new A[3];
           x[0] = new A(5, "dog");
           x[1] = new A("cat", 7);
           x[2] = x[0];
           x[0].update();
           x[1].update(2);
           x[2].update();
           for(i = 0; i <= 2; i++)
              System.out.println(x[i]);
        }
    }

  4. Do both parts a and b.

    1. Write code for a Student class. The private instance variables are name, id, totalHours, and totalGradePoints. Write code for a parameterized constructor that initalizes the instance variables, a toString method, and a getGpa method that returns the student's GPA, equal to totalGradePoints divided by totalHours.

    2. Write a main method that creates one Student object using data read from the text file c:\StudentInfo.txt. You may assume one item of data per line, for example:
      Rogers, William
      3453426576
      85
      326

  5. Write a Java application that creates an array of 100 double elements. Use a for loop to fill the array with 100 random values between 0 and 1. Then use another for loop to find the smallest array value and print the result.

  6. Write a Java application that creates a user interface that contains a field and a button. When the button is clicked, the application should replace the string in the textbox by the number of vowels in the textfield. (Vowels are the letters a, e, i, o, and u.)