Chapter 8: Collections

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Count how many odd numbers exist in the array. cntOdd = 0; for (i = 0; i < myVals.length; ++i) { if ( (myVals[i] % 2) == 1 ) { ____________ ; } }

++cntOdd

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; With what value does currYear = yearsArr[3] assign currYear? 3 2025 invalid index 0

0 When yearner is created, all array elements are initialized with the default int value, or 0. So, currYear is assigned with 0.

Array scoresList has 10 elements with indices 0 to 9, accessed as scoresList[0] to scoresList[9]. If that array instead has 100 elements, what is the last element's index?

99

Given that this loop iterates over all items of the array, how many items are in the array? for (i = 0; i < 99; ++i) { System.out.println(someArray[i]); }

99

How many total elements are stored within array familyAges with 50 elements, and array familyHeights with 50 elements?

100

What is the value of peoplePerDay[8]? peoplePerDay[9] = 5; peoplePerDay[8] = peoplePerDay[9] - 3; 8 5 2

2

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; What value is stored in yearsArr[1]? 1 1999 2012

2012

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; With what value does currYear = yearsArr[2] assign currYear? 2 2025 invalid index

2025

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; What are the contents of the array if the above code is followed by the statement: yearsArr[0] = yearsArr[2]? 1999, 2012, 1999, 0 2012, 2012, 2025, 0 2025, 2012, 2025, 0

2025, 2012, 2025, 0 Each element is its own variable, and can be read and assigned just like any other variable. yearsArr[3] was never assigned so its value is 0.

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; How many elements in memory does the array declaration create? 0 1 2 3 4

4 the declaration creates array yearsArr with 4 elements

Assume N is initially 1. What is the value of peoplePerDay[2]? peoplePerDay[N] = 15; N = N + 1; peoplePerDay[N] = peoplePerDay[N - 1] * 3; 15 2 45

45 peoplePerDay[1] is assigned with 15. Then, peoplePerDay[2] is assigned with peoplePerDay[1] * 3, which is 15 * 3, so 45.

Given: int[] maxScores = {20, 20, 100, 50};. What is maxScores[3]?

50

If the array's last index was 499, how many elements does the array have?

500

import java.util.Scanner; public class ArrayPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int NUM_ELEMENTS = 8; // Number of elements in array int[] userVals = new int[NUM_ELEMENTS]; // User numbers int i; // Loop index System.out.println("Enter " + NUM_ELEMENTS + " integer values..."); for (i = 0; i < userVals.length; ++i) { userVals[i] = scnr.nextInt(); System.out.println("Value: " + userVals[i]); } System.out.print("You entered: "); for (i = 0; i < userVals.length; ++i) { System.out.print(userVals[i] + " "); } System.out.println(); } } What is userVals.length? 1 8

8

import java.util.Scanner; public class ArrayPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int NUM_ELEMENTS = 8; // Number of elements in array int[] userVals = new int[NUM_ELEMENTS]; // User numbers int i; // Loop index System.out.println("Enter " + NUM_ELEMENTS + " integer values..."); for (i = 0; i < userVals.length; ++i) { userVals[i] = scnr.nextInt(); System.out.println("Value: " + userVals[i]); } System.out.print("You entered: "); for (i = 0; i < userVals.length; ++i) { System.out.print(userVals[i] + " "); } System.out.println(); } } How many times does each for loop iterate? 1 8 unknown

8 NUM_ELEMENTS is 8, which is used to allocate an array of 8 elements. So, userVals.length is 8, and the for loop iterates through the 8 array elements.

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; What is the index of the last element for the following array: int[] pricesArr = new int[100]; 99 100 101

99 The 100 elements will have indices 0...99

8.1

Array concept(general)

8.2

Arrays

Given the following code: final int NUM_ELEMENTS = 5; int[] myArray = new int[NUM_ELEMENTS]; int i; To find the maximum element value, a reasonable statement preceding the for loop is: maxVal = 0; T/F

False 0 would yield a wrong final maxVal if all element values were negative. A better statement would be: maxVal = myArray[0].

postageCosts[2] represents the cost for a weight of 2 ounces. True False

False The index is used to access the lists in increasing order of weight. letterWeights[2] is 3, meaning postageCosts[2] corresponds to 3 ounces.

Given the following code: final int NUM_ELEMENTS = 5; int[] myArray = new int[NUM_ELEMENTS]; int i; The normal for loop structure iterates as long as:i <= myArray.length T/F

False The normal structure uses <, not <=. The <= causes the loop to iterate outside of the array's valid index range.

8.4

Iterating through arrays

8.5

Multiple Arrays

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; Recall that the array declaration was int[] yearsArr = new int[4];. Is currYear = yearsArr[4] a valid assignment? Yes, it accesses the fourth element. No, yearsArr[4] does not exist.

No, yearsArr[4] does not exist.

letterWeights[0] is 1, meaning element 0 of letterWeights and postageCosts correspond to a weight of 1 ounce. True False

True The first weight for which the USPS defines a cost is 1 ounce.

Given the following code: final int NUM_ELEMENTS = 5; int[] myArray = new int[NUM_ELEMENTS]; int i; To compute the sum of elements, a reasonable statement preceding the for loop is: sumVal = 0; T/F

True sumVal starts at 0, then as the for loop iterates through each element, that element's value gets added to sum.

Array

a special variable having one name, but storing a list of data items, with each item being directly accessible.

element

each item in an array

