Name: 
 

Chapter 7 - "Characters, Strings, and the StringBuffer



Multiple Choice: Identify the choice that best completes the statement or answers the question.
 

 1. 

Which of the following would you use to compare the contents of two Strings?
A.
equals( )
C.
isEqual( )
B.
==
D.
=
 

 2. 

What is the output of the following program?

public class Test {
   public static void main(String[] args) {
       String s1 = "Testing";
       String s2 = "testing";
       String s3 = s1.toLowerCase();
       if (s2.equals(s3))
         System.out.println("true");
       else
         System.out.println("false");
   }
}
A.
true
C.
Testing
B.
false
D.
testing
 

 3. 

Which of the following would you use to compare strings without considering case?
A.
==
C.
equals( )
B.
equalsIgnoreCase( )
D.
isEqual( )
 

 4. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
      String s1 = "Programs";
      String s2 = "programs";
      if (s1.equalsIgnoreCase(s2))
         System.out.println("True");
      else
         System.out.println("False");
  }
}
A.
programs
C.
False
B.
True
D.
Programs
 

 5. 

The value of n after the following program executes is ____.

public class Test {
  public static void main(String[] args) {
      String s1 = "Programs";
      String s2 = "Progress";
      int n = s1.compareTo(s2);
      System.out.println(n);
  }
}
A.
0
C.
positive value
B.
false
D.
negative value
 

 6. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
    String name = "Jack Smith";
    int i = name.indexOf(' ');
    System.out.println("i = " + i);
  }
}
A.
i = -1
C.
i = 4
B.
i = 3
D.
i = 5
 

 7. 

What is the value of c after the following code executes?

     String s = "Application";
     char c = s.charAt(5);
A.
i
C.
a
B.
l
D.
c
 

 8. 

What is the value of result?

    String s = "Programming";
    boolean result = s.startsWith("Program");
A.
0
C.
true
B.
1
D.
false
 

 9. 

What is the value of s2 after the following statements execute?

    String s = "AAABBBCCCDDD";
    String s2 = s.replace('C', 'X');
A.
"AAABBBXXXDDD"
C.
"AAABBBXCCDDD"
B.
"AAABBBCCCDDDXXX"
D.
"AAABBBXXXCCCDDD"
 

 10. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
     String location = "downtown";
     int j = location.indexOf('l');
     System.out.println("j = " + j);
  }
}
A.
j = -1
C.
j = 4
B.
j = 0
D.
j = 8
 

 11. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
      String s1 = "A simple program";
      boolean b = s1.startsWith("A simple program");
      System.out.println("b = " + b);
  }
}
A.
b = true
C.
b = -1
B.
b = false
D.
b = 1
 

 12. 

What is the output of the following program?

public class Test {
  public static void main(String[] args) {
    String myString = "A class about strings";
    String myString2 = myString.substring(0, 13) + " main";
    System.out.println(myString2);
  }
}
A.
A class about strings
C.
A class main
B.
A class about main
D.
A class aboutmain
 

 13. 

What is the output of the following program?

public class Test {
  public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Testing with a String Buffer");
      System.out.println("The length of the string is: " + sb.length());
  }
}
A.
The length of the string is: 29
B.
The length of the string is: 26
C.
The length of the string is: 27
D.
The length of the string is: 28
 

 14. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("This is a test!");
      sb.setLength(10);
      System.out.println(sb);
  }
}
A.
This is a
C.
This is a t
B.
This is a test!
D.
This is
 

 15. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("This is a test!");
      System.out.println("The capacity of the string is: " + sb.capacity());
  }
}
A.
The capacity of the string is: 15
B.
The capacity of the string is: 31
C.
The capacity of the string is: 16
D.
The capacity of the string is: 14
 



 
Check Your Work     Start Over