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.

72 questions · AP Computer Science A · by Study Foxy

Study these questions free

Questions

  1. 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
  2. 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
  3. 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
  4. Collecting more data than a program needs increases the harm if the data is ever leaked.

    True or false

  5. If data is publicly available, there are no ethical concerns about how it is used.

    True or false

  6. 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
  7. What is the general term for information that can identify a specific person, such as a name or Social Security number? (abbreviation)

  8. 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
  9. 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?
  10. 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
  11. Finding that two values in a data set rise together proves that one causes the other.

    True or false

  12. Programs can find patterns in data sets that would be impractical to find by hand.

    True or false

  13. 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
  14. What is printed? int[] a = new int[4]; System.out.println(a[2]);

    • 0
    • null
    • 2
    • An exception
  15. What is printed? String[] s = new String[3]; System.out.println(s[0]);

    • null
    • ""
    • 0
    • An exception
  16. 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
  17. 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
  18. What is printed? int[] a = {3, 6, 9}; a[1] = a[0] + a[2]; System.out.println(a[1]);

    • 12
    • 6
    • 9
    • 15
  19. 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
  20. What is printed? int[] a = {1, 2, 3}; int[] b = a; b[0] = 99; System.out.println(a[0]);

    • 99
    • 1
    • 0
    • An exception
  21. What is printed? int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; System.out.println(a == b);

    • false
    • true
    • 3
    • A compile error
  22. 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
  23. 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[];
  24. What is the index of the last element in an array arr?

    • arr.length - 1
    • arr.length
    • arr.length + 1
    • 1
  25. Once an array is created, its length cannot change.

    True or false

  26. The first element of an array is at index 1.

    True or false

  27. int[] a = {5, 10, 15, 20}; What is a[1] + a[3]?

  28. int[] nums = new int[7]; What is nums.length?

  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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++)
  38. An enhanced for loop visits array elements in order from index 0 to the last index.

    True or false

  39. In for (int x : arr) { x = 0; }, every element of arr becomes 0.

    True or false

  40. int[] a = {3, 1, 4}; int s = 0; for (int x : a) { s += x * x; } What is s?

  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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

Showing 50 of 72. Open the quiz to study them all.