Core APIs Flashcards

1
Q

STRING: int length()

A

Returns the length of the string

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

STRING: char charAt(int index)

A

Returns the character at the specified index in the string.

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

STRING: int indexOf(int ch, int fromIndex)

A

Returns the index of the first occurrence of the specified character, starting the search at fromIndex.

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

STRING: int indexOf(String str)

A

Returns the index of the first occurrence of the specified substring.

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

STRING: String substring(int beginIndex)

A

Returns a new string that is a substring of this string, starting from beginIndex to the end.

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

STRING: int indexOf(String str, int fromIndex)

A

Returns the index of the first occurrence of the specified substring, starting the search at fromIndex.

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

STRING: String substring(int beginIndex, int endIndex)

A

Returns a new string that is a substring of this string, starting from beginIndex and ending at endIndex - 1.

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

STRING: String toLowerCase()

A

Converts all characters in the string to lowercase.

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

STRING: String toUpperCase()

A

Converts all characters in the string to uppercase.

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

STRING: boolean equals(Object obj)

A

Compares this string to the specified object for equality.

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

STRING: boolean startsWith(String prefix)

A

Tests if this string starts with the specified prefix.

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

STRING: boolean equalsIgnoreCase(String str)

A

Compares this string to another string, ignoring case considerations.

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

STRING: boolean endsWith(String suffix)

A

Tests if this string ends with the specified suffix.

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

STRING: boolean contains(CharSequence charSeq)

A

Returns true if and only if this string contains the specified sequence of characters.

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

STRING: String replace(char oldChar, char newChar)

A

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

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

STRING: String replace(CharSequence target, CharSequence replacement)

A

Replaces each substring of this string that matches the literal target sequence with the specified replacement sequence.

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

How to return the length of the string?

A

int length()

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

How to get the character at a specific index in the string?

A

char charAt(int index)

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

How to find the index of a character, starting the search from a specific index?

A

int indexOf(int ch, int fromIndex)

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

How to find the index of the first occurrence of a substring?

A

int indexOf(String str)

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

How to find the index of a substring, starting the search from a specific index?

A

int indexOf(String str, int fromIndex)

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

How to get a substring from a specific index to the end of the string?

A

String substring(int beginIndex)

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

How to get a substring between two specified indices?

A

String substring(int beginIndex, int endIndex)

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

How to convert all characters in the string to lowercase?

A

String toLowerCase()

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

How to convert all characters in the string to uppercase?

A

String toUpperCase()

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

How to compare this string to another object for equality?

A

boolean equals(Object obj)

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

How to compare this string to another string, ignoring case?

A

boolean equalsIgnoreCase(String str)

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

How to check if a string starts with a specific prefix?

A

boolean startsWith(String prefix)

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

How to check if a string ends with a specific suffix?

A

boolean endsWith(String suffix)

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

How to check if a string contains a specific sequence of characters?

A

boolean contains(CharSequence charSeq)

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

How to replace all occurrences of a character with another character in a string?

A

String replace(char oldChar, char newChar)

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

How to replace all occurrences of a target sequence with a replacement sequence in a string?

A

String replace(CharSequence target, CharSequence replacement)

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

How to remove whitespace from both ends of a string?

A

String strip()

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

How to remove whitespace from the end of a string?

A

String stripTrailing()

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

How to remove whitespace from the beginning of a string?

A

String stripLeading()

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

How to add a specified number of spaces at the beginning of a string?

A

String indent(int numberSpaces)

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

How to remove whitespace from both ends of a string (alternative to strip())?

A

String trim()

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

How to remove leading whitespace from every line in a string?

A

String stripIndent()

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

How to return a string with escape sequences translated?

A

String translateEscapes()

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

How to check if a string is empty (length of 0)?

A

boolean isEmpty()

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

How to check if a string is empty or contains only whitespace characters?

A

boolean isBlank()

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

STRING: String stripLeading()

A

Removes whitespace from the beginning of the string.

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

How to create a formatted string using a format string and arguments?

A

static String format(String format, Object args…)

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

How to create a formatted string using a specific locale, format string, and arguments?

A

static String format(Locale loc, String format, Object args…)

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

STRING: String stripTrailing()

A

Removes whitespace from the end of the string.

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

How to create a formatted string using this string as the format string?

A

String formatted(Object args…)

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

STRING: String strip()

A

Removes whitespace from both the beginning and end of the string.

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

STRING: String trim()

A

Removes whitespace from both the beginning and end of the string (similar to strip()).

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

STRING: String indent(int numberSpaces)

A

Adds the specified number of spaces to the beginning of each line in the string. Can use negative numbers to remove spaces.

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

