1 / 10

Chapter 10 Review

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();

nida
Download Presentation

Chapter 10 Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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 4thcharacter 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 -12 + “”; Yes Integer.toString(-12); Yes String.valueOf(-12); Yes -String.valueOf(12); No

  10. 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

More Related