Introduction to Using Data Sets

Help Questions

AP Computer Science A › Introduction to Using Data Sets

Questions 1 - 6
1

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

At a pop-up snack shop, the owner logs daily granola bar sales for a 7-day event to decide whether to reorder more inventory. Because the event lasts exactly one week, an array is used where each element stores one day’s sales. When a counting mistake is found, the owner must modify one element and then recompute totals to make an accurate reorder decision.


int[] dailySales = {20, 18, 22, 19, 25, 21, 17};

int total = 0;

for (int i = 0; i <= dailySales.length; i++) {

    total += dailySales<u>i</u>; // add each day

}

System.out.println(total);

Refer to the problem scenario in the passage, identify the error in the code snippet and suggest a fix.

Replace dailySales[i] with dailySales.get(i) for arrays

Start loop at i = 1 to avoid out-of-bounds

Change total to double to prevent integer overflow

Change i <= dailySales.length to i < dailySales.length

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and understanding array bounds and loop conditions. Data sets in Java require careful attention to indexing, as arrays use zero-based indexing from 0 to length-1, making proper loop bounds crucial for avoiding runtime errors. In this passage, the scenario involves a loop that attempts to access dailySales[7] when the array only has indices 0-6, causing an ArrayIndexOutOfBoundsException. Choice A is correct because changing the condition from i <= dailySales.length to i < dailySales.length ensures the loop stops at index 6, preventing the out-of-bounds access. Choice C is incorrect as it confuses array syntax with ArrayList methods, while choice D would skip the first element entirely. To help students: Emphasize that array indices run from 0 to length-1, practice debugging common off-by-one errors, and use visual representations of arrays to reinforce proper indexing.

2

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

A pharmacy tracks daily mask sales in an int[] for a week to decide whether to place a larger order. The manager needs a quick way to update Thursday’s sales when a late online order is added, then recompute the total for decision-making.


int[] dailySales = {14, 12, 16, 11, 13, 15, 10};

// Add 3 more sales to Thursday (index 3)

dailySales<u>3</u> += 3;

int total = 0;

for (int i = 0; i < dailySales.length; i++) {

    total += dailySales<u>i</u>;

}

Refer to the problem scenario in the passage, what is the outcome of the given code snippet for total?

88

91

94

97

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and in-place array modification with compound operators. Data sets can be updated dynamically, and compound assignment operators like += provide a concise way to modify existing values based on new information. In this passage, the scenario involves adding 3 to Thursday's sales (index 3), changing it from 11 to 14, then recalculating the total for all seven days. Choice B is correct because the original sum is 91, and adding 3 more sales increases the total to 94 (14+12+16+14+13+15+10=94). Choice A (91) would be the original total before the update, while other choices don't match the arithmetic. To help students: Practice compound assignment operators on array elements, trace through code that modifies arrays before processing, and emphasize how array changes affect subsequent calculations.

3

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

In a small bookstore, the manager records the number of mystery novels sold each day for a week to decide how many to reorder. The data set is a fixed-size array because there are always 7 days in a week, and each index represents a day (0 = Monday, 6 = Sunday). By summing the array, the manager estimates demand; by computing an average, the manager decides whether to increase next week’s order. If a late receipt changes Tuesday’s count, the array value must be updated before recalculating totals.


int[] dailySales = {12, 9, 15, 8, 11, 14, 10};

int total = 0;

for (int i = 0; i < dailySales.length; i++) {

    total += dailySales<u>i</u>; // add each day

}

double avg = (double) total / dailySales.length;

Refer to the problem scenario in the passage, what is the value of avg after the code executes?

11.0

11.285714285714286

12.0

79.0

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and understanding their application in Java. Data sets in Java, such as arrays, ArrayLists, and HashMaps, allow efficient data storage and manipulation, with arrays being ideal for fixed-size collections like weekly sales data. In this passage, the scenario involves calculating the average daily sales of mystery novels over a week, requiring students to trace through array summation and division operations. Choice B is correct because it shows the result of summing all values (12+9+15+8+11+14+10=79) and dividing by 7, yielding 79.0/7 = 11.285714285714286. Choice A (11.0) is incorrect as it represents integer division without proper casting, while choices C and D represent the total sum and a single day's value respectively. To help students: Practice tracing through loops with running totals, emphasize the importance of casting for floating-point division, and use real-world scenarios to make array operations more concrete.

