APIs Flashcards

1
Q

Does calling a method change a String?

ex.: “1234”.replace(‘1’, ‘9’);

A

No.

Strings are immutable. Calling methods does not change the object, it creates a new String object.

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

Can you extend String class?

A

No.

Not because it is immutable, but because it is final.

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

Can StringBuffer or StringBuilder be extended?

A

No.

They are final classes.

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

Can Wrapper classes be extended?

A

No.

They are final classes.

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

Does the String class have a method called reverse?

A

No.

But StringBuilder and StringBuffer do.

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

How do you represent just a date with no time zone information?

A

java.time.LocalDate

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

What are common classes of the java.time package?

A

LocalDate, LocalTime, LocalDateTime, Instant, Period, Duration are part of this package.

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

How do you get time zone information?

A

Use ZonedDateTime

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

How do you format Date objects?

A

java.time.format.DateTimeFormatter

LocalDate d1 = LocalDate.parse(“2015-01-01”, DateTimeFormatter.ISO_LOCAL_DATE);

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

If using ArrayList API, what happens if the fromIndex and toIndex arguments of a subList ( ) method are equal?

A

The subList returns an empty list.

List foo - new ArrayList( );
foo.add("f");
foo.add("o");
foo.add("o");
List bar = new ArrayList(  foo.subList(1, 1) );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

T/F: Literal strings within the same class in the same package represent references to the same String object.

A

True.

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

T/F: Literal strings within different classes in the same package represent references to the same String object.

A

True.

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

When do Strings that are computed by constant expressions actually get computed?

A

At compile time.

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

How are Strings computed by constant expressions treated?

A

When computed at compile time, they are treated as if they were literals.

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

What does the java string intern( ) method do?

A

It returns the interned (canonical representation) of string.

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

What is the result of explicitly interning a computed string?

A

It is the same string as any pre-existing literal string with the same contents.

17
Q

T/F: Strings computed at run time are the same as any pre-existing String object with the same contents.

A

False.

Strings computed at run time are newly created and therefore are distinct.

18
Q

What will the following code print?

String abc = "";
abc.concat("abc");
abc.concat("def");
System.out.print(abc);
A

It will print an empty string.

19
Q

For the String class, what does the following return?

String substring(int beginIndex)

A

This returns a new string that is a substring of this string.

20
Q

For the String class, what does the following return?

String substring(int beginIndex, int endIndex)

A

Returns a new string that is a substring of this string.

21
Q

For the String class, what does the following return?

int indexOf(int ch)

A

Returns the index within this string of the first occurrence of the specified character.

22
Q

For the String class, what does the following return?

int indexOf(int ch, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

23
Q

For the String class, what does the following return?

int indexOf(String str, int fromIndex)

A

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

24
Q

For the String class, what does the following return?

int indexOf(String str)

A
Returns the index within this string of the first occurrence of the specified substring.
int indexOf(String str, int fromIndex)
25
Can String be extended?
No. It is final.
26
Does StringBuilder extend String?
No, it extends Object.
27
What does the following code return? "hamburger".substring(4, 8);
urge
28
What does the following code return? "smiles".substring(1, 5)
mile
29
What does the following code return? "unhappy".substring(2)
happy
30
What does the following code return? "emptiness".substring(9)
"" (an empty string)
31
What does the following code return? "Harbison".substring(3)
bison
32
What will the third line print? String s = "fancy-"; s.append("pants"); System.out.println(s);
It won't compile. There is no `append( )` method for String.
33
Is the following valid syntax? String String = "String";
Oddly enough, yes. There are no restrictions on naming variables the same as classes.