Arrays Flashcards

1
Q

Is Array an ordered list or unordered list?

A

Ordered

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

Can an array contain duplicates?

A

Yes

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

int[] arrays= new int[0];
Integer[] arrays = new Integer[0];
is arrays primitive?

A

No int is the primitive data type but all arrays are objects/

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

What are the valid datatype of array

A

a pic
* primitive data type
* interface
* abstract class
* concrete class

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

Which of the following will not compile?
public void a(String …name)
a(“Ala”);
a(new String[] {“Bret”});

A

Vargs arguement can take either an array or inidividual value

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

Which of the following will not compile?
public void a(String [] name)
a(“Ala”);
a(new String[] {“Bret”});

A

arrays only accepts array values not individual value.

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

Can an array datatype be null?

A

NO

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

Which of the following are legal and illegal?
String lion[] = new String[] {“lion”};
String tiger[] = new String[1]{“tiger”} ;
String bear[] = new String[] {};
String whale[] = new String[0]{};

A

Legal are lion and bear.
If you declare the size, you are not allowed {};

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

Can you do the following?
int intArray[2];

A

No it has to be the following :
int intArray[] = new int[4];

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

Can you do the following?
int array = new int[];
intarray[2] = new int;

A

does not compile, you need the size

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

Can you do the following?
intarray = new int[2.3];

A

won’t compile

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

Create an array with int datatype and of size 3.

A

int [] arrayname = new int [3];

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

Create an array and initialize with 3,4,5

A

````
int arrayname [] = {2,3,4}; //short cut

int [] numbers = new int [] {2,3,4}; //redundant

````

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

int [] helloworld = {1,2,3};
This type of approach is called what?

A

Anonymous array. You dont specify the size or the type.

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

int[] ids, types

A

creates two reference variable of type int[]

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

int ids[], type

A

one array and one int

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

List the five ways of declaring an array

A

int [] arrayName;
int[] arrayName;
int arrayName[];
int arrayName [];

~~~
int [] number = new int[4];

int number[];

int [] numbers, codes, scores;

int number[], codes, scores;

int number [], codes [], scores[]
````

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

What does it output? why?
````
String [] bugs = {“a”, “b”, “c”};
String [] copy = bugs;

System.out.println(bugs.equals(copy));
````

A

true. referenced to the same object.

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

String[] bugs = {“aa”, “mo”, “c”};
String[] z = {“aa”, “mo”, “c”};
System.out.println(bugs[0].equals(z[0]));

A

true

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

String[] birds = new String[6];
System.out.println(birds.length);

A

6 bc there are 6 slots

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

How to find the size of an array?

A

length.

not length();

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

What do you need to use Arrays?

A

java.util.Arrays;

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

What can you use to sort the Array?

A

import java.utils.Arrays;

Arrays.sort(arrayName);

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

If you have String what sorts first?

A
  1. Numbers (so 10 > 9)
  2. Uppercases
  3. lowercases
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What can you use to search within a array?

A

Java’s inbuild Arrays.binarchSearch();
However it needs to be sorted before you use them

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

Binary search rules.
What happens if it is found?
What happens if it is not found?
What happens if it unsorted array?

A

1.index of match
2. negative values
3. result is not predictible

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

Write three legal ways of writing main method including the Varargs way

A

public static void main(String[] args)
public static void main(String args[])
public static void main(String… args)

28
Q

Create a 2 multidimentional array.
Created a 2d and 3d array

A

int [] md [];
int [] [] md;
int md[] [];
````
int [] two[], three[] [] ;

````

29
Q

int [][] args = new int[4][];

A

you can actually do this
but you cannot do
in[][] args = new int [] [];
must provide the first = new int[4][];

30
Q

int [][] array = new int[2][];
array[0] =new int[4];
array[1] = new int[1];
array[0][0] =3;
System.out.println(array[0][0]);

A

valid
prints 3

31
Q

What to import to use ArrayList?

A

import java.util.ArrayList;

32
Q

Is Arraylist ordered? can it contain duplicates?

A

Ordered yes, can contain duplicates

33
Q

int array[] = new int[21];
System.out.println(array[-10]);

A

will compile successfullhy but will give ArrayIndexOutOfBoundsException at runtime.

34
Q

int array[] = new int[2];
System.out.println(array[1.2]);

A

Will not compile.

35
Q

Is null a valid datatype in String array?

A

Yes, so in abstract, interface and objects

36
Q

Provide some important properties of arrayList

A
  • Implements the inteface List
  • allows null values to be added to it
  • implements all the list operations
  • allows duplicate
  • maintains its insertion order
  • Iterator/ListIterator to iterate over the items
  • supports generics
37
Q

Create a arraylist.

A

ArrayList list = new ArrayList(3);
ArrayList lister = new ArrayList();

38
Q

Can you create a arraylist with type String?

A

You can with generics
ArrayList<String> liste = new ArrayList<String>();</String></String>

