Arrays & String Handling Flashcards

1
Q

Create array of 12 integers

A

int month_days[] = new int[12];

or

int month_days[] = {31,28,25,43,23};

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

Can you have multidimensional array where the sizes of second dimension are unequal?

A

yes, it’s an array of arrays, so you can define the vertical dimension per each row.

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

T/F: Java implements strings as an object type String, whereas other languages implement them as character arrays?

A

True

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

T/F: String objects cannot be changed.

A

True, you cannot change the characters of a String.

Each time you create an altered version, a new String object is created.

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

How do you create a modifiable String?

A

use StringBuffer or StringBuilder

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

Create array of characters
Create a string from 3rd letter that lasts 3 characters

A

char chars[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’};
String s = new String(chars, 2,3);

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

Explain how string concatenation works with other data types

int age = 34
String s = “He is “ + age + “years old”

A

int value of age is automatically converted to string representation within the String object

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

Explain what happens what you do println of a newly created class

Box b = new Box(2,3,4)
System.out.println(b)

A

automatically calls b.toString() method.

However, this is seldom sufficient and you likely want to override toString() in the new class to define it how you like.

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

Explain:

String s1 = “Hello”
String s2 = new String(s1)

System.out.println(s1.equals(s2))
System.out.println(s1==s2)

Output:
true
false

A

equals method compares the characters within the String object.

== checks if references the same instance

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

String arr[] = {“a”, “asdf”, “as”}

Return the length of arr

A

arr.length

which is 3

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

What does compareTo do to Strings? What are the output values?

String a = “AAAAAAA”
String b = “BBBBBBB”

A

Returns if before or after in the dictionary

<0 is earlier
>0 is later
0 is equal

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

What does indexOf do?

A

Finds first occurrence of that character in the string

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

explain String method replace()

A

replaces a character or character sequence with another

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

explain String method trim()

A

removes leading and trailing whitespace

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

what does valueOf() do?

A

returns the human-readable String value of that input (e.g. integer to string)

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

What method would you use to concatenate strings with a delimiter between?

A

join()

String a = String.join(“, “, “alpha”, “beta”, “gamma”)

17
Q

Will StringBuffer automatically grow to fit the size object you need?

A

yes

18
Q

What does it mean an array is dynamically allocated?

A

Just declaring an array does not create it.

You need to allocate memory space by defining it’s size.

19
Q

T/F: an array is an object?

A

True

20
Q

T/F: When creating an array, you need to define the size of all dimensions, but you can leave the last dimension open and flexible

A

True

int array[][] = new int[5][]

21
Q

What does it mean that arrayA = arrayB copies the reference to arrayB and not the actual values?

A

When copying or comparing arrays, it uses references not the underlying data.

It means arrayA has pointers to arrayB, so if you update arrayB it will also update in arrayA.

22
Q

T/F: Strings also compare the references not the underlying content?

A

True. I guess this is how all objects work.

Could have one object ID = 9: “cat”
and another new String(“cat”) at ID=10

Same underlying, but not same in ==

To make it compare the underlying use .equals method:
a.equals(b)

23
Q

Write for loop that sums all elements in an array

A

for (int tempint : array)

24
Q

When camparing strings, explain difference between == and equals() method

A

== asks if they refer to the same object

equals() asks if contents of the strings are the same

25
Q

What are command line arguments?

A

passed to the program at run-time, enter in command line after executing a program