go to previous page   go to home page   go to next page hear noise

Do you think that the following is correct?

str1.length() = 12 ;  // change the length of str1

Answer:

No. The left side of an assignment statement must be a variable.


Practice Program

class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // a reference to a String object.
    String str2;   // a reference to a second String object.

    int len1, len2;    // the length of str1 and the length of str2

    str1  =  ; // create the first String

    str2  =  ; // create the second String

    len1  = str1.length();    // get the length of the first string

    len2  =  . ;    // get the length of the second string

    System.out.println("The combined length of both strings is " +
         + " characters" );
  }
}

It has been a long time since you have had some blanks to fill in. I'm sure that you miss them. Complete the above program so that it

  1. Creates two String objects.
  2. The first String holds the characters "Green eggs".
  3. The second String holds the characters " and ham".
  4. The program computes the length of each string, and then
  5. Writes out the the sum of the individual lengths.

QUESTION 16:

Fill in the blanks of the program. Here are some random jottings you may wish to paste into the blanks:

length()
new String
(" and ham.")
str1
str2
(   +   )
len1
len2
()
"Green eggs"