String Worksheet
1. What is the output for the following:
        String s1 = “Bob”;
        String s2 = “Linda”;
        String s3 = s1+s2;
        System.out.println(s3);
   a. Bob
   b. Linda
   c. BobLinda
   d. Bob Linda
   e. error
2. What is the output for the following:
        String s1 = "Bob";
        String s2 = s1;
        s2 = "Linda";
        String s3 = s1+s2;
        System.out.println(s3);
   a. BobBob
   b. LindaLinda
   c. BobLinda
   d. Bob
   e. error
3. The statement s3=s1+s2; is equivalent to:
   a. s3=s2+s1;
   b. s3.concat(s1,s2);
   c. s3 = s2.concat(s1);
   d. s3 = s1.concat(s2);
   e. none of these
4. What is the output for the following:
      String s1 = "Bob";
      String s2 = "Linda";
      String s3 = new String("Bob");
      if(s1==s3)
                System.out.println("They are equal");
      else
                System.out.println("Not equal");
   a. They are equal
   b. Not equal
   c. Error
5. What is the output for the following: ****This problem may be omitted for A students*****
      String s1 = "Bob";
      String s2 = "Linda";
      String s3 = "Bob";
      if(s1==s3)
                System.out.println("They are equal");
      else
                System.out.println("Not equal");
   a. They are equal
   b. Not equal
   c. Error
6. What is the output for the following:
      String s1 = "Bob";
      String s2 = "Linda";
      String s3 = new String("Bob");
      if(s1.equals(s3))
                System.out.println("They are equal");
      else
                System.out.println("Not equal");
   a. They are equal
   b. Not equal
   c. Error
7. What is the output for the following:
      String s1 = "Bob";
      String s2 = "Linda";
         if(s1.compareTo(s2)==0)
                 System.out.println("They are equal");
         else if(s1.compareTo(s2)<0)
                 System.out.println(s1+" is smaller");
         else
                 System.out.println(s2+" is smaller");
   a.   They are equal
   b.   Bob is smaller
   c.   Linda is smaller
   d.   Error
8. Which of the following would assign s3 to “in” using the declarations in #7?
   a. String s3 = s2.substring(2,2);
   b. String s3 = s2.substr(1,2);
   c. String s3 = s2.substring(1,3);
   d. String s3 = s2.substring(1,2);
9. What is the output for the following:
      String s1 = "bob";
      String s2 = "Linda";
         if(s1.compareTo(s2)==0)
                 System.out.println("They are equal");
         else if(s1.compareTo(s2)>0)
                 System.out.println(s1+" is bigger");
         else
                 System.out.println(s2+" is bigger");
   a.   They are equal
   b.   bob is bigger
   c.   Linda is bigger
   d.   Error
10. What is the output for the following:
      String s1 = "bob";
      String s2 = "Linda";
         if(s1.compareToIgnoreCase (s2)==0)
                 System.out.println("They are equal");
         else if(s1.compareToIgnoreCase(s2)>0)
                 System.out.println(s1+" is bigger");
         else
                 System.out.println(s2+" is bigger");
   a.   They are equal
   b.   bob is bigger
   c.   Linda is bigger
   d.   Error
11. What is the output of the following code if banana is entered?
      s1 = // a String read from the user
        for(int i = 0; i < s1.length(); i++)
             s2+=s1.charAt(s1.length()-i-1);
        System.out.println(s2);
   a.   banana
   b.   bananaananab
   c.   ananab
   d.   error
12. What is the value of x:
      String s2="fast car";
      int x = s2.indexOf(‘a’);
   a.   1
   b.   2
   c.   7
   d.   6
   e.   –1
13. What is the output of the following?
      String s1 = "Result";
      s1 = s1 + 1 + 2;
      System.out.println( s1 );
   a.   Result12
   b.   Result3
   c.   Syntax error
   d.   Result 1 2
   e.   R12
14. What is the output of the following?
      String s1 = "Result";
      s1 = 1 + 2 + s1;
      System.out.println( s1 );
   a.   12Result
   b.   3Result
   c.   Syntax error
   d.   1 2 Result
   e.   12R
15. What is the output of the following?
      String s1 = "Result";
      s1 = s1 + (1 + 2);
      System.out.println( s1 );
   a.   Result12
   b.   Result3
   c.   Syntax error
   d.   Result 1 2
   e.   R12
16. What is the output of the following?
      String s1 = "Result";
      s1 = s1 + 3 * 4;
      System.out.println( s1 );
   a.   Result34
   b.   Result12
   c.   Syntax error
   d.   Result 3 4
   e.   R34
17. What is the output of the following?
      String s1 = "Result";
      s1 = 3 * 4 + s1;
      System.out.println( s1 );
   a.   34Result
   b.   12Result
   c.   Syntax error
   d.   3 4 Result
   e.   34R
18. What is the output of the following?
      String s1 = "mississippi";
      s2 = "is";
      int result = s1.indexOf(s2);
      System.out.println( result );
   a.   0
   b.   -1
   c.   11
   d.   1
   e.   4
19. <Fill in the blank>
    What is the output of the following?
       String s1 = "ohio";
       String s2;
       for(int i = 0; i < s1.length( ); i++)
       {         s2 = s1.subString(i);
                 System.out.println(s2);
       }
_________________________________________________________
20. What is the output of the following?
      String s1 = "missouri";
      String s2 = "mississippi";
      System.out.println( s2.indexOf( s1.substring(1,4) );
   a.   0
   b.   1
   c.   2
   d.   3
   e.   -1
21. (From apcentral) Assume the following declarations have been made
        private String s;
        private int n;
        public void changer(String x, int y)
        {
                x = x + "peace";
                y = y * 2;
        }
Assume s has the value "world" and n has the value 6. What are the values of s and n after the call
changer(s, n) ?
         s                  n
   a.   world               6
   b.   worldpeace          6
   c.   world               12
   d.   worldpeace          12
   e.   peace               12