import java.util.Scanner; public class ArrayPrinter { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int NUM_ELEMENTS = 8; // Number of elements in array int[] userVals = new int[NUM_ELEMENTS]; // User numbers int i; // Loop index System.out.println("Enter " + NUM_ELEMENTS + " integer values..."); for (i = 0; i < userVals.length; ++i) { userVals[i] = scnr.nextInt(); System.out.println("Value: " + userVals[i]); } System.out.print("You entered: "); for (i = 0; i < userVals.length; ++i) { System.out.print(userVals[i] + " "); } System.out.println(); } } Which one line of code can be changed to allow the user to enter 100 elements? final int NUM_ELEMENTS = 8; for (I = 0; i < userVals.length; ++i){

final int NUM_ELEMENTS = 8; Using arrays and loops, only the constant literal 8 would be changed

Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}. import java.util.Scanner; public class StoreGuesses { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_GUESSES = 3; int[] userGuesses = new int[NUM_GUESSES]; int i; /* Your solution goes here */ for (i = 0; i < userGuesses.length; ++i){ System.out.print(userGuesses[i] + " "); } } }

for(i = 0; i < userGuesses.length; ++i){ userGuesses[i] = scnr.nextInt(); }

Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] hourlyTemp = new int[NUM_VALS]; int i; for (i = 0; i < hourlyTemp.length; ++i) { hourlyTemp[i] = scnr.nextInt(); } /* Your solution goes here */ System.out.println(""); } }

for(i=0; i<NUM_VALS; i++){ if(i == (NUM_VALS-1)){ System.out.print(hourlyTemp[i]); } else { System.out.print(hourlyTemp[i]+", "); } }

Complete the code to print all items for the given array, using the above common loop structure. int[] daysList = new int[365]; ... for (i = 0; ; ++i) _______________________; ++i) { System.out.println(daysList[i]); }

i < daysList.length

Write a single statement to declare an array of ints named myVals with 4 elements each initialized to 10.

int [ ] myVals = {10,10,10,10};

Declare and initialize an array named myVals that stores 10 elements of type int with default values.

int [] myVals = new int[10];

Using two separate statements, declare two related integer arrays named seatPosition and testScore (in that order) each with 130 elements.

int[] seatPosition = new int[130]; int[] testScore = new int[130];

Assign the second element of array myVals with the value 555.

myVals[1] = 555;

Assign myVals array element at the index held in currIndex with the value 777.

myVals[currIndex] = 777;

Count how many negative numbers exist in the array. cntNeg = 0; for (i = 0; i < myVals.length; ++i) { if (_______________________) { ++cntNeg; } }

myVals[i] < 0 or myVals[i]<= -1

Assign numMatches with the number of elements in userValues that equal matchValue. userValues has NUM_VALS elements. Ex: If userValues is {2, 1, 2, 2} and matchValue is 2 , then numMatches should be 3. import java.util.Scanner; public class FindMatchValue { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] userValues = new int[NUM_VALS]; int i; int matchValue; int numMatches = -99; // Assign numMatches with 0 before your for loop matchValue = scnr.nextInt(); for (i = 0; i < userValues.length; ++i) { userValues[i] = scnr.nextInt(); } /* Your solution goes here */ System.out.println("matchValue: " + matchValue + ", numMatches: " + numMatches); } }

numMatches = 0; for(i = 0; i < NUM_VALS; ++i) { if(userValues[i] == matchValue) { numMatches = numMatches + 1; } }

Which assigns element 0 with the value 250? peoplePerDay[250] = 0 peoplePerDay[0] = 250 peoplePerDay = 250

peoplePerDay[0]=250

Which assigns element 1 with the value 99? peoplePerDay[1] = 99 peoplePerDay[99] = 1

peoplePerDay[1]=99

Array scoresList has 10 elements with indices 0 to 9, accessed as scoresList[0] to scoresList[9]. Assign the first element in scoresList with 77.

scoresList[0]=77;

Array scoresList has 10 elements with indices 0 to 9, accessed as scoresList[0] to scoresList[9]. Assign the second element in scoresList with 77.

scoresList[1]=77;

Array scoresList has 10 elements with indices 0 to 9, accessed as scoresList[0] to scoresList[9]. Assign the last element in scoresList with 77.

scoresList[9]=77;

Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS = 4; int[] testGrades = new int[NUM_VALS]; int i; int sumExtra = -9999; // Assign sumExtra with 0 before your for loop for (i = 0; i < testGrades.length; ++i) { testGrades[i] = scnr.nextInt(); } /* Your solution goes here */ System.out.println("sumExtra: " + sumExtra); } }

sumExtra = 0; for (i =0; i<NUM_VALS; i++){ if(testGrades[i] > 100){ sumExtra = sumExtra + (testGrades[i] -100); } }

Assign tempVal with the myVals array element at the index one after the value held in variable i.

tempVal = myVals[i+1];

Assign variable x with the value stored at index 8 of array myVals.

x = myVals[8];

Given: int[] yearsArr = new int[4]; yearsArr[0] = 1999; yearsArr[1] = 2012; yearsArr[2] = 2025; What is the proper way to access the first element in array yearsArr?

yearsArr[0]


संबंधित स्टडी सेट्स

Muscles Crossing the Elbow Joint

View Set

Triumph of parliament in England

View Set

Adding, Subtracting, Multiplying and Dividing Integers

View Set

Chapter 6 Distribution, Promotion, and Selling Key Terms

View Set

CPAS REVIEW - DRAFTING II - 2021

View Set

Chapter 10: Aggregate Supply & Aggregate Demand

View Set

Pharm Final test -- find chapter 81

View Set

AP US Government Unit 2 -- Chapter 13 to 16

View Set

Chapter 22: Drug Therapy for Tuberculosis and Mycobacterium avium Complex Disease

View Set

Frederick Douglass: An American Slave Overseers/Masters

View Set