Core Java APIs Flashcards

1
Q

What does 3 + “c” result in?

A

“3c” (a String)

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

Is String mutable or immutable?

A

immutable

Soooo you can concat it with + but there aren’t any String methods to change them.

HOWEVER, you can set it equal to something else to make it point to that instead. It won’t change the original object though, just create a new one

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

string pool

A

An area in the JVM where string literals are stored. The JVM can optimize the use of string literals by allowing only one instance of a string in the pool.

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

What is the difference between
String name = “Fluffy”;
and
String name = new String(“Fluffy”);

A

The first puts “Fluffy” in the string pool, and the second does not. The second one is less efficient, so do the first.

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

mystring.length()

A

Returns the length of mystring

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

mystring.charAt(i)

A

Returns the character at the i-th index of mystring

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

mystring.indexOf(c)

A

Finds the first index of mystring that matches the char or String c

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

mystring.indexOf(c, i)

A

Finds the first index of mystring that matches c starting from index i

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

What does indexOf() return if it can’t find a match?

A

-1

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

mystring.substring(x)

A

Returns the substring of mystring starting at index x until the end

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

mystring.substring(x,y)

A

Returns the substring of mystring starting at index x and ending at index y (meaning don’t actually include the character at index y)

String s = “funtimes”;
return s.substring(3,7);

will return “time”

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

mystring.toLowerCase()

A

Returns mystring as all lowercase

does not actually change the String

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

mystring.toUpperCase()

A

Returns mystring as all uppercase

does not actually change the String

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

mystring.equals(yourString)

A

Returns true if mystring and yourString contain exactly the same characters in the same order. Otherwise returns false.

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

mystring.equalsIgnoreCase(yourString)

A

Like mystring.equals() but ignores case

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

mystring.startsWith(str)

A

Returns true if mystring starts with str, otherwise returns false

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

mystring.endsWith(str)

A

Returns true if mystring ends with str, otherwise returns false

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

mystring.contains(str)

A

Returns true if mystring contains str, otherwise returns false

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

mystring.replace(str1, str2)

A

Returns mystring with str1 replaced by str2

Note: Can use char or String parameters

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

mystring.trim()

A

Returns mystring with white space removed from the beginning and end

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

StringBuilder

A

Basically the mutable version of String!

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

myStringBuilder.append(c)

A

Appends the character c to myStringBuilder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("animal");
StringBuilder sb3 = new StringBuilder(10);
A

Create empty StringBuilder object

Create StringBuilder object with “animal” as it’s value

Create StringBuilder object with space for ten characters

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

myStringBuilder.charAt()
myStringBuilder.indexOf()
myStringBuilder.length()
myStringBuilder.substring()

A

