COSC 2011 3.0 Fundamentals of Data Structures

Assignment 1

  1. Let the variable i be initialized to 6. What is the value of the following expression?
    (++i == 7) ? i++ : --i
    What is the value of the variable i after the evaluation of the above expression?

  2. What is the value of the following expression?
    8 * 4 / 2 + 1
  3. How often is the variable i incremented in the following loop?
    int i = 0;
    while (i < 10) {
        if (i == 5) {
            i += 10;
            break;
        }
        i++;
    }
    
  4. Consider the sequence defined by

    g(0) = first command line argument
    g(1) = second command line argument
    g(i) = the absolute value of g(i-1) - g(i-2)

    Complete the class definition
    class AbsoluteDifferenceSequence {
     
        public static void main(String[] args) {
    
            /* the method
               public static int Integer.parseInt(String s)
               returns its argument s interpreted as an integer */
    
            /* the method 
               public static int Math.abs(int i)
               returns the absolute value of its argument i */
    
        }
    }
    
    printing the first n elements of this sequence, where n is the third command line argument. For example,
    java AbsoluteDifferenceSequence 2 200 10
    should print out the following ten lines.
    2
    200
    198
    2
    196
    194
    2
    192
    190
    2