All AP Computer Science A Resources
Example Questions
Example Question #1 :Arrays
True or False.
The best data structure to represent a set of keys and values is an array.
False
True
False
Arrays can be two-dimensional. However, when trying to keep track of keys and values it can become complicated when using an array. HashMaps are the best way to represent data containing keys and values.
Example Question #31 :Common Data Structures
Define an unwrapped integer array in Swift (iOS).
var arr: Int = []
var arr: [Int] = []
var arr = []
var arr = [Int]
var arr: [Int] = []
In Swift, the variable must be declared first withvarthen given a name. So now we havevar arrthen to unwrap, we must add a typevar arr: [Int]and then initialize. Therefore, we havevar arr: [Int] = [].var arr = []is technically correct, but the prompt asks you to unwrap the variable.
Example Question #32 :Common Data Structures
Define an unwrapped string array in Swift (iOS).
var arr: String = []
var arr: [String] = []
var arr = [String]
var arr = []
var arr: [String] = []
In Swift, the variable must be declared first withvarthen given a name. So now we havevar arrthen to unwrap, we must add a typevar arr: [String]and then initialize. Therefore, we havevar arr: [String] = [].var arr = []is technically correct, but the prompt asks you to unwrap the variable.
Example Question #1 :Arrays
Suppose your friend has the following lines of code that intend to find the first index of the first positive integer in array[0] ... array[N-1], where array is an array of N integers
int i = 0;
while (array[i] >=0)
{
i++;
}
location = i;
Will your friend's code work as intended?
It works when there are no negative integers.
Yes, it works as intended.
It never works.
It will work, but unexpectedly fail on occasion.
When array contains at least one negative integer
When array contains at least one negative integer
代码段将只有当数组at least one integer. If the array has no negative integers, then the while loop will continue running, incrementing i past the length of the array, in which case an Out of Bounds Exception will occur.
Example Question #41 :Common Data Structures
Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?
int [] [] matrx = new int [5] [4];
int [] [] matrx = new int [5] [];
int [] [] matrx = new int [4] [5];
int [] [] matrx = new int [] [4];
int [] [] matrx = new int [] [5];
int [] [] matrx = new int [] [4];
You create a matrix also known as a 2 dimensional array the same way you'd instantiate a normal array except the first array space remains blank and you'd insert the number for the amount of rows. Due to the fact that you want 5 columns and 4 rows, you'd only input the 4 into the second array.