Arrays Flashcards

(74 cards)

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

only the wrapper can be not the primitive ones

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
What can you use to search within a array?
Java's inbuild Arrays.binarchSearch(); However it needs to be sorted before you use them
26
Binary search rules. What happens if it is found? What happens if it is not found? What happens if it unsorted array?
1.index of match 2. negative values 3. result is not predictible
27
Write three legal ways of writing main method including the Varargs way
public static void main(String[] args) public static void main(String args[]) public static void main(String... args)
28
Create a 2 multidimentional array. Created a 2d and 3d array
int [] md []; int [] [] md; int md[] []; ```` int [] two[], three[] [] ; ````
29
int [][] args = new int[4][];
you can actually do this but you cannot do in[][] args = new int [] []; must provide the first = new int[4][];
30
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]);
valid prints 3
31
What to import to use ArrayList?
import java.util.ArrayList;
32
Is Arraylist ordered? can it contain duplicates?
Ordered yes, can contain duplicates
33
int array[] = new int[21]; System.out.println(array[-10]);
will compile successfullhy but will give ArrayIndexOutOfBoundsException at runtime.
34
int array[] = new int[2]; System.out.println(array[1.2]);
Will not compile.
35
Is null a valid datatype in String array?
Yes, so in abstract, interface and objects
36
Provide some important properties of arrayList
* 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
Create a arraylist.
ArrayList list = new ArrayList(3); ArrayList lister = new ArrayList();
38
Can you create a arraylist with type String?
You can with generics ArrayList liste = new ArrayList();
39
Can you do the following? ``` ArrayList list4 = new ArrayList(); ArrayList list5 = new ArrayList<>(); ```
yes
40
What does an arraylist implement?
List (interface)
41
Can you do the following? Why not? ``` List aa = new ArrayList(); ArrayList bb = new List(); ```
First yes Second no. List is an interface, you cannot instantiate a List
42
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));
Yes bc no specification had you ArrayList list = new ArrayList(); list.add (3); it would be error
43
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")); ```
true b false //cause you delete it
44
ArrayList list = new ArrayList(); list.add("a"); list.remove(0); System.out.println(list);
[]
45
``` ArrayList a = new ArrayList(); a.add("wow"); a.remove(0); a.remove(0); System.out.println(a.remove(0)); ```
it will compile but wont run.
46
Using array list can you find the length?
``` ArrayList stringArray = new ArrayList<>(); stringArray.size(); ``` not size, not length.
47
ArrayList list = new ArrayList(); list.add("a"); list.add("b"); list.clear(); System.out.println(list);
[]
48
ArrayList list = new ArrayList(); ArrayList list2 = new ArrayList(); System.out.println(list.equals(list2));
true An empty list is certainly the same elements in the same order
49
What's the difference between add() and set()?
set() -> without changing the size of the array list add() -> changes size you can do this add(0, "one"); add(0, "two");
50
What is autoboxing?
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
int bad1 = Integer.parseInt("a"); Integer bad2 = Integer.valueOf("123.45");
has to be an int to parse. throws numberFormatException
52
How to convert between array and List? Write an ArrayList and convert to array and vice versa.
``` List list = new ArrayList<>(); list.add("hawk"); list.add("crow"); String[] stringArray = list.toArray(new String[0]); String[] stringArray = {"list1", "list2"}; List a = Arrays.asList(stringArray); ```
53
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); ```
``` false false true ```
54
``` List list = Arrays.asList("30", "8", "3A", "FF"); Collections.sort(list); int x = Collections.binarySearch(list, "GF"); System.out.println("x found in " + x); ```
-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
Sort the following String array "30", "2", "8", "40", "3A", "FF"
2,30,3A, 40, 8, FF
56
Which is more boarder? String or object?
Object
57
Change String to Object
String [] a = {"aaa"}; Object[] b = a;
58
CHange a object array to String array
String [] backToString = (String[] ) b;
59
againStrings[0] = new StringBuilder();
DNC because it only allows String objects and StringBuilder is not a String.
60
3: String[] strings = { "stringValue" }; 4: Object[] objects = strings; 5: String[] againStrings = (String[]) objects; 6: againStrings[0] = new StringBuilder(); 7: objects[0] = new StringBuilder();
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
Can you do the following? public static void main(String ... original) { String ... copy = original;
DNC var args only be used in methods
62
WAP to loop through the arrayList : using ListIterator
63
How many dimensions does the following a allow? boolean [][] bools[], a[]?
three dimesion
64
You are allowed to change call(String[] a) to call(String ..a) and vice versa?
from array to varargs yes Not from varargs to array. If you do it it will cause compile time error.
65
String[] a = {"a", "b", "c"}; Arrays.binarySearch("a", a);
It will give out 0 bc it is already sorted so the answer is not undefined.
66
Whats the output? String[] a = {"a", "b", "c"}; String result = Arrays.binarySearch("a", a); Sysout(result);
Does not compile. Cant store int in a String.
67
Can you use .parseInt or .valueOf with Character?
Nope.
68
What will this return? new Boolean(true)
true
69
What will this return? new Boolean(false)
false
70
What will this return? new Boolean("yes")
false
71
What will this return? new Boolean(null)
false
72
What will this return? new Boolean(True)
Cannot find symbol error
73
What will this return? new Boolean(False)
Cannot find symbol error
74
Create a wrapper class for Byte
Byte a = new Byte((byte)1);