Chapter 3: Core Java APIs Flashcards
What operation is performed?
“string” + 10
concatenation
What is the result of this expression?
10 + 3 + “” + 3 + 5
1335
Define immutability
An object is immutable if it cannot be change after being created
Which of the following are immutable?
int
String
Integer
String and Integer
What strings go in the string pool?
String literal values that appear in the program
How do you explicitly create a string that doesn’t go in the string pool?
new String(“string”)
T or F: If s is a String, the following line would convert all characters in s to upper case
s.toUpperCase()
False. Strings are immutable. toUpperCase() returns a new string and that reference needs to be stored.
s = s.toUpperCase()
String e = “example”;
String s = e.substring(1,4)
What is the value of s?
“xam”. index 1 is included (x) and index 4 is excluded (p)
String s1 = “test”;
String s2 = “test”;
boolean b = s1 == s2;
What is the value of b?
True. Both strings are literals, so they point to the same object in the string pool
String s1 = "test"; String s2 = new String("test");
boolean b = s1 == s2;
What is the value of b?
False. s1 is a literal and is stored in the string pool, s2 is not. Therefore, they are referencing different objects.
String s1 = "test"; String s2 = new String("test");
boolean b = s1.equals(s2);
What is the value of b?
True. The contents of both strings are the same despite them being different objects.
What can you use if you want a mutable representation of a string?
StringBuilder
Why is StringBuilder more efficient in cases with lots of concatenation than String?
StringBuilder doesn’t store interim String values whereas every time String is changed, a new object is created
T or F. This is a valid array declaration:
int arr[];
True. The brackets can come before or after the name of the array and there can be a space as well.
T or F. .length can be used to find how many of an array’s slots are full.
False. .length is the total number of spaces allocated to the array