CH4 - Core APIs Flashcards

1
Q

String concatenation rules

A
  1. If both operands are numeric, + means numeric addition.
  2. If either operand is a String, + mean concatenation.
  3. The expression is evaluated left to right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

StringBuilder replace method

A

StringBuilder replace(int start, int end, String str)

start – The starting index (inclusive) of the substring to be replaced.
end – The ending index (exclusive) of the substring to be replaced.
str – The string that will replace the selected portion.

Returns the modified StringBuilder object.

✅ Inclusive start, exclusive end
✅ The replacement does not have to be the same length as the removed text.
✅ If str is “”, it removes the selected portion.
✅ replace() modifies the existing StringBuilder object (does not return a new instance).

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

In Java, an int and long can be implicitly converted to double.

A

In Java, an int and long can be implicitly converted to double.

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

System.out.println(numbers.substring(7, 7));

A

Results empty String

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

binarySearch() method

A

The binarySearch() method looks at where a
value would be inserted, which is before the second
element for Pippa. It then negates it and subtracts one

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

String intern() method

A

🔹 The String Pool is a part of the heap but is a special memory area managed by the JVM.

🔹 intern() moves the string to the pool only if it is not already there and returns the reference from the pool.

🔹 If the string already exists in the pool, intern() just returns the existing reference instead of creating a new object.

🔹 String literals (“Java”) are automatically stored in the pool at compile time, while new String(“Java”) creates a separate object in the heap.

🔹 s1 == s2 can be false because s1 (created with new String()) is in the heap, while s2 (returned by intern()) is in the pool.

🔹 Using intern() can save memory when dealing with many duplicate strings by ensuring only one copy is stored in the pool.

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

String format() method formatting symbols

A

%s - applies to any type, commonly String values

%d - applies to integer values like int and long

%f - applies to floating-point values like float and double

%n - inserts a line break using the system-dependent line separator

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

String format() method extra

A

-format() method relies on rounding rather than truncating

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

StringBuilder delete() method

A

-delete (int startIndex, int endIndex)

-deleteCharAt(int index)

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

var name = “Hello World”; var name2 = new String(“Hello World”).intern(); System.out.println(name == name2);

What is the output of this ?

A

True

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

BigInteger, BigDecimal

A

Both classes are immutable.
Both classes provide constants for the most common values like BigInteger.ZERO and BigDecimal.ONE

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

LocalDate

A

Contains just a date - no time and no time zone.

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

LocalTime

A

Contains just a time - no date and no time zone.

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

LocalDateTime

A

Contains both a date and time but no time zone.

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

ZonedDateTime

A

Contains a date, time and time zone.

Example: “Conference call at 9 a.m. EST”

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

Period Format

A

P1Y2M3D

Period
#years
#months
#days

Period.ofMonths(3) -> P3M

17
Q

Instant class

A

The Instant class in Java is part of the java.time package and represents a point in time, measured in nanoseconds from the epoch (January 1, 1970, 00:00:00 UTC). It is useful for working with timestamps in applications.

Key Concepts of Instant
-Immutable & Thread-Safe – Instant is immutable and thread-safe, making it ideal for concurrent applications.
-UTC-Based – It represents a timestamp independent of time zones.
-Nanosecond Precision – It stores time with nanosecond accuracy.

18
Q

int[][] scores = new int[5][]

Is it legal declaration?

A

Yes, it is legal to leave out the size for later dimensions of an array of arrays, the first one is required.

19
Q

random() method return type

A

it returns double

20
Q

round() method return type

A

returns an int when called with a float

returns a long when called with a double