CBE 1212
What is the output of the following code? int [][] x = new int [3][ ]; x[0] = new int[3]; x[1] = new int[4]; x[2] = new int[5]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x[0].length; j++) count++; System.out.println(count);
9
What is the output of the following code? public static void main(String args[ ]) { int matrix[ ][ ]; matrix = new int[3][4]; for(int p = 0; p < 3; p++) { for(int q = 0; q < 4; q++) { System.out.print(matrix[q][p] + " "); } System.out.println(); } System.out.println(); }
Error Message - Out of Bounds
Constant values can be assigned using the System.in class.
False
String is one of the primitive data types.
False
Can a private method be called by any other method in the same class?
True
When an object is created from a class, the object inherits the class methods and fields
True
what is a literal?
fixed value in code
If you wanted to get the pixel that is in the top left-hand corner of a picture object, how would you get it? Assume the picture object is called pic.
pic.getPixel(0,0);
If you wanted to get the pixel that is in the bottom-right hand corner of a picture object, how would you get it? Assume the picture object is called pic.
pic.getPixel(pic.getWidth() - 1, pic.getHeight() - 1);
The state of variables and objects in your program after a method has finished executing is/are called __________.
postconditions
Something that must always be true prior to a method being executed is a __________.
precondition
What is the key word to jump out of a method?
return
What does the void do in a method definition?
specifies that the method doesn't return anything
Show the output of the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; increase(x); int[] y = {1, 2, 3, 4, 5}; increase(y[0]); System.out.println(x[0] + " " + y[0]); } public static void increase(int[] x) { for (int i = 0; i < x.length; i++) x[i]++; } public static void increase(int y) { y++; } }
2 1
In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + " "); } }
2 1 0
In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {3, 2, 1}; int[] list2 = {1, 2, 3, 4}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + " "); } }
2 1 0
What is the output of the following code? int[] myList = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < myList.length; i++) { myList[i - 1] = myList[i]; } for (int e: myList) System.out.print(e + " ");
2 3 4 5 6 6
How many total elements would the following multi-dimensional array have? int[][][][] array = new int [2][3][4][1];
24
What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] >= max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax);
4
In the following code, what is the printout for list2? class Test { public static void main(String[] args) { int[] list1 = {3, 2, 1, 4}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = list2.length - 1; i >= 0; i--) System.out.print(list2[i] + " "); } }
4 2 1 0
What is the output of the code? int x = -3; int [] data = {3,2,4,3,1,0}; data [1] = data[(4 + x)] + data[3]; System.out.println( data[1] );
5
What is the length of table[0].length? int[][][] table = new int[7][6][ ];
6
What is printed by the following code fragment? int[] a = {0,1,2,3,4,5,6}; System.out.println(a.length);
7
What is the output of the following code? public static void main(String[] args) { int[][] array = {{4, 1}, {1, 1, 2}}; System.out.println(method6 (array)); } public static int method6(int[][] method6) { int result = 0; for (int i = 0; i < method6.length; i++) for (int j = 0; j < method6 [i].length; j++) result += method6[i][j]; return result; }
9
What is the output of the following code? int [][] x = new int [3][ ]; x[0] = new int[5]; x[1] = new int[4]; x[2] = new int[3]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x.length; j++) count++; System.out.println(count);
9
What is a common error made by programmers while working with arrays shown in this example? int[] array = new int[5]; for (int i = 0; i <= 5; i++){ System.out.println(array[i]); }
Both initializing array and not assigning values to its elements AND off by 1 error for indexes
You can use the operator == to check whether two variables refer to the same array.
True
What is the usual name of the array that holds command line arguments?
args
How do you access the first command line argument in a program called ClassSignup?
args[0];
What is the correct term for numbers[99]?
indexed variable
What is the value of array[3]? public static void main(String[] args) { double a = 2.5; double b = 15.7; int array[] = {1,2,3,4}; for (int i = 0; i <= 3; i++){ array[i] += (b - a); } }
17
int[][] array ={{1 , 2 , 3},{6, 8, 10},{18, 19, 20}}; What number is array[2][1] equal to?
19
If you don't put a "break" at the end of your case statement what will happen, assuming the value of the switch variable matches the case?
The instructions in this case will run, and execution will fall through into the next case, possibly running more instructions than you intended.
What is the scope of a variable?
The part of the program that can refer to (access) the variable
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for (int i = 0; i < x.length; i++) System.out.print(x[i] + " "); } }
The program displays 0 0
What is the output of the following code? int [][] x = new int [3][ ]; x[0] = new int[3]; x[1] = new int[4]; x[2] = new int[5]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x[i].length; j++) count++; System.out.println(count);
12
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {0, 1, 2, 3, 4, 5}; xMethod(x, 5); } public static void xMethod(int[] x, int length) { for (int i = 0; i < length; i++) System.out.print(" " + x[i]); } }
The program displays 0 1 2 3 4
Analyze the following code: public class Test { public static void main(String[] args) { double[] x = {2.5, 3, 4}; for (double value: x) System.out.print(value + " "); } }
The program displays 2.5 3.0 4.0
Command Line Parameters are also known as?
Runtime Arguments
What is the output of the following code? public static void main(String args[ ]) { int matrix[ ][ ]; matrix = new int[3][4]; for(int p = 0; p < 3; p++) { for(int q = 0; q < 4; q++) { System.out.print(matrix[p][q] + " "); } System.out.println(); } System.out.println(); }
0 0 0 0 0 0 0 0 0 0 0 0
In the following code, what is the printout for list1? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list1.length; i++) System.out.print(list1[i] + " "); } }
0 1 2
What is the output of the following code? double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) { max = myList[i]; indexOfMax = i; } } System.out.println(indexOfMax);
1
What is output of the following code: public class Test { public static void main(String[] args) { int list[] = {1, 2, 3, 4, 5, 6}; for (int i = 1; i < list.length; i++) list[i] = list[i - 1]; for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); } }
1 1 1 1 1 1
What is the output of the following code? int[] myList = {1, 2, 3, 4, 5, 6}; for (int i = myList.length - 2; i >= 0; i--) { myList[i + 1] = myList[i]; } for (int e: myList) System.out.print(e + " ");
1 1 2 3 4 5
In the following code, what is the printout for list2? public class Test { public static void main(String[] args) { int[] list1 = {1, 2, 3}; int[] list2 = {1, 2, 3}; list2[2] = list1[0]; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + " "); } }
1 2 2
What is the output of the following code? int[] myList = {10, 9, 8, 7, 6, 5}; for(int i=1; i<myList.length; i++){ myList[i] = myList[i-1]; } for(int e: myList) System.out.print(e + " ");
10 10 10 10 10 10
What is the output of the following code? int [][] x = new int [3][4]; x[0] = new int[3]; x[1] = new int[4]; x[2] = new int[5]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x[i].length; j++) x[i][j] = count++; System.out.println(count);
12
What is the output of the following code? int [][] x = new int [3][ ]; x[0] = new int[3]; x[1] = new int[4]; x[2] = new int[5]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x[2].length; j++) count++; System.out.println(count);
15
What is a constructor?
A special method that is called just once when a new object is created
An array can be used in which of the following ways? a. As a parameter of a method b. As a return value of a method c. All of the above d. As a local variable
All of the above
A class can be thought of as a/an __________ for creating objects.
Alternative & Blueprint
Public methods are accessible:
Inside and outside of the class in which they are declared.
What is method overloading?
It is creating mutliple methods with the same name, but different parameter lists. It
If there is a statement after a "return" will the statement execute?
No
Private methods are accessible:
Only inside the class in which they are declared.
What is the output of the following code? int [][] x = new int [3][4]; x[0] = new int[3]; x[1] = new int[4]; x[2] = new int[5]; int count = 0; for(int i = 0; i < x.length; i++) for(int j = 0; j < x[1].length; j++) x[i][j] = count++; System.out.println(count);
Out of bounds Error
Analyze the following code: public class Test { public static void main(String[] args) { int[] a = new int[4]; a[1] = 1; a = new int[2]; System.out.println("a[1] is " + a[1]); } } What is the result?
The Program displays a[1] is 0
Analyze the following code: public class Test { public static void main(String[] args) { int[] oldList = {1, 2, 3, 4, 5}; reverse(oldList); for (int i = 0; i < oldList.length; i++) System.out.print(oldList[i] + " "); } public static void reverse(int[] list) { int[] newList = new int[list.length]; for (int i = 0; i < list.length; i++) newList[i] = list[i]; for (int i = 0; i < list.length; i++) list[i] = newList[list.length - 1 - i]; } }
The program displays 5 4 3 2 1.
Analyze the following code: public class Test { public static void main(String[] args) { int[] x = new int[5]; for (int i = 0; i < x.length; i++) x[i] = i; System.out.println(x[i]); } }
The program has a syntax error because i is not defined in the last statement in the main method.
Analyze the following code. int[] list = new int[5]; list = new int[6];
The code can compile and run fine. The second line assigns a new array to list.
What will be the relative values of array1 compared to array2 after the following code runs? int[ ] array1 = {1,2,3,4,5}; int[ ] array2 = array1; for(int i = 0; i < 5; i++) { array2[ i ] = array2[ i ] * 2; }
They are equal
For an array with variable x, x.length does not exist if x is null.
True
Static methods can be called within the same class and outside the class they are in as well.
True
The array index is limited to int type.
True
The array index of the first element in an array is 0.
True
True or False: An if-then-else statement can test ranges of conditions, whereas a switch statement runs based on matching a single value, usually an integer.
True
Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))?
[1, 20, 30, 40, 50]
You can tell that Math.abs(-57) is an example of a class method because:
abs(...) is called on a class (Math), rather than an object (like myMathObj)
An object is an instance of a/an:
class
What is the output of this code? String a = "comp"; String b = "uter"; String c = a.substring(1, 4) + b.substring(1, 4); System.out.println(c);
ompter
When you pass an array to a method, the method receives __________.
the reference of the array
In java, arrays are passed by _____ in methods?
value
In java, integers are passed by _____ in methods.
value