Ch 8: Strings Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

in java, what is a string of characters represented by

A

an object of the String type

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

characteristics of String objects are

A

have methods

can be passed to other methods (always as references) or returned from methods

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

literal strings are

A

represented by text in double quotes ex: “Twitter”

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

what operators can be used to concatenate a string with another string, number , or an object

A

+ and +=

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

what is concatenation

A

combining of two strings with + or +=

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

what are escape characters

A

\ is used as the escape character ex: \n, \t, ", \

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

can a literal string be empty

A

yes, it is called an empty string ex: String s = “”;

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

do literal strings objects need to be created

A

even though they act as string object, they do not have to be created

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

explain String city = “Boston”;

A

sets the reference city to a String object “Boston”. Boston is not the name of the variable (its name is city). Boston is its value

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

the String class has over a dozen constructors, true or false

A

true, but it is less common to use constructors for strings than for other types of objects

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

is there a difference between String str = “Foo Fighters”; and String str = new String(“Foo Fighters”);

A

yes, but a far as your programmed is concerned, the two declarations act identically

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

what is the field of the String type set as default

private String Hello;

A

null

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

explain String s1 = “”;

A

s1 is set to an empty string

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

explain String s2 = new String();

A

s2 is set to an empty string

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

what happens when a method is called for a reference that is equal to null

A

program throws a NullPointerException and quits

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

once a string object is constructed, it cannot be changed. t or f

A

t

17
Q

define immutable object

A

none of its methods can change the content of a String object

18
Q

what type of object is a string

A

immutable object

19
Q

what method allows you to get the value of a character at a given position in the string

A

charAt();

20
Q

how do you set or replace a single character in a string

A

String bandName = “Foo Fighters”;
char c = bandName.charAt(0);
bandName = Character.toLowerCase(c) + bandName.substring(1);

// bandName now refers to a new string
// with the value "foo fighters"
21
Q

what happens when there are no variables referring to an old string

A

the old string is “thrown away” due to java’s automatic garbage collector releases the memory from the old string and returns it to the free memory pool

22
Q

explain String s2 = s1;

A

two variables refer to the same string object. ex= s1 and s2 both point to one String object of “Foo Fighters”
this is assigning a reference

23
Q

s2 = new String(s1);

A

two variables refer to different String objects with the same contents. this is copying a reference