STRING: String translateEscapes()

A

Translates escape sequences in the string (like \n, \t) into their corresponding characters.

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

STRING: String stripIndent()

A

Removes any leading whitespace from every line in the string.

48
Q

STRING: boolean isEmpty()

A

Returns true if the string has a length of 0, otherwise false.

49
Q

STRING: boolean isBlank()

A

Returns true if the string is empty or contains only whitespace characters, otherwise false.

50
Q

STRING: static String format(String format, Object args…)

A

Creates a formatted string using a format string and arguments.

50
Q

STRING: static String format(Locale loc, String format, Object args…)

A

Creates a formatted string using a specific locale, format string, and arguments.

51
Q

STRING: String formatted(Object args…)

A

Creates a formatted string using this string as the format string and the provided arguments.

52
Q

STRING BUILDER: append()

A

Adds characters to the end of the StringBuilder.

53
Q

STRING BUILDER: insert(int offset, String str)

A

Inserts a string at the specified position in the StringBuilder. Remember it changes indexes!

54
Q

STRING BUILDER: delete(int startIndex, int endIndex)

A

Removes the characters in a substring of this StringBuilder. The substring begins at the specified startIndex and extends to the character at index endIndex - 1. It supports indexes larger than its string length. Remember it changes indexes!

55
Q

STRING BUILDER: deleteCharAt(int index)

A

Removes the character at the specified position in this StringBuilder. It supports indexes larger than its string length. Remember it changes indexes!

56
Q

STRING BUILDER: replace(int startIndex, int endIndex, String newString)

A

Replaces the characters in a substring of this StringBuilder with characters in the specified String.

57
Q

STRING BUILDER: reverse()

A

Causes this character sequence to be replaced by the reverse of the sequence.

58
Q

How can we add characters to the end of a StringBuilder?

A

append()

59
Q

How can we insert a string at a specific position in a StringBuilder?

A

insert(int offset, String str)

60
Q

How can we remove a range of characters from a StringBuilder?

A

delete(int startIndex, int endIndex)

61
Q

How can we remove a single character at a specific index from a StringBuilder?

A

deleteCharAt(int index)

62
Q

How can we replace a range of characters in a StringBuilder with a new string?

A

replace(int startIndex, int endIndex, String newString)

63
Q

How can we reverse the entire sequence of characters in a StringBuilder?

A

reverse()

64
Q

How are strings interned?

A

On compile time automatically when in format of string literals. If in format of new String(), then at runtime. Also, can be manually by calling intern() method on string.

65
Q

How do we check for string reference equality?

A

With ==, but only on interned strings

65
Q

How do we check for string equality?

A

With equals()

66
Q

Can arrays and primitives be initialized in same line?

A

Yes. eg. int ids[] = {}, types = 7;

67
Q

What do arrays actually contain?

A

They contain references to objects and primitives

68
Q

How do we initialize new string array?

A

String names[] = new String[2];

69
Q

How can we initialize two dimensional array?

A

String[] names[] = new String[][]{new String[2]};
String names[][] = new String[][]{new String[2]};
String[][] names = new String[][]{new String[2]};

70
Q

How do we get size of array?

A

length. Without parentheses.

71
Q

How do we order arrays?

A

Arrays.sort(…)

72
Q

How do se search arrays?

A

Arrays.binarySearch(haystack, needle)
But array must be sorted for it to work!

73
Q

What are rules for Arrays.binarySearch?

A
  • array must be sorted. if not sorted, result will be random
  • if is sorted and needle is found, result will be positive
  • if is sorted and needle is not found, result is -(index-1)
74
Q

How do we compare arrays?

A

Arrays.compare(…)

75
Q

What are rules for arrays comparing?

A

generally, if first array is “superior”, result is positive:
- if all is identical, result is 0
- if all is the same, but second array is bigger, result is negative
- if all is the same, but first is bigger, result is positive
- if first difference in first array is smaller, then result is negative
- if first difference in first array is bigger, result is positive
- only two same types of array can be compared, or else exception is thrown

76
Q

What are rules for defining what values are bigger or smaller in array comparing?

A
  1. capital letters
  2. regular letters - ab, ba
  3. numbers - numbers in order
  4. null
77
Q

MATH: Math.random()

A

Returns a double value between 0.0 (inclusive) and 1.0 (exclusive)

78
Q

How can we generate a random number between 0 and 1?

A

double result = Math.random();

79
Q

MATH: Math.pow(double base, double exponent)

A

Returns a double value that is the first argument raised to the power of the second argument

80
Q

How can we calculate a number raised to a power?

A

double result = Math.pow(double base, double exponent);

81
Q

