String Work Sheet

By Mickey Arnold
Last updated 10 months ago
44 Questions
DIRECTIONS : Fill in each blank with the correct answer/output. Assume each statement happens in order and that one statement may affect the next statement.

String one = "sambenwilearethebest";
String two = "09876543210";
String three = "02 13 97 68 45 0";

System.out.print( one.length());

System.out.print( two.length());

System.out.print( three.length());

System.out.print( one.charAt( 2 ) );

System.out.print( one.charAt( 5 ));

System.out.print(one.charAt(one.length()-1));

System.out.print( one.charAt( 6 ) );

System.out.print( one.substring(0,4) );

System.out.print( one.substring(5) );

System.out.print( one.substring(9) );

System.out.print( one.substring(2,7));

System.out.print( one.indexOf("abc") );

System.out.print( one.indexOf("e") );

System.out.print( one.indexOf("hij") );

System.out.print( two.indexOf("54"));

System.out.print( two.indexOf("24"));

System.out.print( one.indexOf( 'w' ));

System.out.print( two.indexOf( 'b' ));

System.out.print( two.indexOf( 's' ));

System.out.print( three.indexOf("45"));

DIRECTIONS : Fill in each blank with the correct answer/output. Assume each statement happens in order and that one statement may affect the next statement.
String sam = "hey there";
String ben = "hey thar";

out.println( sam.indexOf('h') );

out.println( sam.indexOf('7') );

out.println( ben.indexOf('a') );

out.println( ben.indexOf( 'y' ));

out.println( sam.indexOf("ey") );

out.println( ben.indexOf("ar") );

out.println( sam.charAt(3) );

out.println( sam.charAt(0) );

out.println( sam.substring(3,6) );

out.println( sam.substring(0,4) );

out.println( sam.equals(ben) );

out.println( sam.compareTo(ben) );

out.println( ben.compareTo(sam) );

out.println( ben.compareTo("abc") );

out.println( ben.replaceAll("e","#") );

out.println( ben.replaceAll("#","*") );

out.println( ben.length() );

out.println( sam.length() );

out.println( sam.charAt(20) );

To which package does String belong?

Explain method String substring(int beginIndex, int endIndex) and what it returns?

Explain method int indexOf(String str) and what it returns?

Explain method int length() and what purpose it serves?

How many indexOf methods does the String have? Define the term overloading. Is the String indexOf method overloaded?