What must you enclose a string with in Java?
Quotation marks (double quotes, not single quotes)
What is the output of the following code?String s = "999";
System.out.println(s.length());
3
What is the index of the first character in a String?
0
What is the output of the following code?
String s = "This is a Java String.";
System.out.println(s);
This is a Java String.
What is the substring() method used for in Java?
To take a substring of a String.
What is the output of the following code?String s = "Pi is 3.14159";
System.out.println(s.substring(0, 10));
Pi is 3.14
Are Strings a primitive datatype in Java?
No.
What is the output of the following code?String newString = "Cheese Chop";
String partOne = newString.substring(0, 1);
System.out.println(partOne);
C
What does the indexOf() method do?
It searches a String for the first occurrence of another given String. If it is not found, then it returns -1.
What is the output of the following code?String s = "Chopped";
s = s + " Cheese";
System.out.println(s);
Chopped Cheese
What is the equals() method used for?
It is used to see if two strings are equal.
What is the output of the following code?String s = "abcdefghijklmnopqrstuvwxyz";
int len = s.length() - 1;
String desiredString = s.substring(len, len + 1);
System.out.println(desiredString);
z
How do you add a quotation mark into a String in Java?
Put a backslash before the quotation mark in the String.
Example:
String famousQuote = "\"An apple a day keeps anyone away if you throw it hard enough.\" -Unknown";
System.out.println(famousQuote);
This will print out:
"An apple a day keeps anyone away if you throw it hard enough." -Unknown
What is the output of the following code?String s1 = "New String";
String s2 = "String new";
System.out.println(s1.compareTo(s2));
-5
What is the output of the following code?String s = "new string";
System.out.println(s.substring(4));
string