All the same as in the String class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
myStringBuilder.insert(n, str)
Inserts str at the n-th index of myStringBuilder
26
myStringBuilder.delete(i, j)
Deletes characters from myStringBuilder starting at index i and stopping before index j
27
myStringBuilder.deleteCharAt(i)
Deletes the character of myStringBuilder at the i-th index
28
myStringBuilder.reverse()
Reverses myStringBuilder
29
myStringBuilder.toString()
Converts myStringBuilder to a String
30
How to declare an array of 3 ints
int[] nums = new int[3]
31
How to delcare an array of 3 specific ints
int[] nums = new int[] {1, 2, 3} or shorter int[] nums = {1, 2, 3}
32
4 ways to put the [] when declaring an array
int[] myArray int [] myArray int myArray[] int myArray []
33
myArray.length
Returns the number of elements in myArray
34
What must you import to use the Arrays class?
import java.util.* or import java.util.Arrays
35
int[] nums = {6, 9, 1}; | Arrays.sort(nums);
Sorts nums in ascending order
36
What are the rules of numbers and letters when sorting an array?
Numbers before letters, and uppercase before lowercase
37
int[] numbers = {2,4,6,8}; System.out.println(Arrays.binarySearch(numbers, 2)); System.out.println(Arrays.binarySearch(numbers, 4)); System.out.println(Arrays.binarySearch(numbers, 1)); System.out.println(Arrays.binarySearch(numbers, 3)); System.out.println(Arrays.binarySearch(numbers, 9));
``` 0 1 -1 -2 -5 ``` Array must be sorted for this to work!
38
How to import ArrayList?
import java.util.ArrayList;
39
3 ways to declare an ArrayList (old way)
``` ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2); ```
40
``` ArrayList list = new ArrayList(); list.add("word"); ```
Adds "word" to the ArrayList
41
How to add an item at a specific index in an ArrayList called list
list.add(idx, item)
42
How to remove an item from an ArrayList called list
list.remove(item) //returns true if item is in the list, and false if not or list.remove(index) //returns the item at that index
43
How to set a specific index with an element in an ArrayList called list
E set(int index, E newElement) list.set(i, item) returns the element that got replaced. Keeps the ArrayList the same size
44
ArrayList list = new ArrayList(); list. isEmpty(); list. size();
Checks if list is empty (returns a boolean) Then determines number of elements in list (returns an int)
45
``` ArrayList list = new ArrayList(); list.clear() ```
Removes all elements of list No return
46
``` ArrayList list = new ArrayList(); list.contains(obj); ```
Checks whether obj is in list. Returns a boolean
47
``` ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); ``` list1.equals(list2)
Checks if list1 and list2 have the same elements in the same order
48
Converting String to a primitive (lets say int for this example)
Integer.parseInt("10");
49
Converting String to a wrapper class (lets say Integer for this example)
Integer.valueOf("10");
50
Autoboxing
The compiler automatically converts a primitive type into the matching wrapper class and vice versa.
51
Wrapper Class
Integer for int, Boolean for boolean, etc.
52
Convert from an ArrayList of Strings to an array of Strings ArrayList list = new ArrayList;
String[] stringArray = list.toArray(new String[0]); The "0" says to pick the same size as the list Without specifying "new String[0]" the array will be of type Object[]
53
Convert from an array of Strings to a List of Strings String[] array = {"hawk", "robin"};
List list = Arrays.asList(array); Once it is a List, you cannot change its size
54
How to sort an ArrayList ArrayList list = new ArrayList();
Collections.sort(list);
55
What import statement must you use to work with date and time?
import java.time.*
56
LocalDate
A class that deals with just the date, no time
57
LocalTime
A class that deals with just the time, no date
58
LocalDateTime
A class that deals with both date and time
59
If the date is June 20th, 2016 at 12:30 pm, what is the output? System.out.println(LocalDate.now()); System.out.println(LocalTime.now()); System.out.println(LocalDateTime.now());
2016-06-20 12:30:00.000 2016-06-20T12:30:00.000
60
Create a LocalDate object for June 20th, 2016
LocalDate date1 = LocalDate.of(2016, Month.JUNE, 20); or LocalDate date2 = LocalDate.of(2016, 6, 20);
61
Create a LocalTime object for 12:30:00 pm
LocalTime time = LocalTime.of(12, 30, 0); Can add one more parameter to specify nanoseconds, or one parameter less to not specify seconds
62
What is wrong with this? LocalDate d = new LocalDate();
You can't create a LocalDate, LocalTime, or LocalDateTime object in this fashion. You gotta use of() or now() or whateva
63
How to add 3 days, 3 weeks, 3 months, or 3 years to a LocalDate object called date
date = date.plusDays(3); date = date.plusWeeks(3); date = date.plusMonths(3); date = date.plusYears(3);
64
How to go back 3 days, 3 hours, or 3 seconds to a LocalDateTime object called dateTime
dateTime = dateTime.minusDays(3); dateTime = dateTime.minusHours(3); dateTime = dateTime.minusSeconds(3);
65
Period Object
An objet that can hold a certain period of time Period annually = Period.ofYears(1); // every 1 year Period quarterly = Period.ofMonths(3); // every 3 months Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks Period everyOtherDay = Period.ofDays(2); // every 2 days Period everyYearAndAWeek = Period.of(1, 0, 7); // every year and 7 days
66
How to add a Period called period to a LocalDate object called date
date = date.plus(period);
67
Can you chain Period methods?
no! Period wrong = Period.ofYears(1).ofWeeks(1); is the same as Period wrong = Period.ofYears(1); wrong = Period.ofWeeks(1);
68
What package is the class DateTimeFormatter in?
java.time.format
69
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT) Tell whether the following are legal and what they output 1) f.format(localDate); 2) f.format(localDateTime); 3) f.format(localTime);
1) Legal, shows whole object localDate 2) Legal, shows just the date of localDatetime 3) Throws runtime exception
70
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) Tell whether the following are legal and what they output 1) f.format(localDate); 2) f.format(localDateTime); 3) f.format(localTime);
1) Throws runtime exception 2) Legal shows whole localDateTime object 3) Throws runtime exception
71
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) Tell whether the following are legal and what they output 1) f.format(localDate); 2) f.format(localDateTime); 3) f.format(localTime);
1) Throws runtime exception 2) Legal shows only time part 3) Legal shows whole localTime object
72
What method do you use to create your own DateTimeFormatter pattern?
ofPattern()
73
When specifying a DateTimeFormatter pattern, what do the following mean? 1) M 2) MM 3) MMM 4) MMMM 5) d 6) dd 7) yy 8) yyyy 9) h 10) hh 11) m 12) mm
1) Represents month as a single digit (or two if 10-12) 2) Represents month as two digits 3) Represents the three letter abbreviation of the month 4) Represents the entire month name 5) Represents day with its default amount of digits 6) Represents day as a two digit number 7) Represents year as a two digit number 8) Represents year as a four digit number 9) Represents hour as its default amount of digits 10) Represents hour as a two digit number 11) Represents minutes as default amount of digits 12) Represents minutes as two digits
74
DateTimeFormatter f = DateTimeFormatter.ofPattern("MM dd yyyy"); LocalDate date = LocalDate.parse("01 02 2015", f); LocalTime time = LocalTime.parse("11:22");
Parses date using the formatter f Then parses time using the default formatter for time
75
Difference between calling equals() on a String and calling equals() on a StringBuilder
``` Calling equals() on String objects will check whether the sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside. ```
76
Difference between calling equals() on an array and calling equals() on an ArrayList
Calling equals() on an array checks object reference equality Calling equals() on an ArrayList checks to see if the ArrayLists have the same elements in the same order
77
Can you append a boolean value to a String?
yup