CS10, 09S, UCSB

H09: (Based on Horstman, Chapters 7 and 8) Total points: ? (printable PDF)

Accepted: on paper, in lecture (1-1:50pm) on Monday, May 4th only.
No email submission allowed.

Name: (2 pts)______________________________   UCSBNetID (2 pts)  _____________________

Section (4 pts) Circle one:         Thu 4pm                 Fri 10am                       Fri 11am                  Fri noon


  1. In Java, each of the primitive types has an associated "wrapper type".
    1. (5 pts) What is the wrapper type for int?


    2. (5 pts) What is the primitive type associated with the wrapper type Character?


  2. (5 pts) Here is a for loop that goes through all the elements of an array called balances, and counts how many of them are negative:


    int [] balances = {2000,-10,3000,-15,4000};

    int overDrawnCount = 0;

    for (int i=0; i<balances.length; i++)
    {
    if (balances[i] < 0)
    overdrawnCount++;
    }
    Rewrite this section of code using the enhanced for loop from section 7.4 of the text.

  3. (12 pts) Match the method computing number of elements with the appropriate Data Type
    (draw a line between them).

    a.length
    Array
    a.length()
    Array list
    a.size
    String
    a.size()
    (none of the above)

  4. (5 pts) What do we mean when we talk about the scope of a variable?




    Please turn over for more problems

  5. The Movie class below compiles perfectly, but there is a problem with the constructor.

    1. (5 pts) Describe the problem with this program—what is wrong, and why will this not work?


    2. (5 pts) Show how to fix it.



    public class Movie
    {
        private int year;
        private String title;
    
    /** * Constructor for objects of class Movie * @param theYear the year the movie was released * @param theTitle the title of the movie */
    public Movie(int theYear, String theTitle) { int year = theYear; String title = theTitle; }

    /* ... Other methods would go here, but don't concern yourself with that.
    Just say what's wrong with the constructor. */
    }



End of H09