MATH: Math.floor(double num)

A

Returns a double value that is the largest integer less than or equal to the argument

82
Q

How can we round down a decimal number to the nearest integer?

A

double result = Math.floor(double num);

83
Q

MATH: Math.ceil(double num)

A

Returns a double value that is the smallest integer greater than or equal to the argument

84
Q

How can we round a float to the nearest integer?

A

int result = Math.round(float num);

84
Q

How can we round up a decimal number to the nearest integer?

A

double result = Math.ceil(double num);

85
Q

MATH: Math.round(float num)

A

Returns an int value that is the argument rounded to the nearest integer

86
Q

MATH: Math.round(double num)

A

Returns a long value that is the argument rounded to the nearest integer

87
Q

How can we round a double to the nearest integer?

A

long result = Math.round(double num);

88
Q

MATH: Math.min(long a, long b)

A

Returns the long value that is the smaller of the two arguments

89
Q

MATH: Math.min(double a, double b)

A

Returns the double value that is the smaller of the two arguments

89
Q

How can we find the minimum of two long integers?

A

long result = Math.min(long a, long b);

90
Q

MATH: Math.min(int a, int b)

A

Returns the int value that is the smaller of the two arguments

91
Q

How can we find the minimum of two integers?

A

int result = Math.min(int a, int b);

92
Q

MATH: Math.min(float a, float b)

A

Returns the float value that is the smaller of the two arguments

92
Q

DATE TIME: How can we find the minimum of two double precision numbers?

A

double result = Math.min(double a, double b);

93
Q

How can we find the minimum of two float numbers?

A

float result = Math.min(float a, float b);

94
Q

DATE TIME: Are LocalDateTime and similar classes mutable?

A

No

95
Q

DATE TIME: Can LocalDateTime and similar classes be instantiated?

A

No, only by factory methods

96
Q

DATE TIME: How can you get current date?

A

LocalDate.now()

97
Q

DATE TIME: How can you get specific date?

A

LocalDate.of(int year, int month, int dayOfMonth)

97
Q

DATE TIME: How can you get current date and time?

A

LocalDateTime.now()

97
Q

DATE TIME: How can you get current time?

A

LocalTime.now()

98
Q

DATE TIME: How can you get current zoned date and time?

A

ZonedLocalDateTime.now()

99
Q

DATE TIME: How can you get specific zoned date and time?

A

Same as LocalDateTime, but with ZoneId at the end

99
Q

DATE TIME: How can you get specific time?

A

LocalTime.of(int hour, int minute, int second)

100
Q

DATE TIME: What do you need to watch out when using “of()” method?

A

Not to mix time an and date values where they don’t belong:
var date = LocalDate.of(2022, Month.JANUARY, 42);

101
Q

DATE TIME: How can you get specific date and time?

A

LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)

102
Q

DATE TIME: How can you check if some date or time is before other date or time?

A

isBefore() method

103
Q

DATE TIME: How can you define a period?

A

Period.ofYears(), Period.ofMonths(), Period.ofWeeks(), Period.ofDays()
Period.of(int years, int months, int days)

104
Q

DATE TIME: Where can you use Period?

A

On LocalDate and LocalDateTime classes

105
Q

DATE TIME: How can you calculate or add period?

A

date.plus(period)

106
Q

DATE TIME: How can you define duration?

A

Duration.ofDays() … Duration.ofNanos()
Duration.of(int number, ChronoUnit)

107
Q

DATE TIME: What is Duration used for?

A

To quickly calculate duration between two temporals

108
Q

DATE TIME: How can you get difference between two temporals?

A

ChronoUnit.DAYS.between(Temporal first, Temporal second, ChronoUnit)
Duration.between(Temporal first, Temporal second, ChronoUnit)

109
Q

DATE TIME: How can you add or remove temporals from another temporal?

A

dateTime.plus(int number, ChronoUnit)
dateTime.minus(int number, ChronoUnit)

110
Q

DATE TIME: How can you add to Duration?

A

duration.plusDays(int number)

duration.plusSeconds(int number)

111
Q

DATE TIME: How can you remove from Duration?

A

duration.minusDays(int number)

duration.minusSeconds(int number)

112
Q

DATE TIME: How can you truncate time?

A

time.truncatedTo(ChronoUnit)

113
Q

DATE TIME: What is the difference between LocalDateTime.now() and Instant.now()?

A

Instant.now() contains zone information

114
Q

DATE TIME: What is alternative to Instant.now()?

A

ZonedLocalDateTime.toInstant()

115
Q

DATE TIME: How can you use Instant()?

A

To calculate duration: Duration.between(Instant one, Instant two)