CS 1713 More Sample Final Exam Problems

  1. (10 points)
    1. Write a method that takes two parameters, a char and an int. It prints the first parameter the number of times given by the second parameter.
    2. Use the method from part a) to write a code segment to output a triangular pattern of asterisks like the one below. The first row has only one asterisk and the last row has no leading blanks. All rows other than the first and last contain exactly two asterisks. The variable rows contains the number of rows. This is the same as the number of lines of output. You may assume that rows is at least 2.
      For rows = 6 the pattern looks like:
                *
               * *
              *   *
             *     *
            *       *
           ***********
           

  2. (5 points) Write a code segment that sets the integer variable count to the number of times the letter 'A' occurs in the String s.

  3. (10 points)
    1. Write a code segment that declares and allocates a 2-dimensional array of doubles with 100 rows and 20 columns.
    2. Write a method that takes an arbitrary rectangular 2-dimensional array of doubles and a column number as parameters and returns the average of the elements in the given column. If the column is empty, return 0.

  4. (18 points) Write a class called TextBook to hold information about a book for a course. A textbook has a title, an author, a course number and a price. Have the class implement Comparable with the comparison by title. Show the entire code for the class including a constructor, accessor and modifier methods for all attributes, and all methods needed to implement Comparable.

  5. (7 points) Given the following declarations, find the value indicated.
       String s = new String("ABCDEFGABCDEFG");
       String t = new String("DEF");
       String u = new String("DEF");
       String v = new String("ABD");
       
    1. s.length();
    2. s.substring(5)
    3. s.substring(3,8)
    4. s.indexOf(t)
    5. s.indexOf(v)
    6. t == v;
    7. t.equals(v);

  6. (10 points)
    1. Write a method, findLargest, that takes a single array of integers as a parameter and returns the index of the largest value in the array. If the array contains no elements, return -1. If there are several entries that have the largest value, return the index of the first such entry.
    2. Write a method, findLargestFromEnd, which is similar to the one above, but if there are several entries that have the largest value, return the index of the last such entry.
    3. Write a method, findLargest5, that takes a single array of integers as a parameter and returns the value of the largest entry in the array that is divisible by 5. If there are no such values, return -1. Be sure that your method works even if the array only contains negative values.