The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Questions and Exercises: Arrays

Questions

  1. What is the index of Brighton in the following array?
    String[] skiResorts = {
        "Whistler Blackcomb", "Squaw Valley", "Brighton",
        "Snowmass", "Sun Valley", "Taos"
    };
    
  2. Write an expression that refers to the string Brighton within the array.
  3. What is the value of the expression skiResorts.length?
  4. What is the index of the last item in the array?
  5. What is the value of the expression skiResorts[4]?

Exercises

  1. The following program, WhatHappens(in a .java source file), contains a bug. Find it and fix it.
    //
    // This program compiles but won't run successfully.
    //
    public class WhatHappens {
        public static void main(String[] args) {
            StringBuffer[] stringBuffers = new StringBuffer[10];
    
            for (int i = 0; i < stringBuffers.length; i ++) {
                stringBuffers[i].append("StringBuffer at index " + i);
            }
        }
    }
    
Check your answers.(in the Learning the Java Language trail)

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.