This book is now obsolete Please use CSAwesome instead.

4.12. Code Practice with Strings

The following code should get the first letter of the first name, middle name, and last name and append (concatenate) them together and then return them all in lowercase. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 has an ending ' instead of ". Line 7 is missing a =. Line 8 has firstname, but it should be firstName. Remember that you should uppercase the first letter of each new word, after the first word, to make the variable name easier to read (use camel case). Line 9 has subString, but the method name is substring. Line 11 is missing a ).

Show Comments

The following code should print “Mary’s favorite color is blue”. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing a starting ". Line 6 is missing a ending ;. Line 7 has Name when it should be name. Remember that variable names start with a lowercase letter.

Show Comments

The following code should print “Gabby’s favorite sport is soccer”. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 is missing a =. Line 6 is missing the closing ". Line 7 has Name when it should be name. Remember that a variable name starts with a lowercase letter. Line 8 is missing an ending +.

Show Comments

The following code should print the first 3 letters of the string message all in lowercase letters. However, the code has errors. Fix the errors so that the code runs as intended.

Line 5 ends with : when it should be ;. Line 6 should be substring(0,3). Line 7 should be part not message. Line 8 should be System.out.println.

Show Comments

The following code should print “Your name is Carly and your favorite color is red”. Finish the code so that it prints the output correctly.

Add the required strings using the + operator and be sure to include spaces as needed.

Show Comments

Finish the code below so that it prints “Your name is Justin and your age is 16”.

Use the + operator to append the strings. Be sure to include spaces as needed.

Show Comments

Write the code to print “Julian’s favorite color is green. His favorite food is pizza.”.

Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.

Show Comments

Finish the code below to print your favorite animal and food.

Use + to add strings together. Add spaces as needed and periods.

Show Comments

Finish the code below to print your favorite movie and book.

Add the strings together using +. Don’t forget to include spaces and periods at the end of the sentences.

Show Comments

The following code starts with String firstNameCaps = ALEX; and should print Alex. Use the toLowerCase and substring methods to do accomplish this task.

Create a string that is all lowercase. Create a new string from a substring of the original string (first letter) and a substring of the rest of the string that is all lowercase (all except the first letter). Print that string.

Show Comments

The following code should remove the word “very ” (and following space) from the message and print the new message. You can use indexOf to find the position of a substring in your string. You can use substring to create a new string removing the word.

Use indexOf to find the position and then create a new message up to the pos and again after the target string.

Show Comments

The following code should replace lol in the message with laugh out loud and print the new message.

Use indexOf to find the position of the “lol” then create a new string from up to that position and append the “laugh out loud” and the substring after it.

Show Comments
You have attempted of activities on this page