Question | Answer |
An array type can be? | Primitives Interfaces Abstract class Concrete class |
Once allocated, all the array elements store... | their default value. object = null int = 0 double = 0.0 char = /u0000 boolean = false |
Initializing an one-dimensional array with for loop | int anArray = new int[3]; for(int i = 0; i < anArray.length; i++){ anArray[i] = i+1; } |
Initializing an array | int anArray [] = new int[2]; anArray[0] = 1; anArray[1] = 2; |
Initializing an multi-dimensional array with nested for loop | int[] multArray[] = new int[5][6]; for (int i = 0; i < multArray.length; i++){ for (int j = 0; multArray[i].length; j++){ multArray[i][j] = i+j; } } |
Initializing an multidimensional array | int[][] multArray = int[2][3]; multArray[0][0] = 10; multArray[0][1] = 11; multArray[0][2] = 13; multArray[1][0] = 12; multArray[1][1] = 13; multArray[1][2] = 115; |
What happens when you try to access a nonexistent array index position? | Code will throw a runtime exception ArrayIndexOutOfBoundsException |
Combining array declaration, allocation, and initialization | one-dimensional array int[] anArray = new int[]{0,1,3,4}; multidimensional array int[][] anArray = new int[] { {0,1,2}, {3,4,5} }; |
What happens when you access: multArr[1][1];? int[][] multArr = new int{{1,2},null,{2,5,8}}; | null pointer exception multArr[1][1] doesn't exist |
What values do elements of Interface type array store? | either null or object that implement the interface type |
What values do elements of Abstract class type array store? | its element are either null or objects of concrete classes that extend the relevant abstract class |
What values do elements of object type arrays store? | Because all classes extend the class java.lang.object, elements of an array whose type is object can refer to any object. And null is also a valid element ! |
Want to create your own Flashcards for free with GoConqr? Learn more.