All Computer Science Resources
Example Questions
Example Question #31 : Object Oriented Program Design
True or False.
The best data structure to represent a set of keys and values is an array.
True
False
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: Int = []
var arr = []
var arr: [Int] = []
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).
var arr: String = []
var arr = []
var arr: [String] = []
var arr = [String]
var arr: [String] = []
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 #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?
Yes, it works as intended.
It works when there are no negative integers.
It will work, but unexpectedly fail on occasion.
It never works.
When array contains at least one negative integer
When array contains at least one negative integer
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 #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.
Certified Tutor