AP Computer Science A Unit 4A: Data Sets, Arrays, and Text Files Practice Questions
Practice questions for AP Computer Science A Unit 4A on data sets, arrays, and text files in Java, plus data ethics, with explanations for every answer.
Questions
An app collects users' locations to show local weather. It later sells that location history to advertisers. What is the main ethical concern?
- Data is used for a purpose users never agreed to when they signed up
- The forecasts might be inaccurate
- Location data can't go in arrays
- Advertisers prefer data stored in text files
A data set used to train a hiring program contains mostly résumés from one group of people. What risk does this create?
- The program may treat applicants from other groups unfairly
- The program will run out of memory
- The program cannot read text files
- The data set will be too small
Why might a researcher remove names and ID numbers from a data set before sharing it?
- To protect individuals' privacy
- To make the data set load faster into an ArrayList
- Because Java cannot store Strings in arrays
- To increase the number of records
Collecting more data than a program needs increases the harm if the data is ever leaked.
If data is publicly available, there are no ethical concerns about how it is used.
Which are responsible data-collection practices?
- Telling users what data is collected
- Collecting only what is needed
- Storing data securely
- Keeping all data forever just in case
What is the general term for information that can identify a specific person, such as a name or Social Security number? (abbreviation)
What is a data set?
- A collection of related data, often stored in a file
- A single variable
- A Java method that sorts an array
- An error that occurs when reading a file
A data set lists daily temperatures for a year. Which question can a program answer using only this data?
- What was the hottest day?
- Why was the summer hotter than the previous year's summer?
- How many people went swimming each day?
- Which city has the best weather?
A survey data set has some blank responses. What should an analysis do?
- Decide deliberately how to handle the missing values
- Treat every blank response as a 0 without thinking about it
- Delete the entire data set
- Assume blanks mean the highest possible answer
Finding that two values in a data set rise together proves that one causes the other.
Programs can find patterns in data sets that would be impractical to find by hand.
Which Java structure is most natural for storing a fixed number of values loaded from a data set?
- An array
- A single int variable
- A String literal
- A boolean
What is printed? int[] a = new int[4]; System.out.println(a[2]);
- 0
- null
- 2
- An exception
What is printed? String[] s = new String[3]; System.out.println(s[0]);
- null
- ""
- 0
- An exception
What is printed? boolean[] f = new boolean[2]; double[] d = new double[2]; System.out.println(f[1] + " " + d[1]);
- false 0.0
- true 0
- null null
- false 0
What is printed? int[] a = {4, 8, 15, 16, 23, 42}; System.out.println(a.length + " " + a[a.length - 1]);
- 6 42
- 5 42
- 6 23
- 42 6
What is printed? int[] a = {3, 6, 9}; a[1] = a[0] + a[2]; System.out.println(a[1]);
- 12
- 6
- 9
- 15
What happens when this runs? int[] a = {1, 2, 3}; System.out.println(a[3]);
- An ArrayIndexOutOfBoundsException is thrown
- 3 is printed
- 0 is printed
- The code does not compile
What is printed? int[] a = {1, 2, 3}; int[] b = a; b[0] = 99; System.out.println(a[0]);
- 99
- 1
- 0
- An exception
What is printed? int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; System.out.println(a == b);
- false
- true
- 3
- A compile error
Why does this code fail to compile? int[] a = {1, 2, 3}; System.out.println(a.length());
- length is a field for arrays, not a method
- Arrays cannot be printed
- a must be declared with new
- length only works on Strings
Which statement creates an array that can hold 10 doubles?
- double[] d = new double[10];
- double d = new double[10];
- double[] d = new double(10);
- double[10] d = new double[];
What is the index of the last element in an array arr?
- arr.length - 1
- arr.length
- arr.length + 1
- 1
Once an array is created, its length cannot change.
The first element of an array is at index 1.
int[] a = {5, 10, 15, 20}; What is a[1] + a[3]?
int[] nums = new int[7]; What is nums.length?
What is printed? int[] a = {2, 4, 6}; for (int i = 0; i < a.length; i++) { System.out.print(a[i] + i + " "); }
- 2 5 8
- 2 4 6
- 0 1 2
- 3 6 9
What is printed? int[] a = {5, 1, 7}; for (int i = a.length - 1; i >= 0; i--) { System.out.print(a[i]); }
- 715
- 517
- 751
- 157
What is printed? int[] a = {1, 2, 3}; for (int x : a) { x = x * 10; } System.out.println(a[0] + a[1] + a[2]);
- 6
- 60
- 123
- 102030
What is printed? int[] a = {1, 2, 3}; for (int i = 0; i < a.length; i++) { a[i] = a[i] * 10; } System.out.println(a[0] + a[1] + a[2]);
- 60
- 6
- 123
- 102030
What is printed? String[] w = {"to", "be", "or"}; String out = ""; for (String s : w) { out += s.substring(0, 1); } System.out.println(out);
- tbo
- tob
- to be or
- t
What happens when this runs? int[] a = {4, 5, 6}; for (int i = 0; i <= a.length; i++) { System.out.print(a[i] + " "); }
- It prints 4 5 6, then throws ArrayIndexOutOfBoundsException
- It prints 4 5 6 and ends normally
- It prints 4 5 and ends normally
- It does not compile
What is printed? int[] a = {10, 20, 30, 40, 50}; for (int i = 0; i < a.length; i += 2) { System.out.print(a[i] + " "); }
- 10 30 50
- 20 40
- 10 20 30
- 10 30
When can't an enhanced for loop (for (int x : arr)) replace an indexed for loop?
- When you need to change array elements or use their indexes
- When the array contains int values instead of objects
- When the array has more than ten elements
- When you only need to read each value once
Which loop correctly prints every element of an array arr?
- for (int i = 0; i < arr.length; i++)
- for (int i = 1; i <= arr.length; i++)
- for (int i = 0; i <= arr.length; i++)
- for (int i = 1; i < arr.length; i++)
An enhanced for loop visits array elements in order from index 0 to the last index.
In for (int x : arr) { x = 0; }, every element of arr becomes 0.
int[] a = {3, 1, 4}; int s = 0; for (int x : a) { s += x * x; } What is s?
What is printed? int[] a = {7, -2, 9, 4}; int max = a[0]; for (int x : a) { if (x > max) { max = x; } } System.out.println(max);
- 9
- 7
- 4
- -2
What is printed? int[] a = {-5, -2, -9}; int max = 0; for (int x : a) { if (x > max) { max = x; } } System.out.println(max);
- 0
- -2
- -9
- -5
What is printed? double[] d = {2.0, 3.0, 4.0, 5.0}; double sum = 0; for (double x : d) { sum += x; } System.out.println(sum / d.length);
- 3.5
- 3.0
- 14.0
- 3
What is printed? int[] a = {3, 8, 2, 8, 5, 8}; int count = 0; for (int x : a) { if (x == 8) { count++; } } System.out.println(count);
- 3
- 2
- 8
- 6
What is printed? int[] a = {1, 2, 3, 4, 5}; for (int i = 0; i < a.length / 2; i++) { int t = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = t; } for (int x : a) { System.out.print(x); }
- 54321
- 12345
- 52341
- 21543
What is printed? int[] a = {1, 2, 3, 4}; int first = a[0]; for (int i = 0; i < a.length - 1; i++) { a[i] = a[i + 1]; } a[a.length - 1] = first; for (int x : a) { System.out.print(x); }
- 2341
- 4123
- 1234
- 2344
What is printed? int[] a = {1, 2, 3, 4}; for (int i = 1; i < a.length; i++) { a[i] = a[i - 1]; } for (int x : a) { System.out.print(x); }
- 1111
- 1123
- 1234
- 2344
What is printed? int[] a = {4, 6, 6, 2, 9, 9, 9}; int pairs = 0; for (int i = 0; i < a.length - 1; i++) { if (a[i] == a[i + 1]) { pairs++; } } System.out.println(pairs);
- 3
- 2
- 4
- 5
What is printed? int[] a = {2, 4, 7, 8}; boolean allEven = true; for (int x : a) { if (x % 2 != 0) { allEven = false; } } System.out.println(allEven);
- false
- true
- 7
- 3
What is printed? int[] a = {5, 3, 9, 1}; int minIdx = 0; for (int i = 1; i < a.length; i++) { if (a[i] < a[minIdx]) { minIdx = i; } } System.out.println(minIdx);
- 3
- 1
- 0
- 4