What this quiz covers
This quiz focuses on Implementing Array Algorithms, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Implement an algorithm to sort the following array in ascending order using insertion sort (no built-in sort): int[] a. Constraints: 0≤n≤104. After sorting, a must be in nondecreasing order. Example: [4,1,3] -> [1,3,4]. Which method correctly sorts the array?
public static void insertionSort(int[] a){
for(int i=1;i<a.length;i++){
int key=a[i];
int j=i-1;
while(j>=0 && a[j]>key){
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
public static void insertionSort(int[] a){
for(int i=1;i<a.length;i++){
int key=a[i];
int j=i-1;
while(j>0 && a[j]>key){
a[j+1]=a[j];
j--;
}
a[j]=key;
}
}
public static void insertionSort(int[] a){
for(int i=0;i<a.length;i++){
int min=i;
for(int j=i+1;j<a.length;j++) if(a[j]<a[min]) min=j;
int t=a[i]; a[i]=a[min]; a[min]=t;
}
}
public static void insertionSort(int[] a){
for(int i=1;i<=a.length;i++){
int key=a[i];
int j=i-1;
while(j>=0 && a[j]>key){ a[j+1]=a[j]; j--; }
a[j+1]=key;
}
}
AP Computer Science a Quiz
Practice Implementing Array Algorithms in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Implementing Array Algorithms, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
Implement an algorithm to sort the following array in ascending order using insertion sort (no built-in sort): int[] a. Constraints: 0≤n≤104. After sorting, a must be in nondecreasing order. Example: [4,1,3] -> [1,3,4]. Which method correctly sorts the array?
public static void insertionSort(int[] a){
for(int i=1;i<a.length;i++){
int key=a[i];
int j=i-1;
while(j>=0 && a[j]>key){
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}
}
(correct answer)public static void insertionSort(int[] a){
for(int i=1;i<a.length;i++){
int key=a[i];
int j=i-1;
while(j>0 && a[j]>key){
a[j+1]=a[j];
j--;
}
a[j]=key;
}
}
public static void insertionSort(int[] a){
for(int i=0;i<a.length;i++){
int min=i;
for(int j=i+1;j<a.length;j++) if(a[j]<a[min]) min=j;
int t=a[i]; a[i]=a[min]; a[min]=t;
}
}
public static void insertionSort(int[] a){
for(int i=1;i<=a.length;i++){
int key=a[i];
int j=i-1;
while(j>=0 && a[j]>key){ a[j+1]=a[j]; j--; }
a[j+1]=key;
}
}
Simulate a line at a help desk using an array-based queue. Implement enqueue (add to back) and dequeue (remove from front) for up to 104 operations. Use a circular buffer with int[] data and indices front, size (no java.util.Queue). Which code correctly dequeues one element and returns it, or returns -1 if empty?
public int dequeue(){
if(size==0) return -1;
int val=data[front];
front=(front+1)%data.length;
size--;
return val;
}
(correct answer)public int dequeue(){
if(size==0) return -1;
int val=data[front];
front=front+1;
size--;
return val;
}
public int dequeue(){
if(size==0) return -1;
front=(front+1)%data.length;
size--;
return data[front];
}
public int dequeue(){
if(size==0) return -1;
int val=data[front];
size--;
return val;
}
Given the following array, find the index of the first occurrence of target using binary search on a sorted array that may contain duplicates. Input: sorted int[] a (0≤n≤105), int target. Output: smallest index i with a[i]==target, else -1. Example: a=[1,2,2,2,5], target=2 -> 1. Which method is correct?
public static int firstIndex(int[] a,int target){
int lo=0, hi=a.length-1, ans=-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(a[mid]==target){ ans=mid; hi=mid-1; }
else if(a[mid]<target) lo=mid+1;
else hi=mid-1;
}
return ans;
}
(correct answer)public static int firstIndex(int[] a,int target){
int lo=0, hi=a.length-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(a[mid]==target) return mid;
if(a[mid]<target) lo=mid+1; else hi=mid-1;
}
return -1;
}
public static int firstIndex(int[] a,int target){
int lo=0, hi=a.length-1, ans=-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(a[mid]==target){ ans=mid; lo=mid+1; }
else if(a[mid]<target) lo=mid+1;
else hi=mid-1;
}
return ans;
}
public static int firstIndex(int[] a,int target){
int lo=0, hi=a.length;
int ans=-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(a[mid]>=target){ ans=mid; hi=mid-1; }
else lo=mid+1;
}
return (a[ans]==target)? ans : -1;
}
Given a sorted int[] ids (ascending, distinct) with 0≤n≤105, write a method that returns the index of target using binary search, or -1 if not found. No built-in search. Example: ids=[2,5,8,10], target=8 -> 2. Which implementation is correct?
public static int find(int[] ids,int target){
int lo=0, hi=ids.length-1;
while(lo<=hi){
int mid=(lo+hi)/2;
if(ids[mid]==target) return mid;
if(ids[mid]<target) lo=mid+1; else hi=mid-1;
}
return -1;
}
(correct answer)public static int find(int[] ids,int target){
int lo=0, hi=ids.length;
while(lo<=hi){
int mid=(lo+hi)/2;
if(ids[mid]==target) return mid;
if(ids[mid]<target) lo=mid+1; else hi=mid-1;
}
return -1;
}
public static int find(int[] ids,int target){
int lo=0, hi=ids.length-1;
while(lo<hi){
int mid=(lo+hi)/2;
if(ids[mid]==target) return mid;
if(ids[mid]<target) lo=mid; else hi=mid;
}
return -1;
}
public static int find(int[] ids,int target){
for(int i=0;i<ids.length;i++) if(ids[i]==target) return i;
return -1;
}
Perform a 2D array operation: write a method that returns the transpose of an int[][] m with r rows and c columns (1≤r,c≤200). The result must be a new array with c rows and r columns where result[j][i] = m[i][j]. Example: [[1,2,3],[4,5,6]] -> [[1,4],[2,5],[3,6]]. Which code is correct?
public static int[][] transpose(int[][] m){
int r=m.length, c=m[0].length;
int[][] t=new int[c][r];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
t[j][i]=m[i][j];
return t;
}
(correct answer)public static int[][] transpose(int[][] m){
int r=m.length, c=m[0].length;
int[][] t=new int[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
t[j][i]=m[i][j];
return t;
}
public static int[][] transpose(int[][] m){
int r=m.length, c=m[0].length;
int[][] t=new int[c][r];
for(int i=0;i<=r;i++)
for(int j=0;j<c;j++)
t[j][i]=m[i][j];
return t;
}
public static int[][] transpose(int[][] m){
int r=m.length, c=m[0].length;
int[][] t=new int[c][r];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
t[i][j]=m[j][i];
return t;
}