4

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

A small electronics shop tracks daily headphone sales for one week to decide whether to reorder. The manager uses an int[] because the number of days is fixed and wants to retrieve Friday’s sales quickly for a report. Friday corresponds to index 4 when Monday is index 0.


int[] dailySales = {4, 6, 5, 7, 9, 8, 3};

int fridaySales = dailySales<u>4</u>; // Friday

System.out.println(fridaySales);

Refer to the problem scenario in the passage, which method call retrieves Friday’s sales most directly?

dailySales(4)

dailySales[4]

dailySales.get(4)

dailySales.length(4)

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and proper array element access syntax. Data sets in Java arrays use bracket notation [index] for direct element access, providing constant-time retrieval of any element by its position. In this passage, the scenario requires retrieving Friday's sales (index 4) from the weekly sales array, demonstrating the simplicity of array indexing. Choice B is correct because dailySales[4] is the proper Java syntax for accessing the element at index 4, which returns the value 9. Choice A incorrectly uses ArrayList syntax .get() on an array, while choice C uses function call syntax that doesn't exist for arrays. To help students: Distinguish between array and ArrayList syntax, practice direct array access operations, and reinforce that arrays provide O(1) access time for any valid index.

5

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

A movie theater records ticket sales each day in an int[] for a 7-day period to decide which days need more staff. The manager wants to compute the average tickets sold per day, ensuring floating-point division. This average helps decide whether to add an extra cashier next week.


int[] dailySales = {100, 120, 90, 110, 130, 140, 80};

int total = 0;

for (int s : dailySales) {

    total += s; // sum tickets

}

double avg = total / dailySales.length;

System.out.println(avg);

Refer to the problem scenario in the passage, how would you modify the code to achieve a correct decimal average?

Use avg = (double) total / dailySales.length

Use avg = dailySales.length / (double) total

Use avg = total / (double) (dailySales.length - 1)

Change avg to int to match total type

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and proper type casting for floating-point division. Data sets often require calculating averages, which necessitates floating-point division to preserve decimal precision rather than truncating to integers. In this passage, the scenario shows integer division (total/dailySales.length) which truncates the decimal portion, requiring a cast to double for accurate results. Choice B is correct because casting total to double before division ensures floating-point arithmetic: (double)total/dailySales.length produces the exact average. Choice A would make avg an integer, losing precision, while choice D inverts the division operation entirely. To help students: Emphasize when integer division occurs (both operands are integers), practice strategic casting for desired results, and demonstrate the difference between integer and floating-point division outcomes.

6

A store tracks weekly sales in an int[] and uses totals for restocking decisions. Consider:

A farmer’s market vendor records daily smoothie sales in an int[] for a 7-day festival to decide how much fruit to bring the next morning. The vendor wants to quickly compute the total sales for the week and the average per day, using a simple loop over the array. Since the data set size is fixed, the array avoids resizing overhead.


int[] dailySales = {5, 7, 6, 9, 8, 10, 4};

int total = 0;

for (int i = 0; i < dailySales.length; i++) {

    total = dailySales<u>i</u>; // update total

}

System.out.println(total);

Refer to the problem scenario in the passage, identify the error in the following code snippet and suggest a fix.

Use total += dailySales[i] instead of total = dailySales[i]

Change dailySales.length to dailySales.size() for arrays

Start i at 1 to avoid double-counting Monday

Initialize total to 1 so multiplication works correctly

Explanation

This question tests AP Computer Science A skills, specifically the introduction to using data sets and understanding accumulator patterns in loops. Data sets often require aggregation operations like summing, which use accumulator variables that must be properly updated with += rather than simple assignment. In this passage, the scenario shows a common error where total = dailySales[i] overwrites the accumulator instead of adding to it, resulting in total holding only the last value (4). Choice A is correct because changing to total += dailySales[i] properly accumulates all values, giving the correct sum of 49. Choice C is incorrect as it misunderstands the problem as multiplication, while choice D would skip the first day's data unnecessarily. To help students: Emphasize the difference between assignment (=) and compound assignment (+=), use trace tables to show how variables change in loops, and practice identifying accumulator pattern errors.