39
Q

Can you do the following?

ArrayList<String> list4 = new ArrayList<String>();
ArrayList<String> list5 = new ArrayList<>();
A

yes

40
Q

What does an arraylist implement?

A

List (interface)

41
Q

Can you do the following? Why not?

List<String> aa = new ArrayList<String>();
ArrayList<String> bb = new List<String>();
A

First yes
Second no.

List is an interface, you cannot instantiate a List

42
Q

Can you do this?
ArrayList list = new ArrayList();
list.add(“a”);
list.add(3);
list.add(Boolean.TRUE);

	System.out.println(list.get(0));
	System.out.println(list.get(1));
	System.out.println(list.get(2));
A

Yes bc no specification
had you
ArrayList<String> list = new ArrayList<String>();
list.add (3); it would be error</String></String>

43
Q

What does this print?

ArrayList list = new ArrayList();
list.add("a");
list.add("b");
System.out.println(list.remove("a"));
System.out.println(list.remove(0));
System.out.println(list.remove("a"));
A

true
b
false //cause you delete it

44
Q

ArrayList list = new ArrayList();
list.add(“a”);
list.remove(0);
System.out.println(list);

A

[]

45
Q
ArrayList<String> a = new ArrayList<String>();
		a.add("wow");
		a.remove(0);
		a.remove(0);
		System.out.println(a.remove(0));
A

it will compile but wont run.

46
Q

Using array list can you find the length?

A
ArrayList<String> stringArray = new ArrayList<>();
stringArray.size();

not size, not length.

47
Q

ArrayList list = new ArrayList();
list.add(“a”);
list.add(“b”);
list.clear();
System.out.println(list);

A

[]

48
Q

ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();

System.out.println(list.equals(list2));

A

true
An empty list is certainly the same elements in the same order

49
Q

What’s the difference between add() and set()?

A

set() -> without changing the size of the array list
add() -> changes size
you can do this
add(0, “one”);
add(0, “two”);

50
Q

What is autoboxing?

A

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

51
Q

int bad1 = Integer.parseInt(“a”);
Integer bad2 = Integer.valueOf(“123.45”);

A

has to be an int to parse.
throws numberFormatException

52
Q

How to convert between array and List?
Write an ArrayList and convert to array and vice versa.

A
List<String> list = new ArrayList<>();
list.add("hawk");
list.add("crow");
String[] stringArray = list.toArray(new String[0]);

String[] stringArray = {"list1", "list2"};
List<String> a = Arrays.asList(stringArray);
53
Q

What is the output of the following ?

		Boolean b = new Boolean(null);
		System.out.println(b);
		
		Boolean bb = new Boolean(false);
		System.out.println(bb);
		
		
		Boolean bbb = new Boolean(true);
		System.out.println(bbb);
A
false

false

true
54
Q
List<String> list = Arrays.asList("30", "8", "3A", "FF");
Collections.sort(list);
int x = Collections.binarySearch(list, "GF");
System.out.println("x found in " + x);
A

-5

so GF is at the last
since it is not found you get -1.
Since it is at last, index is -4
-1-4= -5

55
Q

Sort the following String array
“30”, “2”, “8”, “40”, “3A”, “FF”

A

2,30,3A, 40, 8, FF

56
Q

Which is more boarder? String or object?

A

Object

57
Q

Change String to Object

A

String [] a = {“aaa”};
Object[] b = a;

58
Q

CHange a object array to String array

A

String [] backToString = (String[] ) b;

59
Q

againStrings[0] = new StringBuilder();

A

DNC because it only allows String objects and StringBuilder is not a String.

60
Q

3: String[] strings = { “stringValue” };
4: Object[] objects = strings;
5: String[] againStrings = (String[]) objects;
6: againStrings[0] = new StringBuilder();
7: objects[0] = new StringBuilder();

A

StringBuilder object can clearly go in an Object[].
The problem is that we don’t actually have an Object[]. We have a String[] referred to from an Object[] variable.
At runtime, the code throws an ArrayStoreException.

61
Q

Can you do the following?

public static void main(String … original)
{
String … copy = original;

A

DNC
var args only be used in methods

62
Q

WAP to loop through the arrayList :
using ListIterator

A
63
Q

How many dimensions does the following a allow?
boolean [][] bools[], a[]?

A

three dimesion

64
Q

You are allowed to change call(String[] a) to call(String ..a)
and vice versa?

A

from array to varargs yes
Not from varargs to array. If you do it it will cause compile time error.

65
Q

String[] a = {“a”, “b”, “c”};
Arrays.binarySearch(“a”, a);

A

It will give out 0 bc it is already sorted so the answer is not undefined.

66
Q

Whats the output?
String[] a = {“a”, “b”, “c”};
String result = Arrays.binarySearch(“a”, a);
Sysout(result);

A

Does not compile. Cant store int in a String.