100 likes | 159 Views
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar (String s1, String s2) { int length1 = s1.length(); int length2 = s2.length();
E N D
Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public booleanlastChar(String s1, String s2) { int length1 = s1.length(); int length2 = s2.length(); boolean b = s1.(charAt(length1 – 1 ).equals. s2.(charAt(length2 – 1); return b; }
Write a method that finds the first opening and last closing double quote in a given string and returns the string inside. If not found, your method should return the original string unchanged. Sample Answer: public static String quote(String str) { int first = str.indexOf(‘”’); int last = str.lastIndexOf(‘”’); if (first != -1 && last != -1) System.out.println(str.substring(first, last – 1)); else System.out.println(str); }
Are literal strings objects of the String type? yes Can you use \n in literal strings? yes Would this cause an error: “Mock”.equals(str); no Can you represent an empty string with null? no What is the length of an empty string? 0 Once a string is constructed, can it be changed? no
Main advantage of immutable objects? Can refer to object later without copying String str = “Computer”; int n = str.length(); 8 What would str.charAt(8) return? StringIndexOutOfBoundsException What does s.substring(3) do? Returns tail of string starting from the 4thcharacter For String state = “Georgia”, what does state.substring(2, 3) return? o
What is the difference in s1 + s2 and s1.concat(s2)? none String s1 = “basket”; String s2 = s1; s2 += “ball”; System.out.println(s1 + s2); basketbasketball What does stars.indexOf(‘*’) return? integer of the first occurance of the * What does stars.firstIndexOf(‘*’) return? no firstIndexOf method
For String state = “Georgia”, what does state.substring(1, 5) return? eorg state.substring(4) return? gia String s1 = “dog”; String s2 = “blog”; What is returned by s1.compareTo(s2)? a positive number What is returned by s2.compareTo(s1)? a negative number What is returned by the following code? s1.toUpperCase(); System.out.print(“*” + s1 + “*”); *dog*
String song = “Best Day of My Life”; String song2 = “ we own the night “; System.out.println(song.replace(‘ ‘, ‘#’)); What is printed? Best#Day#of#My#Life System.out.println(song.replace(‘ ‘, ‘*’).replace(‘e’, ‘E’)); BEst*Day*of*My*LifE What is returned by song2.trim()? we own the night What is returned by song.substring(1, 6)? est D What is returned by song.substring(9)? of My Life
Does each of the following produce a string “-12”? “” + -12; Yes “” – 12; No “-” + 12; Yes -12 + “”; Yes Integer.toString(-12); Yes String.valueOf(-12); Yes -String.valueOf(12); No
Is the following a method of the character class? • booleanisDigit(char ch) • Yes • booleanisWhiteSpace(char ch) • Yes • char toUpperCase(char ch) • Yes • booleanisLetter(char ch) • Yes • booleanisLetterOrDigit(char ch) • Yes • booleanisUpperCase(char ch) • Yes