More Practice Exam Questions - james-bern/CS136 GitHub Wiki

These are longer standalone questions, representative of the harder standalone on my exams.

  1. What does this code print?

    int[] array = { 2, 3, 4 };
    
    ArrayList<Integer> list = new ArrayList<>();
    for (int i = 0; i < array.length; ++i) {
        for (int repetition = 0; repetition < 3; ++repetition) {
              list.add(array[i]);
        }
    }
    PRINT(list);
    
    for (int i = 0; i < list.size() / 2; ++i) {
         int j = (list.size() - 1) - i;
         int tmp = list.get(i);
         list.set(i, list.get(j));
         list.set(j, tmp);
    }
    
    PRINT(list);
    
    for (int i = 0; i < list.size(); ++i) {
         list.set(i, list.get(list.get(i))); // <- Read this carefully!
    }
    
    PRINT(list);
    👀
    [2, 2, 2, 3, 3, 3, 4, 4, 4]
    [4, 4, 4, 3, 3, 3, 2, 2, 2]
    [3, 3, 3, 3, 3, 3, 3, 3, 3] 
    
  2. Implement this function:

    // Returns the difference between the largest and smallest numbers in the array.
    // NOTE: Returns 0.0 if array has length 1
    // NOTE: Failed ASSERT if array is null or has length 0
    //
    // EXAMPLE: { 1.0, 5.0, -10.0 } -> 15.0
    // EXAMPLE: { 1.0, 1.0, 1.0 }   -> 0.0
    // EXAMPLE: { }                 -> Failed ASSERT
    double findRange(double[] array) {
        ...
    }
  3. Implement this function:

    // Returns the result of doing an update of the type used in Pascal's triangle:
    // NOTE: Don't modify the input array; return a new array.
    //
    // EXAMPLE: { 1 }          -> { 1, 1 }
    // EXAMPLE: { 1, 1 }       -> { 1, 2, 1 }
    // EXAMPLE: { 1, 2, 1 }    -> { 1, 3, 3, 1 }
    // EXAMPLE: { 1, 3, 3, 1 } -> { 1, 4, 6, 4, 1 }
    int[] pascalUpdate(int[] array) {
        ...
    }
  4. Implement this function.

    // Returns the result of adding the corresponding elements of the two input arrays.
    // NOTE: Don't modify the input arrays; return a new array.
    //
    // EXAMPLE: ({ 1, 2, 3 }, { 1, 2, 3 }) -> { 2, 4, 6 }
    // EXAMPLE: ({ 1, 1 }, { 4, 5 })       -> { 5, 6 }
    // EXAMPLE: ({ 1, 2, 3 }, { 1 })       -> Failed ASSERT
    int[] arraySum(int[] A, int[] B) {
        ...
    }
  5. Implement this function.

    // Returns the indices (NOT values) of all even numbers in the array.
    // NOTE: Assume all numbers in the array are non-negative.
    //
    // { 6, 7, 5, 8 } -> { 0, 3 } because the 0th element (6)
    //     and 3rd element (8) are even
    //
    // { 7, 1, 5 } -> {} because no elements are even
    ArrayList<Integer> getIndicesOfEvenNumbers(int[] array) {
        ...
    }
⚠️ **GitHub.com Fallback** ⚠️