Java Arrays
Suppose the test against the the string is "supal," what value does y have? (Refer to #45)
"False," because all the characters must match for it to be true.
Suppose data is the string "Broken" What is the value of part in the expression: part = data.substring(3,6); ? What type of variable is part?
"part" would be a string. In this case, the variable is "ken"
Suppose you have two arrays: num1 and num2. What does: num1 = num2 do?
(Option C) You are causing num1 to reference num2
What type of an array would you use to hold the hours you work each day for a part time job (assume you work diff numbers of hours each day)?
Irregular
Explain what a Bubble sort is and how does it word? How many loop do you need?
It compares values at two index locations and does a swap if necessary. It repeats this process over and over. You need two loops
Explain how a binary search works starts with a sorted list and cuts in half over and over while making comparisons
It cuts the list in half and then in half over and over again, comparing if the target value is above or below the middle value.
What does the following do, y = test.equals("sup"), and what type of variable is y?
It returns true if there is a match. The variable "y" is boolean
How does a linear search work?
It sequentially checks all the values until a match is found.
What is an irregular array and give an example of declaring one called bus for a bus schedule that just runs M, W, F for 4 times each day, on Tues, Thurs, and Sat twice, and Sun once.
It would be an array that has a different number of columns for each row. int bus[][] = new int[7][]; bus[0] = new int[4]; //on Mondays you can hold 4 data pts bus[1] = new int[2]; // on Tuesday, 2 data pts bus[2] = new int[4]; //Wednesday hold 4 data points //etc.
Which way can be used if the data is unorganized?
Linear
What are the two ways to search a data set?
Linear, and Binary
Can the same array hold both the name of a student and their grades?
No, you cannot mix types
What are strings in Java?
Objects
How many different variable types can the same array hold?
One
Why are arrays convenient?
They group together related data, organize data, and assist in the sorting and searching of data
If you have two arrays, one called apple, and one orange, does this work: apple = orange;
Yes
Can a string have just one character? Show an example and show the same answer but with a different variable type.
Yes String one = "A"; char one = 'A';
What type of array would you use to hold all of your final exam grades (assume just 1st and 2nd semester) for all of your high school years. Write a piece of code to declare it.
int g[][][] = new int[3][2][4]
Write a method to find the minimum value in a one dimensional array.
int min = 10000; for (int i=0; i< 10; i++) if (sample[i] < min) min = sample[i];
Even though will typically use one step to declare an array, the creation of an array is a two step process, what are the two steps?
int sample[]; sample = new int[10]
What single line of code can be used to declare a 1D array called temp to hold 3 items?
int temp[] = new int[3];
Write a piece of code to initial an array called g that's 2X3X2 to even numbers starting with zero.
n=0; for (int d=0; d< 2; d++) for (int c=0; c< 3; c++) for (int r=0; r<2; r++) g[r][c][d] = n; n = n + 2;
How are arrays implemented?
objects
Suppose data is the string "BrokenBroken" What is the value of x in the expression x = data.lastIndexOf("on") ?
x = -1
Suppose data is the string "BrokenBroken" What is the value of x in the expression x = data.lastIndexOf("en") ?
x = 10
Suppose data is the string "BrokenBroken" What is the value of x in the expression x = data.indexOf("en"); ?
x = 4
If data = "Z", what is the value of x in the expression x = data.compareTo("B") ?
x = some positive number
Referring to #21, if x = bus.length, y = bus[0].length, and z = bus[1].length, what are the values for x , y, and z?
x =7 y = 4 z = 2
Suppose data is the string "Broken". What is the value of x in the expression x = data.compareTo("Summer"); ?
x will be some negative number, because there is no match
How can you search for a period in a sentence?
y = sentence.indexOf(".") OR ch = sentence.charAt(".");
Suppose data is the string "finish now". What does the following do? z = data.length(); What type is the variable z?
z is an integer and in this case z = 10
What kind of number is the index value for an array?
0, or a positive integer
How many loops are necessary to find the largest number in an array?
1
How many loops are necessary in the Bubble sort routine?
2
How many data points can array that's 2 x 3 x 4 hold?
24
What type of an array would you use to hold the weight and height of students in the class of 2010?
2D [22][2] in this case the row is the student number, if you had 22 in the class
If an array has 5 elements, what is the index value for the last item in the array?
4
What would x = d.length give for the value of x? (Look at #14)
5, because of the amount of characters
Suppose you are thinking of a number from 0 to 100, what is the first number that it's compared to if searching in binary?
50
What is an array?
A set of data of the same type referenced with a common name
Give an example of a need to use a 3-d array
Storing height, weight, and blood pressure
How can the contents of a string variable be altered?
String are immutable, so their contents can't be altered
Create and fill an array with the strings "This", "is", and "great"
String data[] = {"This", "is", "great"};
Create a string that has no characters
String empty = "";
Create an appropriate array to hold the names and phone numbers of 3 people.
String numbers[][] = { {"Tom", "555-3322"}, {"Sam", "555-8976"}. {"Cole", "596-2142"} };
List an easier way to construct a string
String statement = "Hello";
List a way to construct a string
String statement = new String("Hello");
Can an array hold strings?
Yes
Which search routine is faster for sorted data?
binary
Give some examples where you might use arrays
grades, bus schedule, race times, wind data
What describes the position of an element within an array?
index
What is the term that describes the items that is used to access a value in an array?
index
How do you declare a one-dimensional array to hold ten characters?
char sample[] = new char[10]
What message do you get if you go beyond the index range of an array?
index out of bounds
How can you declare and initialize a one-d array called d with the values {2, 5, 7, 1, 10} using one line of code?
int d[] = {2,5,7,1,10};
What are some advantages of using arrays to hold data?
easily manipulated, sorted easily, tallied easily, searched easily
Write a piece of code to initialize the five elements of a one-d array to the int value of 10.
for (int i=0; i<5; i++) sample[i] = 10;
Suppose data is the string "open now." What does ch = data.charAt(8) return?
returns the character
What does ch = data.charAt(0) return?
returns the o
How can you tell if a character is capitalized?
the decimal value is between 65 and 90 inclusive