Unit 1 Lesson 8 Homework

Part A: Objects and References

  1. Identify the primitive value and the object reference.
int level = 4;
String player = new String("Mina");



  1. Identify the class, object, and reference variable.
String message = new String("Welcome");



Part B: Read and Trace Strings

  1. Determine the exact output.
String label = "Round " + 3 + 1;
String result = 3 + 1 + " rounds";

System.out.println(label);
System.out.println(result);



  1. Determine the value printed by each statement.
String word = "adventure";

System.out.println(word.length());
System.out.println(word.indexOf("vent"));
System.out.println(word.indexOf("z"));



  1. Determine the exact output.
String word = "keyboard";
String first = word.substring(0, 3);
String ending = word.substring(5);

System.out.println(first + ending);



  1. Determine the result of each comparison as negative, zero, or positive.
    a. "cat".compareTo("dog")    b. "java".compareTo("java")    c. "sun".compareTo("moon")



Part C: Find and Fix

  1. Correct the comparison to check whether both objects contain the same text.
String first = new String("team");
String second = new String("team");
boolean sameText = first == second;



  1. Correct the call to return the final three characters without going out of bounds.
String word = "notebook";
String ending = word.substring(5, 9);



Part D: Write String Code

  1. Store the first and last characters of word in separate String variables. A complete main method is not required.




  1. Explain why this call does not change word.
String word = "planet";
word.substring(0, 3);