Java Arrays Questions

Ace your homework & exams now with Quizwiz!

Declare and instantiate an array named scores of twenty-five elements of type int .

int scores[] = new int[25];

Given an array of ints named x and an int variable named total that has already been declared, write some code that places the sum of all the elements of the array x into total. Declare any variables that you need.

total =0; for (int i=0; i<x.length; i++) { total+=x[i]; }

Assume that the array arr has been declared. Write a statement that assigns the next to last element of the array to the variable x , which has already been declared.

x = arr[arr.length - 2 ];

Declare an array reference variable, week, and initialize it to an array containing the strings "mon", "tue", "wed", "thu", "fri", "sat", "sun" (in that order).

String week[] = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};

Assume that an array named a containing exactly 5 integers has been declared and initialized. Write a single statement that adds 10 to the value stored in the first element of the array.

a[0] +=10;

Given that an array named a with elements of type int has been declared, assign 3 to its first element

a[0]=3;

Given that an array named a whose elements are of type int has been declared, assign the value -1 to the last element in a .

a[a.length -1] =-1;

An array of int s named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6 . Assign 9 to the element just after a[k] .

a[k+1]=9;

An array of int s named a has been declared with 12 elements. The integer variable k holds a value between 2 and 8 . Assign 22 to the element just before a[k] .

a[k-1]=22;

Assume that an array of int s named a has been declared with 12 elements. The integer variable k holds a value between 0 and 6 . Assign 15 to the array element whose index is k .

a[k]=15;

Write a statement that declares an array named streetAddress that contains exactly eighty elements of type char.

char streetAddress[]= new char[80];

Declare an array named a of ten elements of type int and initialize the elements (starting with the first) to the values 10 , 20 , ..., 100 respectively.

int a[] = {10, 20, 30,40,50,60,70,80,90,100};


Related study sets

Chapter 4: Premiums, Proceeds, and Beneficiaries

View Set

Foundations of employee motivation

View Set

Completing the Application, Underwriting, and Delivering the Policy

View Set

APES 8.3- Endocrine Disruptors WYRNTK

View Set

Illinois Life Insurance Key Points

View Set