Creating and using arrays Flashcards
Given the following declaration: int[] intArr;
select the correct way to get the number of elements in the array:
1. intArr[ ].length( )
2. intArr.length( )
3. intArr.length
4. intArr[ ].size( )
5. intArr.size( )
3.
- Each array object has a member variable named public final length of type ‘int’ that contains the size of the array.
True/false:
All types of arrays are objects. i.e. intArr instanceof Object is true.
True.
Which of the following statements will correctly create and initialize an array of Strings to non null elements:
- String[] sA = new String[1] { “aaa”};
- String[] sA = new String[] { “aaa”};
- String[] sA = new String[1] ; sA[0] = “aaa”;
- String[] sA = {new String( “aaa”)};
- String[] sA = { “aaa”};
2, 3, 4, 5
Given the following code:
int i = 0 ;
int[] iA = {10, 20} ;
iA[i] = i = 30 ;
What is iA[0], iA[1] and i
iA[0] = 30 iA[1] = 20 i = 30
Which of the following are benefits of an array over an ArrayList:
- It consumes less memory.
- Accessing an element in an array is faster than in ArrayList.
- You do not have to worry about thread safety.
- It implements Collection interface and can thus be passed where ever a Collection is required.
1, 2
- An ArrayList resize dynamically at run time as per the situation. An array cannot be resized once created.
Consider the following, what will it print:
boolean[] b1 = new boolean[2];
boolean[] b2 = {true , false};
System.out.println
( “” + (b1[0] == b2[0]) + “, “+ (b1[1] == b2[1]));
false, true
Consider the following code, what will it print:
String[][][] arr=
{{ { “a”, “b” , “c”}, { “d”, “e”, null } },
{ {“x”}, null },{{“y”}},{ { “z”,”p”}, {} }};
System.out.println(arr[0][1][2]);
null
Consider the following code, what will it print: List s1 = new ArrayList( ); s1.add("a"); s1.add("b"); s1.add(1, "c"); List s2 = new ArrayList( s1.subList(1, 1) ); s1.addAll(s2); System.out.println(s1);
a, c, b
Which of the following will compile successfully:
- int eArr1[] = {10, 23, 10, 2};
- int[] eArr2 = new int[10];
- int[] eArr3 = new int[] {};
- int[] eArr4 = new int[10] {};
- int eArr5[] = new int[2] {10, 20};
1, 2, 3
Which of the following declarations are valid:
- String ejg1[][] = new String[1][2];
- String ejg2[][] = new String[][] { {}, {} };
- String ejg3[][] = new String[2][2];
- String ejg4[][] = new String[][]{{null},new String[]{“a”,”b”,”c”},{new String()}};
- String ejg5[][] = new String[][2];
- String ejg6[][] = new String[][]{“A”, “B”};
- String ejg7[][] = new String[]{{“A”}, {“B”}};
1, 2, 3, 4
Identify the correct statements about ArrayList:
- Standard JDK provides no subclasses of ArrayList.
- You cannot store primitives in an ArrayList.
- It allows constant time access to all its elements.
- ArrayList cannot resize dynamically if you add more number of elements than its capacity.
- An ArrayList is backed by an array.
2, 3, 5
Which of the following correctly declare a variable which can hold an array of 10 integers:
- int[ ] iA
- int[10] iA
- int iA[ ]
- Object[ ] iA
- Object[10] iA
1, 3
Identify the correct statements about ArrayList:
- ArrayList extends java.util.AbstractList.
- It allows you to access its elements in random order.
- You must specify the class of objects you want to store in ArrayList when you declare a variable of type ArrayList.
- You can sort its elements using Collections.sort() method.
1, 2, 5