Creating and using arrays Flashcards

1
Q

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( )

A

3.

- Each array object has a member variable named public final length of type ‘int’ that contains the size of the array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True/false:

All types of arrays are objects. i.e. intArr instanceof Object is true.

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which of the following statements will correctly create and initialize an array of Strings to non null elements:

  1. String[] sA = new String[1] { “aaa”};
  2. String[] sA = new String[] { “aaa”};
  3. String[] sA = new String[1] ; sA[0] = “aaa”;
  4. String[] sA = {new String( “aaa”)};
  5. String[] sA = { “aaa”};
A

2, 3, 4, 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Given the following code:
int i = 0 ;
int[] iA = {10, 20} ;
iA[i] = i = 30 ;

What is iA[0], iA[1] and i

A
iA[0] = 30
iA[1] = 20
i = 30
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following are benefits of an array over an ArrayList:

  1. It consumes less memory.
  2. Accessing an element in an array is faster than in ArrayList.
  3. You do not have to worry about thread safety.
  4. It implements Collection interface and can thus be passed where ever a Collection is required.
A

1, 2

- An ArrayList resize dynamically at run time as per the situation. An array cannot be resized once created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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]));

A

false, true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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]);

A

null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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

a, c, b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following will compile successfully:

  1. int eArr1[] = {10, 23, 10, 2};
  2. int[] eArr2 = new int[10];
  3. int[] eArr3 = new int[] {};
  4. int[] eArr4 = new int[10] {};
  5. int eArr5[] = new int[2] {10, 20};
A

1, 2, 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which of the following declarations are valid:

  1. String ejg1[][] = new String[1][2];
  2. String ejg2[][] = new String[][] { {}, {} };
  3. String ejg3[][] = new String[2][2];
  4. String ejg4[][] = new String[][]{{null},new String[]{“a”,”b”,”c”},{new String()}};
  5. String ejg5[][] = new String[][2];
  6. String ejg6[][] = new String[][]{“A”, “B”};
  7. String ejg7[][] = new String[]{{“A”}, {“B”}};
A

1, 2, 3, 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Identify the correct statements about ArrayList:

  1. Standard JDK provides no subclasses of ArrayList.
  2. You cannot store primitives in an ArrayList.
  3. It allows constant time access to all its elements.
  4. ArrayList cannot resize dynamically if you add more number of elements than its capacity.
  5. An ArrayList is backed by an array.
A

2, 3, 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following correctly declare a variable which can hold an array of 10 integers:

  1. int[ ] iA
  2. int[10] iA
  3. int iA[ ]
  4. Object[ ] iA
  5. Object[10] iA
A

1, 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Identify the correct statements about ArrayList:

  1. ArrayList extends java.util.AbstractList.
  2. It allows you to access its elements in random order.
  3. You must specify the class of objects you want to store in ArrayList when you declare a variable of type ArrayList.
  4. You can sort its elements using Collections.sort() method.
A

1, 2, 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly