 |
Simple Function A simple function utilizing class String Download files: SimpleFunc.javaAreas: Introduction to classes and objects. 
 |
| 1 | /* | | 2 | *Program found on a midterm given out by Paul Gries during Summer 2000 | | 3 | */ | | 4 | | | 5 | public class SimpleFunc{ | | 6 | | | 7 | public boolean f(String s, String t, int k) { | | 8 | | | 9 | return (s.indexOf(t) != k) || (s.charAt(k) != 'w'); | | 10 | | Displaying 10 of 12 lines View full source
|
 | Question 1 |  |
 | What class do indexOf() and
charAt() belong to? |
  | Question 2 |  |
 | If one were to make the method call
f(s, t, k)
with s="hello", t="he", and k=0, what would be the
order of operations executed for the line
(s.indexOf(t) != k) || (s.charAt(k) !='w')? |
  | Question 3 |  |
 | What possible values are returned by this function? |
  | Question 4 |  |
 | If s.indexOf(t) != k,
but t is not a substring of s, what can one say
about k? |
  | Question 5 |  |
 | If one were to execute charAt(index)
with index equal to -10, a string index out of bounds
exception
(i.e. java.lang.StringIndexOutOfBoundsException)
would be thrown. However, the method f
still works properly if parameter k is passed the value
-10. Why is this so? |
  | Question 6 |  |
 | If the parameter k is passed the value -1, does the
method f work? If yes, then under what
conditions? |
  | Question 7 |  |
 | What would be a good precondition for f()? |
  | Question 8 |  |
 | What kind of exception is thrown if the first two
conditions of the precondition
in question 7 are not fulfilled? |
|
 |
|