CS/IT 115 Practice Final

Thanks to Bob Wilson’s Practice Exams for CS110

 

1. Operations on Strings and Characters

Given the following local variable declarations :

String s = “abc”;

String a = “Did Hannah see bees? Hannah did.”;

What is the value of the following expressions ?

s.length()      // a. ____________

a.charAt(5)     // b. ____________

s.equals(a)     // c. ____________

a.indexOf(“H”)  // d. ____________

s.substring(1,3)// e. ____________

s + a.charAt(0) // f.______________

 

2. Operations with Classes and Objects

Explain each fragment of code in the left column with one or two numbers from the right column, so that all 10 numbers are used somewhere at least once.

a. ____ import java.util.ArrayList;          1. Uses Autoboxing (conversion int to Integer, etc.)

b. ____ ArrayList<Integer> a =

          new ArrayList<Integer>();       2. Uses Auto Unboxing (conversion Integer to int)

c. ____ a.add(6);                            3. Allows “ArrayList” to be used in the code.

d. ____ int small = a.get(0);                4. Creates a non-String object (uses a constructor)

e. ____a.add(4);                            5. Uses an instance method

f. ____ small *= 2;                          6. Creates an reference variable for an object

g. ____ Integer big = small;                 7. Uses a static method

h. ____ int max = 1000;                      8. Creates and initializes a local variable

i. _____Collections.sort(a);                 9. Changes the size of an ArrayList

j. ____ String x = “abc”;                    10. Constructs a String

                                                                                                                11. Creates a primitive type variable

                                                                                                                12. Changes a primitive type variable

 

3. ArrayLists.

import java.util.ArrayList;

public class ArrayTest

{

   public static void main(String [] args)

   {

      ArrayList<String> myList = new ArrayList<String>();

      boolean status; // for return value when needed

      String removed; // for return value when needed

      // write code to add "foo" and "bar" to the end of myList

      _____________________________________________________

      _____________________________________________________

      // write a line of code to add "never" at beginning of myList

      _____________________________________________________

      // write a line of code to remove the item at index 2 from myList

      _____________________________________________________

      // print the contents of myList

      System.out.println(myList);

      // what prints?   ________________________________

   }

}

4. Writing a simple class

Write a class Person with fields String “name” and int “age”. Include a constructor method that sets the name and the age based on arguments passed. Include getter methods for both fields, and a setter for age. You can leave equals unimplemented, but do override Object’s toString.

 

5. Client code for an object class.  Suppose class PhoneNumber has the following API:

public class PhoneNumber
               PhoneNumber(int areaCode, int exchange, int localNumber)
       int     getAreaCode()        // area code of number (3 digits)

       void    setAreaCode(int newAreaCode)    // replace old area code with new one
       int     getExchange()      //  exchange of number  (3 digits)
       int     localNumber()    // local number   (4 digits)
       boolean isValid()     // true if all three numbers fit in the #digits allowed
       String  toString()    // a String such as "617-354-5500"

 

Suppose this class is fully implemented in PhoneNumber.java.  Write a class TestPhone.java that creates two phone numbers for 617-203-4444 and 617-203-5555, calls isValid() on each of them and prints them out if they are invalid using the toString() method. Then write code to see if they have the same area code and exchange, and print out “same exchange” if so.  Finally, change the area code of the first number to 688 and print out the new version of the number.

6. Interfaces. Continuing with PhoneNumber.java. Turn the API methods for PhoneNumber (other than toString(), an Object method) into an interface Phone.java.  Show the full source code for Phone.java.  How do you make PhoneNumber.java relate to the Phone interface?

7. Consider the setup of Project p3, with an ArrayList<ITSystem> systems already loaded up.  Write a helper method for SysInventory.java named highestRoomNo that finds the highest room number of any system in systems and returns it as its return value. Note that this code can use the systems ArrayList directly, since it’s object method code.

8. In Project p4, we read all the words (Strings with no internal spaces) from words.txt into an ArrayList<String> words.  Write a helper method named hasDuplicates that accepts words as a parameter and checks whether or not there are duplicates among these given words, using the same duplicate detection method used for the “codes” in the project. The method hasDuplicates should return true if any duplicates exist, else false.

9. Inheritance. Consider the classes set up for problem 11, pg. 619.   Here is some client code for those classes:

ArrayList<SeaCreature> animals = new ArrayList<SeaCreature>();

animals.add(new Whale());

animals.add(new Squid());

animals.add(new Mammal()):

animals.add(new SeaCreature());

 

a.  Explain why we can add a Whale to an ArrayList<SeaCreature>, given that add expects an argument of type SeaCreature.

 

b.      What is printed out by

for (int i=0; i< animals.size(); i++) {

  animals.get(i).method1();

}

 

c.       What is printed out by

System.out.println(animals);