String Flashcards

1
Q

What are the three rules for concatenation?

A
  1. If both operands are numeric, +1 means numeric addition
  2. If either operand is a String, +1 means 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

What will the following print:
````

System.out.println(1+2);
System.out.pirntln(“a” + “b”);

System.out.println(“a”+”b” +3);

System.out.println(1+2+”c”);

````

A

3
ab
ab3
3c

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

What does the following output?

````
String s = “1”;
s += “2”;
s += 3;
System.out.println(s);

````

A

123

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

What’s the output?
int a =9;
String c = “hello”;
String b = c + a + 1;
System.out.println(b);

A

hello91

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

What’s the output?
int a =9;
String b = a + 1;
System.out.println(b);

A

will not compile

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

What’s the output?
int a =9;
String b = “”+ a + 1;
System.out.println(b);

A

91

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

If an String object is created, can it be allowed to change?

A

No.
Strings are immutable which means they cannot be changed.

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

Are String mutable or immutable?

A

Cannot be changed, Hence IMMUTABLE

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

What is the output of the following?

````
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);

~~~

A

12

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

Can you do this? What’s the difference?
````
String name = “Fluffy”;
String name = new String(“Fluffy”);

````

A

The string pool is like a cache. If you do this:

String s = “Fluffy”;
String p = “Fluffy”;
then the Java compiler is smart enough to make just one String object, and s and p will both be referring to that same String object. If you do this:

String s = new String(“Fluffy”);
then there will be one String object in the pool, the one that represents the literal “Fluffy”, and there will be a separate String object, not in the pool, that contains a copy of the content of the pooled object. Since String is immutable in Java, you’re not gaining anything by doing this; calling new String(“literal”) never makes sense in Java and is unnecessarily inefficient.

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

What method length() does ?

````
String string = “0123456789”;
System.out.println(string.length());

````

A

The method length() returns the number of characters in the String.

````
System.out.println(string.length()); // 10

````

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

String a=”hello”;
String b=”hello”;
if(a==b) returns what?

A

false.
only a.equals returns true.

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

What method chartAt() does ?

````
String string = “animals”;
System.out.println(string.charAt(0));

````

A

The method charAt() lets you query the string to find out what character is at a specific index. The method signature is as follows: char charAt(int index)

````
a

````

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

StringBuilder a = “aa”;

A

Must know. DNC

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

What method indexOf() does ?

````

String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(“al”, 5));

````

A

The method indexOf()looks at the characters in the string and finds the first index that matches the desired value. indexOf can work with an individual character or a whole String as input. It can also start from a requested position. indexOf() returns –1 when no match is found.

````

String string = “animals”;
System.out.println(string.indexOf(‘a’)); // 0
System.out.println(string.indexOf(“al”)); // 4
System.out.println(string.indexOf(“al”, 5)); // -1

````

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

````
String four = “LMAO”;
System.out.println(four.substring(1));
System.out.println(four.substring(0));
System.out.println(four.substring(3));
System.out.println(four.substring(4)); //nothing
System.out.println(four.substring(-1)); //outofbounds
System.out.println(four.substring(5)); //outofbounds

System.out.println(four.substring(0, 1));
System.out.println(four.substring(0, 2));
System.out.println(four.substring(0, 3));
System.out.println(four.substring(0, 4));
System.out.println(four.substring(0, 5)); //out of bounds

System.out.println(four.substring(0, 0)); //empty

````

A

String four = “LMAO”;
System.out.println(four.substring(1)); //MAO
System.out.println(four.substring(0)); //LMAO
System.out.println(four.substring(3)); //O
System.out.println(four.substring(4)); //nothing
//System.out.println(four.substring(-1)); //outofbounds
//System.out.println(four.substring(5)); //outofbounds

	System.out.println(four.substring(0, 1)); //L
	System.out.println(four.substring(0, 2)); //LM
	System.out.println(four.substring(0, 3)); //LMA
	System.out.println(four.substring(0, 4)); //LMAO
	//System.out.println(four.substring(0, 5)); //out of bounds

	System.out.println(four.substring(0, 0)); //empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

String a= “LMAO”;
String aa =null;
System.out.println(a+aa);

A

LMAOnull

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

String a= “LMAO”;
String aa =null;
System.out.println(a+aa.toUpperCase());

A

nullpointerexception

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

String a= “LMAO”;
boolean aa =true;
System.out.println(a+aa);

A

LMAOtrue

20
Q

What method substring() does ?

````
String string = “animals”;
System.out.println(string.substring(3));
System.out.println(string.substring(string.indexOf(‘m’)));
System.out.println(string.substring(3, 4));
System.out.println(string.substring(3, 7));

````

A

It returns parts of the string. The first parameter is the index to start with for the returned string. There is an optional second parameter, which is the end index you want to stop at.

String string = “animals”;
System.out.println(string.substring(3)); // mals
System.out.println(string.substring(string.indexOf(‘m’))); // mals
System.out.println(string.substring(3, 4)); // m
System.out.println(string.substring(3, 7)); // mals

21
Q

What toLowerCase() and toUpperCase() do ?

String string = "animals"; System.out.println(string.toUpperCase()); System.out.println("Abc123".toLowerCase());

A

toUpperCase() converts any lowercase characters to uppercase in the returned string. toLowerCase() converts any uppercase characters to lowercase in the returned string. These methods leave alone any characters other than letters.

