String Fumble Example
The following program excerpt is an exercise about
String operations. The program tries to compare, compare
(ignoring case difference) and copy Strings. Please refer
to the comments for the motivation. However, there are some
mistakes, which may make the program inconsistent with the
intention of the programmer. Please try your best to find these
mistakes before reading the questions.
Download files: StringFumbles.java
Areas: if statements.
Download files: StringFumbles.java
Areas: if statements.
|
|
StringFumbles.java |
|
View full source |
| Question 1 |
| Is there a problem if we change the first line of code
to the following String initialization? String firstStr = 'I am Canadian'; If so, what is it? |
| Question 2 |
| After thirdStr is assigned a value, what is the String in thirdStr? |
| Question 3 |
| Do firstStr and thirdStr have the same character String after the assignment statement for thirdStr? |
| Question 4 |
| Does the following comparison tell us whether the character
strings are the same? if (firstStr == thirdStr) |
| Question 5 |
| What is the output of the first if statement? |
| Question 6 |
| How should we correct the following comparison? if (firstStr == thirdStr) |
| Question 7 |
| What does the following code do? String fourthStr = new String(firstStr); |
| Question 8 |
| What is the output of the second if statement? |
| Question 9 |
| What does the following code do? String fifthStr = firstStr; |
| Question 10 |
| What is the output of the third if statement? |
| Question 11 |
| Does the following String comparison have
the intended effect? if (0==firstStr.compareTo(thirdStr)) |
| Question 12 |
| Aside from the problem discussed in the above question, there is another problem in the last if statement. Can you find it? |
