Computer Science : Standard Data Structures

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

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

Example Question #101 : Standard Data Structures

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: [Int] = []

var arr = []

Correct answer:

var arr: [Int] = []

Explanation:

In Swift, the variable must be declared first with var then given a name. So now we have var arr then to unwrap, we must add a type var arr: [Int] and then initialize. Therefore, we have var 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 with var then given a name. So now we have var arr then to unwrap, we must add a type var arr: [String] and then initialize. Therefore, we have var arr: [String] = []var arr = [] is technically correct, but the prompt asks you to unwrap the variable. 

Example Question #31 : Common Data Structures

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.

When array contains at least one negative integer

Yes, it works as intended.

It will work, but unexpectedly fail on occasion.

It never works.

Correct answer:

When array contains at least one negative integer

Explanation:

The code segment will work only when the array has 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 #1 : Arrays

Which of these instantiate a matrix called matrx with 5 columns and 4 rows that takes in integers?

Possible Answers:

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

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

int [] [] matrx = new int [] [5];

int [] [] matrx = new int [5] [];

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

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