CSIT114 – Sample Final Exam Page 1 of 7 CSIT114-Introduction to Java Namita Singla Sample Final Exam May 10, 2006 Name: _________________________________________ Attached you will find the source code for class TextLine.java. Use it to answer some of the questions that follow. In this fragment of TextLine.java (with line numbers) 1) 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. public void removeAllOccurrences(char c) { String newText = ""; for (int i = 0; i < countChars(); i++) { char nextChar = text.charAt(i); if (nextChar != c) { newText = newText + nextChar; } } text = newText; } Find a place on line 74 where a message is sent to an object. Identify the name and type of the object receiving the message, the file where you would you expect to find the code for the method that will respond to the message, and write the declaration for that method, showing its signature. CSIT114 – Sample Final Exam Page 2 of 7 2) Write code at the //method body comment that will make this method do what its javadoc says it should do: /** * Print out a range of characters from text, one to a line. * * You can assume that both start and end are less than the * total number of characters in text. * * Example: If the text is "Hello" the message * printChars(2,4) should print * * l * l * o * * @param start * the start of the range. * @param end * the end of the range. * */ public void printChars(int start, int end) { //method body } CSIT114 – Sample Final Exam Page 3 of 7 3) Describe how the author of the TextLine.java class used two important Java coding conventions. 4) Add to the TextLine class a method called reverse() to reverse the text. For e.g. if text is “Hello” (before calling this method) then it becomes “olleH” (after calling this method). This method takes no arguments and return type is void. Hint: Refer removeAllOccurrences method. CSIT114 – Sample Final Exam 5) Page 4 of 7 Scope of Variables a. (5) What is the scope of the variable “newText” declared on line 72 of TextLine.java? b. (5) Line 73 of TextLine.java contains a declaration for variable “i”. What is the scope of this variable? 6) Consider the following code fragment: int sum = 0; int i = 0; while (i < 5) { sum += i; i++; } System.out.print(sum); Replace the while loop in the fragment above with a for loop that prints the same value of sum variable. CSIT114 – Sample Final Exam Page 5 of 7 7) Consider the following loop: for (int count = 0; count < 7; count++) { System.out.println(2*count+1); } Circle and label the 4 parts of a loop (initialization, loop test, loop update, loop body). What is printed to the console during execution of the above code? 1. /* 2. Author – Namita Singla TextLine.java This class provides a string 3. that can be edited by a variety of methods that are given below. 4. Date: March 29, 2005 5. */ 6. 7. public class TextLine { 8. 9. // Instance variable 10. private String text; 11. 12. 13. /** 14. * Constructor Sets text to t 15. * @param t 16. */ 17. public TextLine(String t) { 18. text = t; 19. } 20. 21. /** 22. * Static method Returns true if c is vowel 23. */ 24. public boolean isVowel(char c) { 25. return "aeiouAEIOU".indexOf(c) >= 0; CSIT114 – Sample Final Exam 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. } 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. /** * Adds s to end of TextLine * * @param s */ public void appendToTextLine(String s) { text += s; } /** * Return number of characters in TextLine * * @return number of characters in TextLine */ public int countChars() { return text.length(); } /** * Sets TextLine to s * * @param t * New text */ public void setTextLine(String t) { text = t; } /** * Get text * * @return text */ public String getTextLine() { return text; } /** * Remove all occurrences of char c from text * * @param c * Char to remove from text */ public void removeAllOccurrences(char c) { String newText = ""; for (int i = 0; i < countChars(); i++) { char nextChar = text.charAt(i); if (nextChar != c) { newText = newText + nextChar; } } text = newText; Page 6 of 7 CSIT114 – Sample Final Exam 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. Page 7 of 7 } /** * Returns true if c is '.' or '!' or '?' * @return true if c is . or ! or ? */ public boolean isSentenceEnder(char c) { return ".!?".indexOf(c) >= 0; } /** * Clear TextLine */ public void clear() { text = ""; } /** * Print out a range of characters from text, one to a line. * * You can assume that both start and end are less than the * total number of characters in text. * * Example: If the text is "Hello" the message * printChars(2,4) should print * * l * l * o * * @param start * the start of the range. * @param end * the end of the range. * */ public void printChars(int start, int end) { //method body } }