AP Computer Science A : Standard Data Structures

Study concepts, example questions & explanations for AP Computer Science A

varsity tutors app store varsity tutors android store

Example Questions

1 2 3 4 5 6 7 8 9 11 Next →

Example Question #1 :Arrays

True or False.

The best data structure to represent a set of keys and values is an array.

Possible Answers:

False

True

Correct answer:

False

Explanation:

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).

Possible Answers:

var arr: Int = []

var arr: [Int] = []

var arr = []

var arr = [Int]

Correct answer:

var arr: [Int] = []

Explanation:

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).

Possible Answers:

var arr: String = []

var arr: [String] = []

var arr = [String]

var arr = []

Correct answer:

var arr: [String] = []

Explanation:

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?

Possible Answers:

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

Correct answer:

When array contains at least one negative integer

Explanation:

代码段将只有当数组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?

Possible Answers:

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];

Correct answer:

int [] [] matrx = new int [] [4];

Explanation:

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.

1 2 3 4 5 6 7 8 9 11 Next →
Learning Tools by Varsity Tutors