String string = “animals”;
System.out.println(string.toUpperCase()); // ANIMALS
System.out.println(“Abc123”.toLowerCase()); // abc123

22
Q

What equals() and equalsIgnoreCase() do ?

````
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));

````

A

The equals() method checks whether two String objects contain exactly the same characters in the same order. The equalsIgnoreCase() method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed.

System.out.println(“abc”.equals(“ABC”)); // false
System.out.println(“ABC”.equals(“ABC”)); // true
System.out.println(“abc”.equalsIgnoreCase(“ABC”)); // true

23
Q

What startsWith() and endsWith() do ?

````
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));

````

A

The startsWith() and endsWith() methods look at whether the provided value matches part of the String.

System.out.println(“abc”.startsWith(“a”)); // true
System.out.println(“abc”.startsWith(“A”)); // false
System.out.println(“abc”.endsWith(“c”)); // true
System.out.println(“abc”.endsWith(“a”)); // false

24
Q

What method contains() does ?

````
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));

````

A

The contains() method also looks for matches in the String. It isn’t as particular as
startsWith() and endsWith()—the match can be anywhere in the String.

System.out.println(“abc”.contains(“b”)); // true
System.out.println(“abc”.contains(“B”)); // false

25
Q

What method replace() does ?

````

System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));
````

A

The replace() method does a simple search and replace on the string. There’s a version that takes char parameters as well as a version that takes CharSequence parameters.

System.out.println(“abcabc”.replace(‘a’, ‘A’)); // AbcAbc
System.out.println(“abcabc”.replace(“a”, “A”)); // AbcAbc

26
Q

What method trim() does ?

System.out.println("abc".trim()); System.out.println("\t a b c\n".trim());

A

The trim() method removes whitespace from the beginning and end
of a String. In terms of the exam, whitespace consists of spaces along with the \t (tab) and \n (newline) characters. Other characters, such as \r (carriage return), are also included in what gets trimmed.

System.out.println(“abc”.trim()); // abc
System.out.println(“\t a b c\n”.trim()); // a b c

27
Q

What is the main difference between StringBuilder and StringBuffer ?

A

StringBuilder is faster than StringBuffer because it’s not synchronized.

28
Q

What equals() and ‘==’ do with String / StringBuilder ?

A

Calling == on String objects will check whether they point to the same object in the
pool. Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object. Calling equals() on String objects will check whether the sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.

29
Q

What Arrays.binarySearch() does ?

A

Arrays.binarySearch() 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.

30
Q

What does append () do?

````
StringBuilder sb = new StringBuilder(“Luck”);
sb.append(“is needed”).append(‘:’).append(true);
System.out.println(sb);

````

A

It’s a method in StringBuilder class.
Luckisneeded:true

31
Q

What is insert() used in?

A

StringBuilder class.

32
Q

StringBuilder sb = new StringBuilder(“animals”);
sb.insert(7, “.”);
sb.insert(0, ‘A’);

A
  1. animals.
  2. Aanimals.
33
Q

StringBuilder is mutable or immutable?

A

changeable/ Mutable

34
Q

What does delete() and deleteCharAt() do and used in?
WAP to delete all the contents of sb
WAP to delete the first char of sb

A

in StringBuilder.
Simply deletes.
````
StringBuilder sb = new StringBuilder(“hello”);
sb.delete(0, sb.length());

sb.append(“hhello”);
sb.deleteCharAt(0);
````

35
Q

String methods memorize -
length();
charAt();
indexOf();
subString();
toLowerCase();
toUpperCase();
equals();
equalsIgnoreCase();
startsWith();
endsWith();
contains();
replace();
trim();

A

Just refresh your memory

36
Q

String builder methods:
length();
charAt();
indexOf();
subString();

-new
append(),insert(),
delete(), deleteChartAt()
reverse(),
toString()

A

Just to refresh your memory

37
Q

What happens when you do equals on StringBuilder?

A

Checks for reference equality

38
Q

What is the output?
StringBuilder a = new StringBuilder(“aa”);
StringBuilder b = new StringBuilder(“aa”);
System.out.println(a.equals(b));
System.out.println(a==b);

A

false
false

39
Q

String a = new String(“aa”);
String b = “aa”;
System.out.println(a.equals(b));
System.out.println(a==b);

A

true false

40
Q

String a = “aa”;
String b = “aa”;
System.out.println(a.equals(b));
System.out.println(a==b);

A

true, true

41
Q

String a = “aa”;
String b = “bb”;
System.out.println(a=b);

A

bb
cause you are assigning.

42
Q

if(“string”.toUpperCase() == “STRING”)
{
System.out.println(true);
}
else
{
System.out.println(false);
}

A

false

43
Q

if(“string”.toUpperCase().equals(“STRING”))
{
System.out.println(true);
}
else
{
System.out.println(false);
}
}

A

true

44
Q

output?
System.out.println(“0123456789”.substring(1, 6).substring(2,4).substring(1));

A

4

45
Q

String s1=”java”;
StringBuilder s2= new StringBuilder(“java”);
System.out.println(s1==s2));

A

you cant use String == StringBuilder
gives you java: incomparable types:

46
Q

String a = “nelly”;
a.concat(“1”).replace(“1”, “L”).toUpperCase().toLowerCase();

A

It is evaulated left to right.
so contact to