Java Programming - Chapter 7: Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the difference between an array's size declarator and a subscript?

A size declarator tells us the size of an array, the subscript allows us to locate a specific element in an array (as an index)

What is null in Java?

A special value in Java that indicates the array elements do not yet have reference objects.

What is the difference between an array's size declarator and a subscript?

The size declarator specifies the number of elements in the array. A subscript identifies a specific element in the array.

TRUE OR FALSE: Once an array size is created it cannot be changed.

True

How are OBJECTS in an array accessed?

With subscripts, just like any other data type.

Consider the following code: int [ ] firstArray = { 5, 10, 15, 20 }; int [ ] secondArray = new int[5]; for (int index = 0; index < firstArray.length; index++) { secondArray[index] = firstArray[index]; } Does the code properly copy the contents of an array into another array?

YES => The loop in this code copies each element of firstArray to the corresponding element of secondArray.

If you need to access the values that are stored in an array, from the first element to the last element, is it better to use the enhanced for loop for the traditional for loop?

enhanced for loop

write a 14-element float array referenced by the variable miles

float [ ] mile = new float[14];

B. Write a loop that displays the contents of each element in the array you declared in A.

for (index = 0; index < planets.length; index++) { System.out.println(planet[index]); }

Look at the following statements: int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = new int[7]; Write code that copies the a array to the b array.

for (int i = 0; i < a.length; i++) b[i] = a[i];

Write a loop that displays the first character of the strings stored in each element of the array you declared in A

