A method findMax is intended to return the largest value in an array of integers.
public int findMax(int[] nums) {
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
return max;
}
Which of the following preconditions must be true for the method to work without throwing an exception?
AP (Advanced Placement) · AP Computer Science A
Ethical and Social Issues Around Data Collection: Practice Questions
5 multiple-choice questions marked as you go, and 2 written questions with worked solutions. All on Ethical and Social Issues Around Data Collection.
Consider the following method that processes a 2D array:
public void process(int[][] mat) {
for (int r = 0; r < mat.length; r++) {
for (int c = 0; c < mat[0].length; c++) {
if (r == c) {
mat[r][c] *= 2;
}
}
}
}
If mat is initialized as {{1, 2}, {3, 4}}, what is the value of mat after process(mat) is called?
Consider the following method designed to reverse the elements in an array:
public void reverse(int[] data) {
for (int i = 0; i < data.length / 2; i++) {
int temp = data[i];
data[i] = data[data.length - 1 - i];
data[data.length - 1 - i] = temp;
}
}
If data is {1, 2, 3, 4, 5, 6}, how many total assignments to elements within data (lines involving data[...] = ...) are executed?
Consider the following code segment:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
What will be the contents of arr after the loop finishes execution?
Given a 2D array int[][] table = {{5, 10, 15}, {20, 25, 30}}, which expression correctly accesses the value 30?
Consider a scenario where you are managing a library system using a 1D array of Book objects named libraryShelf.
Part a: Write a code segment that iterates through the array and counts how many books have a price higher than \(P\), where \(P = 50.0\).
Part b: If the array is currently full with \(n\) elements, describe the steps and code logic required to 'remove' a book at index \(k\) while maintaining the relative order of the remaining books.
Part c: Calculate the minimum number of comparisons needed to find a specific book title in an unsorted array of size \(n = 100\) in the worst-case scenario.
Write your answer out first, then check it against the worked solution.
A 2D array int[][] matrix of size \(M \times N\) represents a grayscale image where each value is an intensity between 0 and 255.
Part a: Write a nested loop structure to identify if the matrix is 'horizontally symmetric'. A matrix is horizontally symmetric if for every row \(i\) and column \(j\), matrix[i][j] == matrix[i][N - 1 - j].
Part b: Write a method applyThreshold(int limit) that modifies the 2D array: any value greater than limit becomes 255, and any value less than or equal to limit becomes 0.
Part c: Analyze the time complexity of the symmetry check in terms of \(M\) and \(N\). If \(M = 1000\) and \(N = 1000\), how many total comparisons are performed in the worst case?
Write your answer out first, then check it against the worked solution.
* The content provided by thinka is generated by AI and may not always be accurate or up-to-date. Please use it as a supplementary resource and verify with official materials.
You've seen the model answer. Now get yours marked.
This page can show you how a good answer looks. It cannot tell you what your answer was missing. thinka marks your written work against the real mark scheme in about 15 seconds.
Want more questions like these? Get a fresh set on this topic, marked as you go.
Practise More