CIS 1068. Some Practice Problems

The exam is cumulative and may include material covered in the first set of problems and in the first midterm.

code trace

Be able to trace through any method in this slide set.

  1. What's printed by the following program?
    public class WhatsPrinted1 {
        public static void whatsPrinted(int A[]) {
    	for (int i=1; i<A.length; i++) {
    	    A[i]=A[i-1]*2+1;
    	}
        }
    
        public static void main(String args[]) {
    	int A[] = {12,3,8,9,7,11};
    	whatsPrinted(A);
    	System.out.println(A[A.length-1]);
        }
    }
    
    
  2. What's printed by the following program?
    public class WhatsPrinted2 {
        public static void whatHappens(int A[]) {
    	int []B = new int[A.length];
    
    	for (int i=0; i<A.length; i++) {
    	    B[i]=A[i]*2;
    	}
    	A=B;
        }
        
        public static void main(String args[]) {
    	int A[] = {10,20,30};
    	whatHappens(A);
    	System.out.println(A[0]);
        }
    }
    
    
  3. What's printed by the following program?
    public class WhatsPrinted3 {
        public static int[] whatHappens(int A[]) {
    	int []B = new int[A.length];
    
    	for (int i=0; i<A.length; i++) {
    	    B[i]=A[i]*2;
    	}
    	return B;
        }
        
        public static void main(String args[]) {
    	int A[] = {10,20,30};
    	whatHappens(A);
    	System.out.println(A[0]);
        }
    }
    
    
  4. What's printed by the following program?
    public class WhatsPrinted4 {
        public static int[] whatHappens(int A[]) {
    	int C[]=new int[A.length];
    	for (int i=0; i<A.length; i++) {
    	    C[i]=A[i];
    	}
    	return C;
        }
    
        public static void main(String args[]) {
    	int A[]={10,20,30};
    	int B[]=whatHappens(A);
    
    	if (A==B) {
    	    System.out.println("equal");
    	} else {
    	    System.out.println("not equal");
    	}
        }
    }
    
        
    
    

files

  1. Write a method which is passed the name of a text file. The method returns the number of characters found in the file. You are not required to handle FileNotFoundException.
  2. Write a method which is passed the name of a text file. The method returns the number of words found in the file. You are not required to handle FileNotFoundException.
  3. Write a method which is passed the name of a text file. The method returns the length of the longest word found in the file. You are not required to handle FileNotFoundException.
  4. Write a method which is passed the name of a text file which contains grades in a course that's in the following format:
    Stan 99 87 100
    Dipper 100 100 97 100
    Mabel 100 100
    Seuss 72 85 65

    the method returns the average of the student with the highest semester average.

  5. Write a method which is passed the name of a text file. The method returns the number of blank lines contained in the file. You are not required to handle FileNotFoundException.

arrays

  1. Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore.
  2. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards.
  3. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the same contents.
  4. Write a method called copy, which is passed A[], which is an array of int. The method returns a new array consisting of exactly the same items in A[].
  5. Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[].
  6. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
  7. Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of all of the items in A[] which are greater or equal to n.
  8. Write a method which is passed an array of int and returns true if the array is sorted in ascending order.
  9. Write a method called generateTriangleNumbers(). This method will take in an integer x and will return an array of integers containing the first x triangle numbers. The nth triangle number is the sum of 1 + 2 + 3 + 4...(n − 1) + n.
    1. generateTriangleNumbers(3) returns the array {1,3,6}
    2. generateTriangleNumber(1) returns the array {1}
    3. generateTriangleNumbers(7) returns the array {1, 3, 6, 10, 15, 21, 28}
  10. Write a method which is passed a String and returns the String in reverse.
  11. Write a method which is passed an array of String and modifies it so that all the Strings in the array are reversed. For example, if we pass the array: {"apple", "banana", "racecar", "abc"} the method transforms it to: {"elppa", "ananab", "racecar", "cba"}
  12. Write a method which is passed a two-dimensional array of int and prints it row by row. Do not assume that each row has the same number of columns.
  13. Write a method which is passed a two-dimensional array of int and prints it column by column. You may assume that each row has the same number of columns
  14. Implement the method:
    /* sets every value in A[][] to initVal */
    public static void initialize(int A[][], int initVal) 
        
  15. Implement the method:
    /* returns the largest element in A */
    public static int largestItem(int A[][]) 
        
  16. Implement the method:
    /* returns the sum of the row in A that has the largest
     * sum. */
    public static int largestRow(int A[][]) 
        
  17. Implement the method:
    /* Returns column i of A as an array. For example, if */
    /* A[][] is: */
    /* |-----+-----+-----+-----+-----| */
    /* |  10 |  20 |  30 |  40 |  50 | */
    /* |-----+-----+-----+-----+-----| */
    /* |  60 |  70 |  80 |  90 | 100 | */
    /* |-----+-----+-----+-----+-----| */
    /* | 110 | 120 | 130 | 140 | 150 | */
    /* |-----+-----+-----+-----+-----| */
    /* and i is 1, the method returns the array */
    /*                                 */
    /*   |----+----+-----|             */
    /*   | 20 | 70 | 120 |             */ 
    /*   |----+----+-----|             */
    /*                                 */
    /* You may assume that every row has */
    /* the same number of columns. */
    public static int[] getCol(int A[][], int i)
        
  18. Implement the method:
    /* returns the sum of the column in A that has the largest
     * sum. You may assume that each row has the same number
     * of columns. */
    public static int largestCol(int A[][])
        

strings

  1. Write a method which is passed a String which returns true if the String contains a double letter or false otherwise.
  2. Write a method which is passed a String. It returns the index of the last Roman Numeral (recall that the Roman Numerals are {'I', 'V', 'X', 'L', 'C', 'D', 'M'}) found in the String or -1 if no Roman Numeral is found in the String.
  3. Write a method which is passed a String s and returns a String that's exactly the same as s, but with no vowels.
  4. Write a method which is passed a String s and returns a new String that's the same as s, but with the first letter of each word capitalized. There is a static method in the Character class toUpperCase that is passed a char c. If c is a lower-case letter, it returns its upper case equivalent. If it's not a lower-case letter, it returns c.
  5. Write a method called acronym, which is passed a String s and returns a new String consisting of the first letter of each word in s. For example, if s refers to the String "United Parcel Service", the method returns the String "UPS".
  6. Write a method appendIfMissing which is passed a String s and a String e (for ending). If s doesn't already end with e, the method returns a new String which consists of the contents of s concatenated with e. It returns s otherwise. For example, if s refers to the String "lightningbug" and e refers to the String "bug", the method returns "lightningbug". If s refers to the String "Airplane II", and e refers to the String ": the Sequel", the method returns "Airplane II: the Sequel".

classes

  1. What's the difference between static and non-static methods?
  2. Overload the move() method in the Bug class so that it takes as a parameter the number of spaces to move.
  3. Create a Rectangle class. Rectangles have a location, which is the x,y coordinate of its lower left-hand side. They also have a length and width. Include a constructor, accessor and mutator methods, a getArea() method, and equals().
  4. Create a Flash Card class. Flash Cards have a Question and an Answer, each of which are Strings. Your class should include a constructor, toString() and equals() methods. Write a main() method that creates an array of three Flash Cards, and prints each of them.
  5. Create an address class. Addresses have a street number, a street, a city, state, and zip code. Your class should include a constructor, toString() and equals() methods.
  6. Create a LineSegment class. A line segment should contain two Points. Add a constructor and toString methods. add a getSlope() method.