for( index = 0; index < planets.length; index++) { System.out.print("The first letter of planet " + (index+1) + " is " + planet[index].charAt(0); }

Write a 100-element int array referenced by the variable employeeNumbers

int [ ] employeeNumbers = new int[100];

A program has the following declaration: double[ ] values; Write code that asks the user for the size of the array and then creates an array of the specified size, referenced by the values variable.

int size; System.out.print("Enter the size of the array: "); size = keyboard.nextInt(); values = new double[size];

What does bounds checking mean?

it refers to when java does not allow a statement to use a subscript that is outside the range of valid subscript for an array

Look at the following method header: public static void myMethod(double[] array) Here is an array declaration: double[] numbers = new double[100]; Write a statement that passes the numbers array to the myMethod method.

myMethod(numbers);

Look at the following method header: public static void myMethod(double [ ] array) Here is an array declaration: double [ ] numbers = new double [100]; Write a statement that passes the numbers array to the MyMethod method.

myMethod(numbers);

If you do not provide an initialization list for a string array, you must use the _________ keyword to create the array

new

How would you correctly write a method header that returns an array of a double type? NOTE: The name of the method is called getArray

public static double [ ] getArray ( );

Write a method named zero, which accepts an int array as an argument and stores the value 0 in each element.

public static void Zero(int [ ] array) { for ( i = 0; i < array.length; i ++) { array[i] = 0; } }

When does bound checking occur in java

During runtime

An array and its reference variable are considered the same entity (T/F)

FALSE => They are two separate entities

You can compare two arrays with the == operator (T/F)

FALSE => You cannot compare the CONTENTS of arrays this way, however, the == compares the MEMORY ADDRESS of the arrays

When you create an uninitialized array of String objects, you do not need to assign a value to each element in the array (T/F)

FALSE => You must assign a value to each element in the array that you intend to use. In an uninitialized array, because they do not reference any objects, they are set to null.

When an entire array is passed into a method, the actual contents of the array is passed

FALSE => a reference to the array is passed, but not the actual array itself.

TRUE OR FALSE : Declaring an array reference variable creates an array

FALSE => declaring an array reference variable does not create an array

TRUE OR FALSE => int numbers [ ] is an incorrect array declaration notation

FALSE => this is an acceptable notation.

TRUE OR FALSE: It is not possible to declare a reference variable and create an instance of an array with one statement

FALSE => you can declare a reference variable and create an instance of an array with one statement. For example: int [ ] numbers = new int[6];

Describe the enhanced for loop

It is designed to iterate once for every element in an array. Each time this loop iterates, it copies an array element to a variable

What does it mean for a subscript to be out-of-bounds?

It means that the subscript is outside of the array range

Look at the following code: in [ ] array1 = {2, 4, 6, 8, 10}; int [ ] array2 = array1; Has array1 been copied into array 2?

NO! This is not how you properly copy the contents of an array into another array. This code makes a copy of the address stored in array1 and stores it in array2 (REFERENCE COPY). So, now array2 can now reference array1

To determine if two arrays are equal you must compare each of the elements of the two arrays. (T/F)

TRUE

When passing an array into a method as a n argument, you pass the value in the variable that references the array (T/F)

TRUE

Methods can return arrays (T/F)

TRUE => A method can return a reference to an array

TRUE OR FALSE: An array's size declarator must be a non-negative integer

TRUE => it is also common practice to use a final variable as a size declarator. For example: final int NUM_ELEMENTS = 6; int [ ] numbers = new int[NUM_ELEMENTS];

TRUE OR FALSE: the subscript of the last element in an array is one less than the total numbers array

TRUE => subscript numbering always starts at 0

A program uses a variable named array that references an array of integers. You do not know the number of elements in the array. Write a for loop that stores −1 in each element of the array.

(int i = 0; i < array.length; i ++) array[i] = -1;

The first element in an array is assigned the subscript __

0

What would the valid subscript values be in a four-element array of doubles?

0, 1, 2, 3

What is the output of the following code? int[] values = new int[5]; for (int count = 0; count < 5; count++) values[count] = count + 1; for (int count = 0; count < 5; count++) System.out.println(values[count]);

1 2 3 4 5

What is the output of the following code? int[] values = new int[5]; for (int count = 0; count < 5; count++) values[count] = count + 1; for (int count = 0; count < 5; count++) System.out.println(values[count]);

1 2 3 4 5

What's wrong with the following array declarations? int[] readings = new int[−1]; double[] measurements = new double[4.5];

1. The size declarator cannot be a negative number, the size declarator must be a positive expression 2. The size declarator cannot be a float number (such as 1.2, or 4.5, 5.5, 9.3, etc)

How do you create an array in Java?

1. You declare a reference variable and use the new keyword to create an instance of the array in memory. For example: int[ ] number; 2. using the new keyword to create an array and assign its address to the number's variable. Example: numbers = int [6]

What are the two main steps in getting the average values in a numeric array?

1. getting the sum of all the values in the array 2. divide the sum by the number of elements in the array

Write code that declares a Rectangle array with five elements. Instantiate each element with a Rectangle object. Use the Rectangle constructor to initialize each object with values for the length and width fields.

Rectangle[] array = new Rectangle[5]; for (int i = 0; i < array.length; i++) { array[i] = new Rectangle(i, i + 1); }

A. Write a statement that declares a String array initialized with the following strings: "Mercury", "Venus", "Earth", and "Mars".

String[ ] planets = { "Mercury", "Venus", "Earth", "Mars" };

(T/F) You may create objects that are instances of classes that you have created.

TRUE

An ArrayList object automatically expands in size to accommodate the items stored in it. (T/F)

TRUE

Declaring an array reference variable does not create an array (T/F)

TRUE

What happens in Java when a program tries to use a subscript that is out-of-bounds?

The java program crashes, and then it displays a runtime error message

What is wrong with the following array declarations? int [ ] readings = new int[-1]; double [ ] measurements = new double [4.5];

The size declarator of an array must NOT be negative and it must not be a floating number, it must be a whole integer.

Array

an object that can store a group of variables, all of the same data type

Write statements that create the following arrays: A 100-element int array referenced by the variable employeeNumbers. A 25-element double array referenced by the variable payRates. A 14-element float array referenced by the variable miles. A 1000-element char array referenced by the variable letters.

a. int[ ] employeeNumbers = new int[100]; b. double[ ] payRates = new double[25]; c. float [ ] miles = new float[14]; d. char[ ] letters = new char[1000];

write a 1000-element char array referenced by the variable letters

char [ ] letters = new char[1000];

write a 25-element double array referenced by the variable payRates

double [ ] payRates = new double[25];

Write a statement that creates and initializes a double array with the following values: 1.7, 6.4, 8.9, 3.1, and 9.2. How many elements are in the array?

double [ ] value = {1.7, 8.9, 3.1, 6.4, 9.2} There are 5 elements in the array

Write a method named zero, which accepts an int array as an argument and stores the value 0 in each element.

public static void zero(int[] array) { for (int i = 0; i < array.length; i++) array[i] = 0; }

Look at the following statements: int[] numbers1 = { 1, 3, 6, 9 }; int[] numbers2 = { 2, 4, 6, 8 }; int result; Write a statement that multiplies element 0 of the numbers1 array by element 3 of the numbers2 array and assigns the result to the result variable.

result = numbers1[0] * numbers2[3]; This would be multiplying 1 * 8

The number inside the brackets of an array is called _________

size declarator => indicates the number of elements, or values that an array can hold

In an array, the elements within it can be accessed by its assigned number called ___________

subscript

What is a subscript

used as an index to pinpoint a specific element within an array


Ensembles d'études connexes

Chapter 12 Vectors and the Geometry of Space

View Set

Managing People & work - QUIZ 10

View Set

ap euro famous documents and treaties

View Set

Chapter 39: Assessment and Management of Patients With Rheumatic Disorders

View Set

PREPPU: Assessment of Hematologic Function and Treatment Modalities

View Set

Passpoint immobility and pain & powerpoint questions

View Set