You are on page 1of 2

Name(s)__________________________________________ Period ______ Date ___________________

U3L2 Extra Practice - Array Elements

Check for Understanding

Question: Consider the following code segment:

int[] values = {2, 3, 4, 1, 5, 2, 4};


int sum = values[3] + values [4];
sum += values[6];

What is stored in sum after this code segment is run?

A. {3, 4, 6}
B. {1, 5, 4}
C. 0
D. 10
E. 4

Response:

AP Exam Prep

Question: What values are stored in the array nums after this code segment is run?

int[] nums = {3, 4, 8, 1, 0, 0, 2}; 3 4 8 1 0 0 2

nums[2] = nums[4];

nums[3] -= nums[5];

nums[6] = 10 + nums[4];

nums[0]++;

nums[5] += nums[1];

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

nums[4]--;

nums[2] -= nums[3]; 4 2 -1 1 -1 4 10

CSA 1
Extra Practice

Do This: Use the following table to create an array of US regions and their population in 2010. Then print out
the relationships using formatted Strings as specified in the comment. Finally, update the population array to
the 2019 census data and print the new relationships.

Region 2010 Total Population 2019 Total Population

Northeast 55,317,240 55,982,803

Midwest 66,927,001 68,329,004

South 114,555,744 125,580,448

West 71,945,553 78,347,268

public class Census


{
public static void main(String[] args)
{
// declare and initialize a String array called regions that holds 4 elements as shown in the table
String[] regions = {"Northeast", "Midwest", "South", "West"};

// declare and initialize an int array called population that holds the 2010 population values
// for each region

Int[] population = {55,317,240, 66,927,001, 114,555,744, 71,945,553};

// Print out the population for each region in the format:


// The <region> region had a population of <population> in 2010.
System.out.println( "The " + regions[0] + " region had a population of " + population[0]+ " in 2010");
System.out.println( "The " + regions[1] + " region had a population of " + population[1]+ " in 2010");
System.out.println( "The " + regions[2] + " region had a population of " + population[2]+ " in 2010");
System.out.println( "The " + regions[3] + " region had a population of " + population[3]+ " in 2010");
// Modify the value in the population array to contain the 2019 population data

Population[0] = 55,982,803;
Population[1] = 68,329,004;
Population[2] = 125,580,448;
Population[3] = 78,347,268;

// Print out the population for each region in the format:


// The <region> had a population of <population> in 2019.

System.out.println( "The " + regions[0] + " region had a population of " + population[0]+ " in 2019");
System.out.println( "The " + regions[1] + " region had a population of " + population[1]+ " in 2019");
System.out.println( "The " + regions[2] + " region had a population of " + population[2]+ " in 2019");
System.out.println( "The " + regions[3] + " region had a population of " + population[3]+ " in 2019");

}
}

CSA 2

You might also like