To ExamInfo

CSC 212 -- Sample Final Exam

  1. Explain the difference between an instance variable and a local variable.

    Ans: An instance variable can be used anywhere in the class. A local variable can only be used in the method where is is defined. An instance variable represents the state (current condition) of the object. An instance variable can be declared with one of the accessibility modifers private, protected, public. A local variable is not declared with a modifier.

  2. What is wrong with these statements?

    int[ ] a;
    for(i = 0; i <= a.length - 1; i++)
        a[i] = i;
    
    Ans: The array a is never instantiated. The variable i is not declared.

  3. Write a main program that accepts an input string in a command line argument. Print out the characters of this string, one character per line.

    Ans:

    public static void main(String[ ] args)
    {
        for(int i = 0; i <= args[0].length; i++)
            System.out.println(args[0].charAt(i)); 
    }
    
  4. What is the output of the following Java program? Show your work.

    public static void main(String[ ] args)
    {
        String[ ] a = { "watermelon", "date", 
            "lemon", "orange", "grapefruit" };
        String temp;
        int i;
        for(i = 1; i <= 4; i++)
        {
            temp = a[i];
            a[i] = a[i - 1];
            a[i - 1] = temp;
        }
    
        for(i = 0; i <= 4; i++)
            System.out.print(a[i].charAt(i));
    }
    
    Ans:
    
     i     a[0]       a[1]       a[2]       a[3]       a[4]     temp
    ---+----------+----------+----------+----------+----------+----------
     1  watermelon date       lemon      orange     grapefruit date
     2  date       watermelon lemon      orange     grapefruit lemon
     3  date       lemon      watermelon orange     grapefruit orange
     4  date       lemon      orange     watermelon grapefruit grapefruit
        date       lemon      orange     grapefruit watermelon 
    
    Output: deapr
    
  5. Draw the reference diagram and predict the output:

    public class A
    {
        int a;
        String b;
        public A( )
        {
            this(7, "goose");
        }
        public A(int intVal, String strVal)
        {
            a = intVal + 5;
            b = "s" + strVal;
        }
        public void update( )
        {
            a += 5;
            b += "u";
        }
        public void update(int intVal)
        {
            a += intVal;
            b += String.valueOf(intVal);
        }
        public String toString( )
        {
            return a + " " + b;
        }
    }
    
    public class B extends A
    {
        public void update( )
        {
            a += 7;
            b += "v";
        }
    }
        
    public class Main
    {
        public static void main(String[ ] args)
        {
            int i;
            A[ ] x = new A[3];
            x[0] = new A(5, "fox");
            x[1] = new B( );
            x[2] = x[0];
            x[0].update( );
            x[1].update( );
            x[2].update(2);
            for(i = 0; i <= 2; i++)
                System.out.println(x[i]);
        }
    }
    
    Ans:       0   1   2
    +---+    +---+---+---+
    | o----->| o | o | o |
    +---+    +-|-+-|-+-|-+
      x        |   +-----------+ 
      x        |       |       |
               ++  +---+       |  
                |  |           |   
                V  V           V    
             a     b        a     b  
           -----+-----   ------+------
             10  sfox       7   goose
             15  sfoxu     14   goosev
             17  sfoxu2
    Output:
    sfoxu
    goosev
    sfoxu
    
    
  6. Write code for a GroceryItem class. The private instance variables are name (String), barCode (int), and price (double). Write code for a parameterized constructor that initalizes the instance variables, a toString method, a getter method getPrice, and a setter method setPrice.

    Ans:

    public class GroceryItem
    {
        private String name;
        private int barCode;
        private double price;
    
        public GroceryItem(String theName, 
                           int theBarCode, double thePrice)
        {
            name = theName;
            barCode = theBarCode;
            price = thePrice;
        }
    
        public double getPrice( )
        {
            return price; 
        }
    
        public void setPrice(double thePrice)
        {
            price = thePrice;
        }
    
        public String toString( )
        {
            return String.format("%s %d %.2f", name, barCode, price);
        }
    }
    
  7. Write a main method that creates a GroceryItem object using data read from the text file c:\GroceryInfo.txt. You may assume one item of data per line, for example:

    Then test the toString, getPrice, and setPrice methods.

    public static void main(String[ ] args) throws FileNotFoundException
    {
        Scanner s = new Scanner(new FileReader("c:\\GroceryInfo.txt"));
    
        GroceryItem g = new GroceryItem(s.next( ), 
                            s.nextInt( ), s.nextDouble( ));
    
        System.out.println(g);
        g.setPrice(2.85);
        System.out.println(g.getPrice( ));
    }
    
  8. 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 largest array value and print the result.

    public static void main(String[ ] args)
    {
        int[ ] a = new int[100];
        for(int i = 0; i <= 99; i++)
            a[i] = Math.random( );
       
        int max = 0; 
        for(int i = 0; i <= 99; i++)
            if (a[i] > max)
                max = a[i];
        System.out.println("Max value is " + max);
    }