Strings Overview
String Methods
String Indexing
100

What must you enclose a string with in Java?

Quotation marks (double quotes, not single quotes)

100

What is the output of the following code?
String s = "999";
System.out.println(s.length());

3

100

What is the index of the first character in a String?

0

200

What is the output of the following code?


String s = "This is a Java String.";
System.out.println(s);

This is a Java String.

200

What is the substring() method used for in Java?

To take a substring of a String.

200

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

300

Are Strings a primitive datatype in Java?

No.

300

What is the output of the following code?
String newString = "Cheese Chop";
String partOne = newString.substring(0, 1);
System.out.println(partOne);

C

300

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.

400

What is the output of the following code?
String s = "Chopped";
s = s + " Cheese";
System.out.println(s);

Chopped Cheese

400

What is the equals() method used for?

It is used to see if two strings are equal.

400

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

500

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

500

What is the output of the following code?String s1 = "New String";
String s2 = "String new";
System.out.println(s1.compareTo(s2));

-5

500

What is the output of the following code?
String s = "new string";
System.out.println(s.substring(4));

string

M
e
n
u