Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 10 Review

2 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(); boolean b = s1.(charAt(length1 – 1).equals. s2.(charAt(length2 – 1); return b; }

3 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); }

4 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

5 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 4 th character For String state = “Georgia”, what does state.substring(2, 3) return? o

6 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

7 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*

8 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

9 Does each of the following produce a string “-12”? “” + -12; Yes “” – 12; No “-” + 12; Yes “”; Yes Integer.toString(-12); Yes String.valueOf(-12); Yes -String.valueOf(12); No

10 Is the following a method of the character class? boolean isDigit(char ch) Yes boolean isWhiteSpace(char ch) Yes char toUpperCase(char ch) Yes boolean isLetter(char ch) Yes boolean isLetterOrDigit(char ch) Yes boolean isUpperCase(char ch) Yes


Download ppt "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."

Similar presentations


Ads by Google