3 Strings Flashcards

1
Q

Is the new operator required for Strings?

A

no, it’s optional

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

In a string, When does + concat and when does it perform addition?

A

If either operand involved in the + expression is a String, concatenation is used; otherwise, addition is used.

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

What are 13 methods of the String class?

A

cceeeilrssttt

charAt(),
concat(),
endsWith(),
 equals(),
 equalsIgnoreCase(),
 indexOf(),
length(),
 replace(), 
startsWith(),
 substring(), 
toLowerCase(), toUpperCase(),
 and
trim()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are 10 methods of the StringBuilder class?

A
append(),
 charAt(),
 delete(),
 deleteCharAt(),
indexOf(),
 insert(),
 length(),
 reverse(),
 substring(),
 toString().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between StringBuilder & StringBuffer?

A

StringBuffer is the same as StringBuilder except that it is thread safe

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

What does == do with two Strings?

A

Calling == on String objects will check whether they point to the same object in the pool

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

What does == do with two StringsBuilder references?

A

Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object

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

What does equals() do on String objects?

A

Calling equals() on String objects will check whether the sequence of characters is the same.

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

What does equals() do on StringBuilder objects?

A
Calling equals() on StringBuilder objects will check
whether they are pointing to the same object rather than looking at the values inside.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Is array size fixed on creation?

A

yes

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

What does Arrays.sort(myArray) do?

A

sorts an array

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

Arrays.binarySearch()

A

searches a sorted array and returns the
index of a match. If no match is found, it negates the position where the element would need to be inserted and subtracts 1.

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

T or F Methods that are passed varargs (…) can be used as

if a normal array was passed in.

A

T

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

Can an ArrayList contain primitives?

A

No, primitives must be in wrapper classes. Java will autobox parameters passed in to the proper wrapper type.

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

What is the method to sort an ArrayList?

A

Collections.sort()

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

x

A
A LocalDate contains just a date, a LocalTime contains just a time, and a LocalDateTime contains both a date and time. All three have private constructors and are created using LocalDate.now() or LocalDate.of() The Period class represents a number of days, months, or years to add or subtract from a LocalDate or LocalDateTime. DateTimeFormatter is used to output dates and times in the desired format. The date and
time classes are all immutable, which means the return value must be used.
17
Q

Is an array measured by size or length?

A

Arrays use length and it is a property not a method Example twoD.length is correct. Do NOT use twoD.length();

18
Q
What's the difference? 
String name = "Fluffy";
String name = new String("Fluffy");
A

The first one is in the string pool. second is a new object.

19
Q

What are the rules for concatenation vs addition?

A
  1. If both operands are numeric, + means numeric addition.
  2. If either operand is a String, + means concatenation.
  3. The expression is evaluated left to right.
    Now let’s look at some examples:
    System.out.println(1 + 2); // 3
    System.out.println(“a” + “b”); // ab
    System.out.println(“a” + “b” + 3); // ab3
    System.out.println(1 + 2 + “c”); // 3c
20
Q

What are the two method signatures for String’s replace() method?

A
  • It replaces ALL instances.
    System.out.println(“abcabc”.replace(‘a’, ‘A’)); // AbcAbc
    System.out.println(“abcabc”.replace(“a”, “A”)); // AbcAbc

It can replace 1 character or a whole sequence but it will replace ALL instances of a match.

animalsssss. replace(‘s’,’z’) –> animalzzzzz
animalsssss. replace(“ss”,”z”) –> animalzzzzs

21
Q

6 facts on Strings

A

These are the six facts on Strings:

  1. Literal strings within the same class in the same package represent references to the same String object.
  2. Literal strings within different classes in the same package represent references to the same String object.
  3. Literal strings within different classes in different packages likewise represent references to the same String object.
  4. Strings computed by constant expressions are computed at compile time and then treated as if they were literals.
  5. Strings computed at run time are newly created and therefore are distinct. (So line 4 prints false.)
  6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. (So line 5 prints true.)

When a variable and literal are concatenated at run time it is not the same object.

Consider following classes:
//In File Other.java
package other;
public class Other { public static String hello = "Hello"; }
//In File Test.java
package testPackage;
import other.*;
class Test{
   public static void main(String[] args){
      String hello = "Hello", lo = "lo";
      System.out.print((testPackage.Other.hello == hello) + " ");    //line 1
      System.out.print((other.Other.hello == hello) + " ");   //line 2
      System.out.print((hello == ("Hel"+"lo")) + " ");           //line 3
      System.out.print((hello == ("Hel"+lo)) + " ");              //line 4
      System.out.println(hello == ("Hel"+lo).intern());          //line 5
   }
}
class Other { static String hello = "Hello"; }
What will be the output of running class Test?
22
Q

Is String a final class?

A

Yes and so are these: StringBuffer and StringBuilder are also final. All Primitive wrappers are also final (i.e. Boolean, Integer, Byte etc).

23
Q

Can string be subclassed? Is this legal?

class MyString extends String{
   MyString(){ super(); }
}
A

No, String is a final class. It cannot be subclassed (extended)

24
Q

Why is this false?

“String”.replace(‘g’,’G’)==”String”.replace(‘g’,’G’);

A

replace creates a new String object

25
Q

Why is this true?

“String”.replace(‘g’,’g’)==”String”.replace(‘g’,’g’);

A

If there is no change, replace returns the same object