CS 1713 Introduction to Computer Science, Exam 2 Practice Problems

  1. Write a method that takes a single String parameter and returns the number of "white space" characters. The white space characters and the blank, the tab ('\t'), the return ('\r') and the newline ('\n').
  2. Write a method that has a single String parameter and returns a String with the characters in the reverse order.
  3. Write a method that have a single two-dimensional array of ints as a parameter and returns the average of the values stored in the array. If the array is empty, return 0.
  4. Write a method that takes 2 parameters, an array of Strings and a String. Return the position of the last occurrence of the String in the array.
  5. Write a binary search for an array of Comparable, where the array os already sorted in increasing alphabetical order.
  6. Write a selection sort for an array of doubles.
  7. It takes 30 milliseconds to sort an array of size 10,000 using a selection sort. How long would it take to sort a similar array of size 20,000,000?
  8. 1000 is approximately what power of 2?
  9. 1,000,000 is approximately what power of 2?
  10. It takes 100 microseconds to search a sorted array of size 1,000. with a binary search. How long would it take to search a similar array of size 1,000,000?
  11. Draw an accurate schematic diagram of the program variables showing the execution of the program:
    int a = 17;
    int b = 34;
    int c = 45;
    int[] d = {1,2,3};
    int[] e;
    int[] f;
    f = d;
    a = f[0] + f[1] + f[2];
    d[1] = b;
    e = new int[4];
    e[0] = c;
    d = e;
    
  12. Suppose that RationalNumber is similar to the class we wrote earlier. Draw an accurate schematic diagram of the program variables showing the execution of the program:
    int num = 10;
    int denom = 15;
    RationalNumber[] r;
    r = new RationalNumber[4];
    r[0] = new RationalNumber(num,denom);
    r[1] = new RationalNumber(denom,num);
    r[2] = r[0].add(r[1]);
    r[0] = r[2];
    r[3] = r[0].reciprocal();